Infrared Distance Encoder

As we’ve seen, Afterthought Cake is a tripod robot. The third wheel is not connected to a motor. Instead, that wheel rolls freely to detect the distance traveled. Let’s see how that works.

In the rear, towards the bottom of the robot, is a breadboard with a large pushbutton. Underneath the board is an infrared emitter (LED) and an infrared detector (photosensor) arranged as an interrupter pair.

Front of encoder and pushbutton board Back of encoder and pushbutton board

Breadboard with pushbutton on the front and infrared emitter-detector on the back

The 180 ohm resistor prevents too much current from passing through the emitter, which would damage it. At a maximum of 5 volts (when plugged into the STK500 programming board), the emitter will receive about 18 mA. A lower resistance would produce a brighter light, which might be so bright as to pass through the plastic Lego wheel or bleed around the corners to provide an undesirable constant detection. A higher resistance might not produce a bright enough light, particularly when the battery voltage is just above 3 V.

The detector is in series with a 22 kilohm resistor to divide the battery’s voltage between the resistor and the detector. The greater the amount of light, the higher the voltage present between the two components (where the purple sensor wire attaches).

The photograph below has the detector removed so that you can see the infrared light from the detector’s perspective. Humans cannot see infrared light; however, the digital camera detects and displays it as faint purple.

Infrared encoder emitter seen through LEGO wheel

Infrared encoder emitter seen through LEGO wheel

On the left side of the photo, light is able to pass through the hole in the Lego hub to be seen by the detector. On the right side of the photo, the wheel has rotated far enough that the thick spoke blocks the light.

By monitoring the change in voltage on the detector, the microcontroller can measure the speed and distance travelled by the robot. However, the robot cannot determine direction, because doing so requires at least two sensors, such as in a quadrature encoder.

Emitter Detector Pair

You can purchase the infrared components individually, or you can buy them as a pair molded together into a single piece. The advantages of buying a single piece are:

Unfortunately for me, the thickness of the Lego tire (about 0.14 inches) is wider than the gap (0.125 inches) in the infrared pair that I wanted to use. So, I cut the pair apart using a 1/8th inch end mill on a milling machine.

Splitting slotted optical switch on milling machine

Splitting slotted optical switch on milling machine

Interestingly, after being cut, the individual emitter and detector pieces slide out of the larger package (see bottom right of photo below).

OPTEK OPB660N Infrared Slotted Optical Switch

OPTEK OPB660N Infrared Slotted Optical Switch

Although the emitter and detector could be used bare, the slits or masks ① from the sides of the original package are preferred. The slits reduce the likelihood of light seeping around the holes in the Lego wheel, which would cause the light to always be detected. The slits are another advantage of using a manufactured pair.

Side note: Notice there diagonal notches ② in the corners and bumps on the bottom ③ of the emitter and detector that match the shape of the slitted covers. This appears to be a quality-control feature for the factory. The parts cannot be inserted into the wrong slot, nor can they be inserted backwards.

The final benefit to using a manufactured pair as opposed to buying separate components is that the manufacturer can alter the characteristics of the parts since they know the intended purpose. In this case, Optek makes the detector swing more aggressively to a high or low value, since the intended purpose is detection versus no detection. Although the sensor still outputs intermediate voltages as the wheel hole transitions during movement, it is weighted toward the extremes. In comparison, generic infrared sensors tend to be more gradual for distance or brightness measurement.

We'll get back to the other electronic circuits later in the article. For the moment, let’s see how the holder for the encoder wheel was made.