Jul 13

Translink magnetic ticket format

Translink, the public transit provider for Vancouver, BC, will soon be switching to a stored-value RFID card (the Compass Card), so I figure there's no harm in publishing this now. In short, it's trivial to create Translink tickets which expire many years in the future.

A PDF copy of the report is available at http://qartis.com/translink.pdf.

Some of the code is available at http://github.com/qartis/translink/.

Jul 13

Arduino laser distance meter

UPDATE 2: Serial commands and timing information are now available here.

UPDATE: I've posted code to parse the serial datastream here.

This is a tutorial on how to get serial data out of a cheap handheld laser distance meter, which can be fed into an Arduino or a PC. You can use the distance measurements in all sorts of projects like robots and quadcopters. There are vendors who sell conversion kits that sniff the distance measurement off the LCD and output it over serial. The problem with those kits is that they cost more than the laser meter itself. Luckily I managed to find a laser distance meter that outputs distance measurements over serial directly: the UT390B from Uni-T which sells for about $56 from online retailers (that is an affiliate link). Laser distance meters use precise electronics to measure the length of time it takes a laser beam to reflect back from a target. Handheld units like this one have a range of about 0.2m - 40m with a precision of 2mm. They're a great replacement for HC-SR04 ultrasonic sensors if you need long range measurements.

Preparing the unit

You can see the unit's debugging port underneath the battery cover. To take it apart, remove the 2 visible screws.

ut390b device with battery cover removed showing debug pins

Now you can pry off the cover.

ut390b with front cover open

This ribbon cable connects the unit's keypad. Scrape off the glue and unplug it for now.

the ribbon connecting the two halves of the ut390b device

Next, remove the 2 screws with washers that hold the rubber piece at the front. If you want to remove the screen, it's connected by 4 clips to the circuit board. It's probably a better idea to leave the screen connected, so you can verify that the unit is working properly while you're testing your serial parsing code.
removing the screen from ut390b device DON'T remove these 3 screws. They connect the laser diode to the circuit board, and if you loosen these screws you risk breaking your laser. 3 screws on circuit board which should not be removed The easiest way to access the debug port is by desoldering the battery connector as shown below. Note: this photo shows the 3 screws removed. DO NOT do this, as it's unnecessary and could cause the unit to break in two.removing the ut390b circuit board (don't do this) Finally you can solder some thin wires to the debug port. The pinout is as follows, seen from below the board.pinout of ut390b debug pins. from left to right: GND, RX, TX, BOOT0, VCC (2.7V) We can replace the original keypad with a microcontroller which can simulate button presses to control the unit. The keypad pinout is below. The ribbon connector is 8 pin, 1mm pitch. If there's enough interest, I will assemble and sell a small interface board to break out the necessary signals. pinout of ut390b keypad ribbon connector. from left to right: Unit, n/c, Func, On, GND, Mode, n/c, Off

Decoding the serial output

This unit's serial port runs at 115200 baud (8N1). On bootup, it outputs the following text with \r\n line endings:

curent ver:420411
Year:Jan 21 2013 Time:13:53:10
Iint OK

Notice "APDMIN" and "APDMAX", which are likely calibration values for the avalanche photodiode used to detect the reflected laser beam. A few sloppy printfs as well.. To turn the unit on, connect the ON signal to GND for about 300ms. Once it's on, the ON signal is also used to take a measurement. After a measurement is taken, the unit outputs the following:

Dist: 2827,curtemp =21
nDist: 2827,tempDv=0

The Dist and nDist values are in millimeters. As far as I can tell, the two measurements are always identical. If there's a measurement error (Error 154, out of range or sensor error) the unit will output:

OUT_RAN dist = 30

If the unit can't determine the average distance (Error 160, sensor shaking too much or Error 155, signal too weak) the unit will output one of the following lines:


When you turn it off, it outputs:

WriteTestData TRUE

Serial input?

I'd like to find a way to control the unit over serial. So far all I've found is that sending 0x23 ('#') or 0x73 ('r') will cause the unit to power down. It seems to ignore every other byte, as well as some common english words and modbus commands. If you manage to find any other byte sequences, email me or leave a comment below. If you liked this article, you might also like DORI, my robot project. DORI uses a slightly different laser distance measurement strategy. You can learn more on the project homepage and in my thesis.

Jul 13

Problems with 23K256 and AVR/Arduino

23K256 IC pinout

The Microchip 23K256 is a great little device with 32 KiB of fast static ram over an SPI interface. I had some trouble getting this chip to work, and after searching online it looks like many people have some common problems. I'll show you a brief checklist you can use if you're having trouble like I was.

  1. The HOLD pin. This pin can be used to pause a sequential transfer and resume it immediately without having to re-send the address. If you don't care about this feature, tie the HOLD pin to Vcc (3.3v).
  2. The NC pin. This pin should not be connected to anything, including Vcc or GND. Just leave it floating.
  3. The HOLD bit. Read the datasheet carefully: writing a 1 bit disables the HOLD feature. Make sure you're setting this bit HIGH.
  4. Vcc. If you're using short wires and you're not sharing the SPI bus among several devices, the 23K256 can be powered from 2.7V. But this chip seems to need strong SPI signals with low capacitance. If you're running into problems using the chip on a shared SPI bus, try giving the chip a higher voltage supply -- it can tolerate up to 4.5V. Just don't permanently run the chip above 3.6V or it may become unreliable.
  5. The voltage levels. This chip runs from 2.7V - 3.6V. If you're interfacing with a 5V Arduino, you'll need to convert the Arduino's 5V signals to around 3V using a level converter circuit. This is simple with SPI because all of the data lines are unidirectional -- they're either microcontroller-to-sram or sram-to-microcontroller. The 23K256's 3V logic output should be high enough to be read directly by modern microcontrollers like the AVR in an Arduino. The microcontroller's 5V signals must be dropped to around 3V before they reach the 23K256. A simple voltage divider for these signals (MOSI, SCK, CS) will work at low SPI speeds, and can be built from 2 resistors per signal.
  6. MOSI/MISO. The chip's datasheet labels these pins SI/SO. There are several sources online (including Wikipedia) that say SO means Serial Output. Nope, when we're dealing with SPI, SO always means Slave Out. As in, the data stream going out from the SPI slave device (the 23K256) to the microcontroller.
  7. SS/CS. This is a big gotcha with Arduinos and all Atmel AVR microcontrollers. You can use any gpio pin as a Slave Select (aka Chip Select) line. However, the microcontroller's primary SS pin must be set as an output during SPI operation, even if you're not using it. If the SS pin isn't explicitly configured as an output pin, SPI transactions will mysteriously fail (usually by timing out because the SPIF bit never goes high), because the SPI hardware will be in slave mode. This is an easy mistake to make!
  8. The init sequence. The 23K256 essentially has no init, but we can write a simple routine to make sure communication is working:
#define SRAM_DESELECT() PORTD |= (1 << PORTD7);
#define SRAM_SELECT() PORTD &= ~(1 << PORTD7);

enum {

#define SRAM_MODE 0x41 /* sequential access, hold pin disabled */

uint8_t sram_read_status(void)
    uint8_t val;

    val = spi_write(0xff);

    return val;

uint8_t sram_init(void)
    uint8_t retry;

    DDRD |= (1 << PORTD7); /* set CS pin as output */


    retry = 255;
    while (sram_read_status() != SRAM_MODE && --retry) {

    return retry != 0;