After the two posts describing the
A Bit of Theory
|Small optical encoder, using a graduated disk and an IR diode/phototransistor pair|
To visualise how this work you can make a simple “device” as shown below. It’s a narrow piece of square-ruled graph paper with back stripes two squares wide, and another piece with two squares cut out that serves as a reader with two sensors. As you slide the stripe through the reader and count the number of times the first sensor reads “black”, you’ll be able to tell how far the stripe has moved. Now pay attention to what happens the second sensor at that moment. You will notice that if you move the stripe one way, the second sensor is white, but the if you pull the stripe in the other direction, the sensor will be black. Detecting change from white to black gives you resolution down to one inch (in this case), but if you implement a simple state machine and detect every transition on both sensors your resolutions will be 0.25”. In a nutshell this is precisely what happens inside the glass scales, except the stripe is much finer.
|A crude implementation of optical encoder should help to visualize the principle at work|
There are several practical considerations that will influence your interface hardware when working with quadrature encoders. The most basic parameter is the working voltage of the scales. The vast majority of quadrature encoders I’ve encountered run at 5V, but there are some that run at 12V or 24V and even higher. As long as the voltage is higher than 3.6V or lower than about 2.6V, you will need to implement some sort of level shifting circuit.
Second important characteristic is the output waveform. Most industrial grade encoders have built-in circuitry that conditions the signal from the sensors. The common glass scales that you’re likely to come across [on eBay,
Finally, some of the more expensive
Interfacing quadrature encoders to the MSP430 Launchpad board can pose two kinds of problems. If the output voltage is too low (i.e. is lower than MSP430’s “high” threshold), the microcontroller will not detect or at least miss the pulses coming from the scales. On the other hand, if the voltage is higher than 3.6V, there is a really good chance that the microcontroller's inputs might be damaged.
|Simple resistor voltage divider|
(Image courtesy of Wikipedia)
The voltage shifter/Interface board described in one of my earlier posts was designed to take care of both issues. The LM339 comparators used on that board can handle pretty wide range of pulse voltages, convert sinusoidal waveforms to square waves and even filter brief line glitches. This way, as long as you provide the correct voltage to the scales and the comparator ground references, the board will output clean 3.3V pulses that are safe for MSP430. This flexibility comes at a cost of complexity, though, so for my
If you choose to use the simplified setup, you will need two such dividers per scale (one for each of the two channels). The values I choose were dictated by what I had in my resistor box. As long as the ratio stays close to ⅔, you have some wiggle room but I wouldn’t want to use resistors whose combined values are much lower than 5 Kohm. One caveat though: make sure that the divider is connected to the common ground or the voltage divider won’t work and you have a good chance of zapping your microcontroller.
Now the tricky part: there is no standard connection pinout. Different
|Pin functions for my Easson DRO scales|
(image courtesy of Easson)
Once you obtain the pin diagram, you will need to identify and connect the following lines:
|LaunchPad's TP1 provides convenient 5V supply|
- 5V - positive power supply to the scales (might be market Vcc). This line needs to be connected to TP1 on the Launchpad
- 0V - “ground” for the scales circuitry. This pin needs to be connected to the Launchpad’s ground.
- “Gnd” (or “Ground”) - this is the scale’s frame and can be connected to the same point the 0V is connected.
- A and B - these are the two channels that will provide the pulses to the controller. These need to be connected [through a voltage divider] to the Launchpad’s input pairs. These are: P2.0/P2.5, P2.1/P2.4, P2.2/P2.3/ XIN/XOUT for X,Y,Z and W axes respectively. It doesn’t matter if you flip A and B lines for each pair, though. All it will do is invert the readout, but that can be changed in the TouchDRO settings.