40h optical encoder tutorial

the 40h has several “extra” easily-soldered pads. these are programmed to be used as analog inputs, if you’re inclined to add knobs or sliders or the like.

as we’ve stated before, the 40h firmware is open source. this means users can change the hardware functionality of the unit.

we’ve written a new subversion of the firmware which enables the interfacing of two rotary optical encoders. the following tutorial illustrates how to get it working.

optical rotary encoders vs. conventional potentiometers

similarities: they’re both knobs, or similar to knobs in that they mechanically provide continuous values (versus a on/off toggle).

potentiometers are variable resistors:

  • it’s easy for a microcontroller to read the position of the knob via an analog to digital conversion. (this code is contained in the original 40h firmware.)
  • ADCs are noise-prone and often require averaging across several samples, resulting in lag.
  • values from a potentiometer are also absolute (the value is the position of the knob), and most knobs have bounds (a max and min).
  • accuracy for tiny tiny movements is not so good.
  • super super cheap.

encoders send out pulses:

  • it’s also easy to read by monitoring the pulses in a timed function
  • much more immune to noise
  • no lag
  • infinite rotation, as the delta position is transmitted, not an index. this allows more flexible software interfaces, where the hardware won’t trash onscreen-modified values, allowing a more solid sync
  • incredible resolution and sensitivity (4096 ticks per revolution)
  • expensive

disclaimer

by all means we want you to open up and change your hardware. however, this document is only a guide. we are not responsible if something goes wrong. we will help you fix your problem and we’ll even repair your unit (though you’ll have to pay us).

please be thoroughly acquainted with the entire process before deciding to potentially endanger your hardware. on the other hand, it’s somewhat difficult to break things.

onward

http://usdigital.com has wonderful encoders.

get an S4-256 and matching cable. it’ll run about $50. maybe we’ll work out a group buy on the forum. the encoder shown in the tutorial is different (more expensive and higher resolution, overkill). (Picture of enc03)

before getting started, make sure you can get avarice installed on your system (application to upload the firmware): http://avarice.sourceforge.net/

you’ll also need a JTAG programmer. the only one we’ve tried, hence can suggest, is from olimex. it’s incredibly cheap (relative to the rest of the embedded development industry) and very useful: http://www.sparkfun.com/commerce/product_info.php?products_id=12

step 1: open it up

update: more on disassembly! howto - disassemble the 40h

flip it over, take out the screws with a small hex wrench.

use a putty knife (or similar) to separate the thin silicone glue layer between the plate and base. careful not to cut into the base.

grip and pull up a corner. don’t be shy. i know it’s scary.

unplug the ribbon cables. unscrew and remove the logic board. don’t lose the spacers.

step 2: connect

strip the cable ends to be connected to the logic board. insert them while the encoder is plugged into the cable assembly to ensure proper connection (see Picture of enc06 and Picture of enc09). GND goes to J_0, 5V goes to J_5, A or B go to J_A (left two positions).

solder! use high heat, solder fast, clip the ends. at this point you might also want to solder on a 2×5 .1” connector to the jtag port of the logic board, though optional. (see Picture of enc13).

screw the logic board back to the grid. hold the logic board away from the edge of the unit while screwing in (push it towards the center). reconnect the ribbon cables.

step 3: program firmware

connect the jtag programmer. if you didn’t solder on a connector, plug the connector into the programmer, and tilt the connector against the jtag port.

given that you compiled avarice and got it working, and you’re in the folder of the encoder source files, type:

avarice --erase --program --file 40h.hex --jtag /dev/tty.usbserial-1B1 --jtag-bitrate 500KHz

where ”/dev/tty.usbserial-1B1” is the serial port address of your programmer (try ls -lrt /dev on os X or linux)

this is probably a good time to test. make sure you have the new version of serialio, and the max/msp test patch (not yet posted).

step 4: connectorize and assemble

do not do what we do here, we’ll be mad at you. seriously, the only reason we did this is because we had an old defective base hanging around. we just cut a notch in the silicone and fed the cable through. (see Picture of enc16). this is bad because:

  • it’s not disconnectable!
  • that really-really is not cool.
  • just don’t do it.

what we do suggest is locating a 4 conductor connector that has clearance to fit on the inside of the unit, then have a matching connector for the cabled encoder. we haven’t had time to find one yet, and if someone finds a good candidate (something secure with a screw-on washer or the like) please post it. hold on, we did it:

while you’re at it, why not clean the base with some rubbing alcohol and a paper towel?

finished!

add a huge knob. because the resolution is good enough to handle it.

take a look at the newest serial protocol sheet if you’re so inclined.

Edit this page or watch for changes using RSS.