Encoder Front Page
SRS Home | Front Page | Monthly Issue | Index
Search WWW Search seattlerobotics.org

Smarter Infrared Proximity Detection

By Rich Kappmeier dr_awkward@earthlink.net

With this article I hope to describe an infrared collision avoidance system that works well even when tooling around on a sunny day. Through much trial and error and a few happy accidents I have pieced together this same system I use on my current, and first, robotic effort named, Mr. Mustard.

Aside from a minuscule amount of electronics knowledge and a bit of mechanical knowledge (I owned a ’62 VW Bus, ‘nuf said?) I had never attempted a project like this before.

After about 8 months I had my chassis built, my tracked drive system up and running, and a reliable set of feelers attached to my Basic Stamp 2sx by Parallax. I felt it was high time I incorporated some infrared collision avoidance into Mr. Mustard.

I was having one hell of a time getting the SHARP GP1U26X infrared detectors to work consistently. I could get a signal but it was noisy and unreliable.

Having spent a few days getting intimate with the LM555 timer chip while I was building the 40khz emitters, I hit upon an idea! Maybe I could use a 555 in a one-shot mode as a sort of signal filter? I set it up so the noise and the garbage wouldn't be enough to charge the timing capacitor but once a good signal occurred the cap would charge, the 555 would send a pulse, and the BS2sx would know that there was something "out there."

This scheme worked very well until Mr. Mustard tried to navigate beneath a mess of fluorescent lights, namely the ones we have hanging from the ceiling at my work. In this sort of environment Mustard suddenly acquired an acute case of digital epilepsy.

I tried to fix this problem for a few days, I made some headway, but not as much as I’d hoped, and I finally said "good enough." I moved on to some servo experiments for Mustards sonar "head".

"Good enough" lasted about a month. I read an article on the Seattle Robotics Society archive page on Fuzzy Logic by Steven D. Kaehler. It gave me an idea. Perhaps I could adapt my current IR detector system to utilize a simple form of Fuzzy Logic.

Fuzzy Logic was conceived in 1965 by computer science Professor Lotfi Zadeh, of the University of California at Berkley, to approximate more organic control of machines. In Fuzzy Logic an unreliable, buggy system can be made much more reliable by applying an algorithm, or set of rules, to it. After all, a living organism does not need exact information about an obstacle to avoid it successfully. A creature would only need approximate data about the objects size, its shape, its distance, and its probable make-up. Knowing these approximates would enable the organism to take appropriate action, be it climbing, circumnavigating, or destroying the obstacle.

Mr. Mustards GP1U26X / 555 combination, while in a "noisy" environment like my office area, would get between 30 and 50 erroneous detection’s a second. This really only amounts to a fraction of the total possible detections, about 625 for this period of time, but it is enough to cause problems with the ‘bot. Logically speaking, the sensors, or "pickups," should put out a nice unbroken chain of pulses if there is an object in view. Conversely, noise would mean intermittent pulses here and there, spotty pickup response at best, but enough to trigger the aforementioned digital epilepsy.

Could I program an algorithm that could tell the difference between intermittent detection errors and the clean unbroken string of a reflective object?

Yes I could.

What I needed to do was write a short program that would watch the input pin from the IR pickup and count the total number of pulses for a specific amount of time. As an example, let’s say that the GP1U26X / 555 combination pickup, when triggered, has an ON time of about 1.1 ms and an OFF time of 0.5 ms, yielding a 1.6 ms total period (see below). I chose a maximum 20 hit period of watch time for the algorithm. 20 hits at 1.6 ms each would yield a total watch period of 32 ms. I found that in a noisy environment, there are usually between 3 and 6 erroneous pulses within a similar 32 ms period. I then decided that a reasonable threshold would be 15 pulses out of the possible 20. So, 16 or more pulses within the 32ms period would cause an action, like a turn, to be taken.

To implement this simple one rule algorithm I used the BS2 Count command on the sensor-input pin for a 32 ms period. Count would watch the pin for 32 ms, count the pulses, and store that number in a variable named "HITS". The program would then compare HITS to the constant named "THRESHOLD." If HITS is less than THRESHOLD the robot ignores the erroneous, noise triggered pulses and keeps on rolling along. If HITS is greater than THRESHOLD the program executes a turn, as in the graphic below.


This system is not true Fuzzy Logic because it deals with defined sets of numbers, those numbers that are less than or equal to 15 and those numbers that are greater than or equal to 16. In true Fuzzy Logic data is compared to previously collected data over a period of time. Rapid changes or great fluctuations in the data trigger decisions by the control mechanism. The example program below could be changed to compare a previous hit count to the current hit count. If the difference were great enough in either direction a preprogrammed action would be taken. For a more in depth explanation of Fuzzy Logic see Fuzzy Logic-An Introduction by Steven D. Kaehler on the Seattle Robotics Society web site.

The range for the system has proven to be about 16"(off-white) to 24"(bright white) for large indoor targets, like walls. Unfortunately, as the targets surface area gets smaller so does the range. Like most other infrared sensors, black can be a problem for the system. My black nylon backpack is detectable at 16" but the black covering on my bass amplifier is invisible.

Direct sunlight, right into the optical element, is a problem as well. Such a tremendous volume of IR just overwhelms the phototransistor and blots out everything else. The error filtration still works outdoors but sunlight tends to make the IR signal mushy and hard to read making only short-range (4" to 8") detection’s possible.

The sample program below will work for both the BS2 SX (as written) and standard (with TIME constant set to 32).

‘{$stamp bs2sx} ‘designates the SX version, delete this line if using a BS2 standard


THRESHOLD con 15 ‘the "good" detection threshold is set to 15

TIME con 85 ‘for a BS2 standard this would be set to 32

HITS var word ‘variable storage space for HITS

‘BS2 I/O

Input 0 ‘designate pin 0 as input



pause 1000 ‘takes a reading every second

count 0,TIME, HITS ‘counts pulses on pin 0 for TIME and stores number in HITS

if HITS > THRESHOLD then Detect ‘branch to Detect of HITS is > THRESHOLD

debug "No object in field of view.", cr

debug "Pulses counted =", dec HITS, cr

goto start


debug "OBJECT IN VIEW!", cr

debug "Pulses counted =", dec HITS, cr, bell

goto Start

The schematics below are the IR pickup and emitter used with the program above


The pickup and emitter should be placed roughly side by side with their optical elements facing the same direction, close to parallel. The photo below is of the front end of Mr. Mustard. There are 3 LED’s connected to the emitter; one is facing about 30 degrees to the right of center, one is facing down the centerline, and the last is 30 degrees to the left of center. The pickups are facing roughly 30 degrees to the left and right of the centerline, as well. This configuration lets the LED light paint a significant portion of the area in front of the robot and also allows each of the pickup areas to overlap a bit. The configuration is not vital so you should feel free to play around.



After downloading the test program to the Basic Stamp 2sx Microcontroller the debug screen should appear. About every second the program will loop and tell you if there is a valid detection or not and how many pulses were counted.

You will need to adjust the resistance on potentiometer R8 of the emitter circuit. Make sure the area in front of the system is clear out to at least 36". If at this point you get a constant "OBJECT IN VIEW!" message adjust R8 until it just changes to "No objects in field of view." This should be the maximum range capability of the system. If you get a constant "No objects in field of view," adjust R8 until you see an "OBJECT IN VIEW!" message and then turn it back a bit until "No objects in field of view," reappears.

The system can be tested by just holding your hand in front of the LED/Pickup combination. Hold your hand about 8" from the system and watch the debug screen. You should see "OBJECT IN VIEW!" and under that it should show you "Pulses counted =," and then the number of pulses counted. You can also shoot a TV remote into the pickup. When I do this my system shows between 1 and 4 pulses detected.


If you find that these circuits are not working together initially, there are a few things you can try.

Check your layout for errors; make sure everything is wired correctly and all the components are oriented properly. If the construction checks out OK shoot a TV remote into the pickup as described above. This should trigger some detection pulses. If you get a detection from the remote but no detection while using the emitter, view the LED’s through a digital camera, one with an LCD screen on the back. It could be either a still camera or a movie camera screen you look through but you should see the LED’s turn a light grayish color when they are on. Also, it helps to be in a darkened room while viewing the LED’s. If you can see that the LED’s are lit or if detection is spotty, make sure the infrared signal is set up like it should be. Be sure that the modulation frequency is correct, about 833 Hz, or a total period of 1.2 milliseconds. This can be measured at pin 5 ("out") on the NE556 of the emitter circuit. The period for a 40kHz signal is about 25 microseconds. This can be measured after jumping a 5 volt signal to pin 10 ("reset") of the NE556 of the emitter circuit. Doing this will pull the reset line high and cancel the modulation frequency, giving you a clean wave to view at the output, pin 9 of the NE556.

I hope this system helps make your robot’s navigation a bit more reliable. Good Luck!

IR Pickup Parts List IR Emitter Parts List
R1 10k R1 8k
C1 .1uF ceramic R2 1k Pot
C2 .01uF ceramic R3 3k
C3 .01uF ceramic R4 1k Pot
C4 .1uF ceramic R5 20k
U1 GP1U26X R6 10k Pot
U2 LM555 R7 5.7k
Connector Builders Preference R8 1k Pot
C1 .01uF ceramic
C2 .01uF ceramic
C3 .1uF ceramic
C4 .001uF ceramic
C5 .01uF ceramic
D 1-3 IR Led
U1 LM555