Fine Positioning using an Optical Interrupter

Some engineers leave their technical skills at the office when they head home, and avoid technically-related activities during their (all too scarce) free time. Some of us, however, are drawn to hobbies with links to engineering; one of the most obvious is the honorable craft of model railroading. I've been a member of the Tech Model Railroad Club (TMRC) at the Massachusetts Institute of Technology since my student days, and this article relates to a project I built there recently.

In the days of steam locomotives, a feature of every engine-service facility was a turntable. The table was necessary to turn locomotives, which generally lacked the ability to operate efficiently in reverse, and it also allowed the construction of a compact facility for maintenance, the well-known roundhouse. Although only a few turntables remain in use in North America, they are such a classic feature of yard areas that many people want to include them on their railroads. This was the case at the Tech Model Railroad Club's main freight yard in Gifford City, with an additional local quirk: we pride ourselves on our technology, so any turntable we built had to be reliably automated. This in spite of the fact that in "the prototype" a turntable was never automatically aligned and the smaller ones were pushed around by muscle power!

An excellent scenic treatment of the turntable had been constructed by a departed member of the club, who left for the Hubble Space Telescope project. Unfortunately, the matching control mechanism had failed to stand the test of time. Given the club's minuscule budget and available materials (essentially limited to surplus telephone equipment and items scavenged from MIT labs), this was no surprise. I volunteered to build a new control system for the turntable, taking it as part of the challenge of the project to do the job with simple components, without resorting to extravagant technology.

It seemed obvious that the design would have to include a highly accurate servomechanism. To enable trains to run on and off the bridge without derailments, we concluded that errors in positioning of the rail ends should be no more than 0.010". From this figure, and the diameter of the table (17") we could calculate the accuracy of placement which would be needed. This worked out to about 5000 counts around the circle, or roughly 12 bits of resolution. It is certainly possible to build a servo with precision of this order, deriving feedback from an encoder, but we found suitable encoders to be fairly expensive, even on the surplus market. Also, incremental encoders do not provide any kind of absolute position indication, meaning that our turntable would have to index itself on startup, potentially requiring as much as a full rotation before any positioning could be done. Fortunately, a feature of the application suggested another approach.

Although we had to position the system to a high level of accuracy, we did not have to do so at any arbitrary point. We only had to locate either end of the bridge at each of 17 approach tracks, thus giving 34 stopping points. Because these 34 points were known in advance, it seemed that we might bring the bridge to roughly the correct point by some method which would not be used for final alignment; instead some other system would do the fine positioning, and hopefully this would not involve building separate hardware for every point. In other words, we might build a dual control system, having a coarse positioning mode with full range, and a fine positioning mode with narrow range, and this might let us use two inexpensive systems to do the work of a single prohibitively costly one.

Probably the least expensive--and certainly the most familiar--means of turning rotary position into an electrical signal is the humble potentiometer. Its failing is that typically it cannot be rotated a full 360 degrees, making it unsuitable for a system capable of continuous (albeit slow) rotation. There are special pots, however, made for devices of just this type. These have a wiper inside the case which can travel the full circle, although there has to be an electrical "dead zone" separating what would normally be the two ends of travel. But how could a servo work if the feedback element delivered no signal at some part of its travel? We then realized that if such a pot were connected across a voltage source with the wiper driving a high-impedance input, the addition of a simple pulldown resistor would prevent the unit's output from ever being an open circuit; instead, the dead zone would simply become an extension of the low-voltage end of the potentiometer's range. Although positioning would be impossible in this zone, it would be easy to place it in the largest space between tracks.

After a number of years of intensive programming experience on the 8051 microcontroller, my instinct was to use this ubiquitous component for the Gifford City turntable. Having processing power available made it possible to use a matrix-encoded keypad, similar to the one on a telephone, for user input. A 16-key pad was found, allowing us to use "A" and "B" buttons to designate the desired end of the bridge to align. A friend donated a 10-bit A-D converter chip (National AD1061) which came as a free sample, I found a continuous-rotation pot in a junk bin at my office, and the project was rolling.

The question of how to achieve fine positioning was next. The pot could not be relied on to give the precision (12 bits) we needed, but no commonly available components seemed suitable for the job either. Eventually I hit on a way to use common components--but to use them in an uncommon way. Optical interrupter switches are used everywhere as limit switches and object sensors. A light beam shines between an infrared LED and a phototransistor; an opaque object breaks the light beam, the impedance of the phototransistor goes from a few hundred to many thousand ohms, and a signal (essentially digital) is generated. Simple, cheap, easy to use, but not a device which can tell a system exactly where to stop. But suppose the interrupter were turned into an analog component--could it then tell us which of 3 zones our target was in? Namely "not far enough", "stop right there" and "too far"?

The phototransistor's output can be readily turned into a voltage if the device is wired as part of a voltage divider operating on a fixed voltage. We can then calculate a "transfer function", which in this case means the relationship of output voltage to the position of an opaque blade. I hoped that the transfer function of the optical interrupter (H13A1 or similar) would be steep but not absolutely digital in nature. As a test, I rigged up an opto-interrupter (another recruit from the junk bin) on an impromptu optical bench, namely a milling machine, and made a few measurements. What I found was that the output makes its full transition--from 10% to 90% is the usual criterion--over a range of about 0.015 inches. If we run the output of the voltage divider to two comparators, we can have the three-zone discrimination we need. Using 1/3 and 2/3 of the 5v power supply as set points, the width of the "stop here" zone is about 0.010 inches.

The continuous-rotation pot and the optical switch were the two transducers which were used for the design. The user enters a track number on the keypad, in the form of a number from 1-17 and "A" or "B" to designate the end of the turntable to align with the selected track. Each track's location was previously stored in a electrically erasable programmable read-only memory (EEPROM), and the value corresponding to the selected track is read from this and stored in a "destination" register. A relay cuts off track power to prevent a locomotive from moving while the turntable is running, and rotation begins.

The potentiometer is driven by the vertical shaft on which the turntable is mounted. Its output is fed to the A-D converter, which is triggered at a 500 hertz rate, delivering a 10-bit result. Normally the processor runs the system as a servo, attempting to reduce to zero the difference between the destination register and the present pot reading. But we want the turntable to rotate through the smallest possible angle to its destination (never more than 180 degrees); the mathematics which makes this happen is simple but a litle unexpected. If the number which represents the desired angle is subtracted from the corresponding "present location" angle, the "direction bit", telling the processor which way to run the motor, will be simply the highest-order bit of the result (bit 9 in a 10-bit subtraction), regardless of whether the result is positive or negative, and regardless of the carry bit status. Normally this direction bit is sent out to the electronics which drive the motor, but an exception occurs if the A-D reports that the pot slider is near to the "dead zone" (a very high, or very low, output). While this condition is being reported, a flag is set which prevents the system from reversing direction. The flag is cleared when the next track (potential stopping point) is reached. This feature prevents the servo from responding to the unpredictable readings near the pot's dead zone, as the wiper crosses from "very low" to "very high".

When the "present location" and "destination" quantities match closely, the system changes over to a fine positioning mode. A criterion of +/- 7 A-D counts is used to make this selection, corresponding to about 0.4 inches at the rail ends. When the turntable is within this zone, the drive motor is slowed down and the optical alignment system becomes active. While the servo is using the coarse mode, the processor ignores the output of the optical switch exept for using it to turn off the "don't reverse" flag described above. In fine positioning mode, the two comparators driven by the switch's output are read by the processor. Each track has a corresponding metal blade, an aluminum strip about 0.7 inches wide, which projects from a disk mounted on the vertical drive shaft. Each of these is positioned carefully so that at the desired stopping point for its track, the blade partially cuts the light beam; thus only one optical switch is needed. When the two comparators indicate the "stop here" zone, the motor is stopped and track power is enabled via relays to the selected track and to the turntable itself. Obviously, this system does not form a linear servo, but the motion is slow enough that hunting around the desired stopping point is minimal--never more than 2 reversals and often none at all. If anyone objects, we can claim that "sometimes the operator doesn't hit the right spot first time!"

The motor drives the turntable through a clutch, and it is possible to push it off its stopped location by hand. The servo remains active at all times, however, so if the turntable is moved in this way, the motor will restart and bring it back, first at a high speed and then slowly as the destination is approached. This is very satisfying to watch in action.


The hardware to produce highly accurate positioning does not need to be extremely costly. Optical switch units, normally used in a purely digital mode, can provide feedback for highly repeatable positioning at discrete locations, with discrimination between these locations performed by a coarse position sensor such as a potentiometer. The use of a microcontroller allows a servo to be constructed which can determine which of the two sensing methods should be used for first coarse, and then fine, positioning.