Search This Blog

Sunday, January 20, 2013

IR Distance Sensing Revisited ( and Corrected )

    So, I revisited reading distance measurements with an IR LED and a phototransistor.  It became immediately evident that I had not taken the time to fully understand what I was working with.  This write-up is an attempt to more clearly analyze and explain the hardware and circuit wiring.

Experiment & Results:
   Here in the United States, we have an electronics shop called Radio Shack.  They sell a pair of components: an IR LED and a phototransistor.  The LED has a forward voltage of 1.3-1.7V.  I used the lowest value for safety.  The forward current is 150mA.  So, at 5V, the LED only needs a 16 Ohm resistor.  At 3.3, it needs less than 5 Ohms.  The lowest value I had on hand was 33 Ohms, so that is what I used for these experiments.  The phototransistor is what I really messed up on.  The schematic on the package clearly indicates that the transistor is an NPN by the arrow on the emitter pointing out.  This is consistent with web searches I've done.  They indicate that all phototransistors since the 1960's are NPN.  The pin diagram shows that the collector is on the flat side of the LED casing.  Here's the problem, wired up with the collector (flat side) to positive just doesn't work!

    If I put the resistor to ground and the analog sense pin on what I thought was the emitter (non-flat side), the reading didn't change.  If I put the resistor to Vcc and the sense pin on what I thought was the collector (flat side), I got inconsistent readings.  If I treat the flat-side as the emitter, things change drastically.

    With the flat side ( now calling it the emitter ) hooked to ground and the non-flat side ( now calling the collector ) hooked through a 47k resistor to Vcc, the sense pin on the collector behaved consistently.  The values were large ( 800 - 990 ) and got smaller as the distance to the object decreased.  With the resistor and sense pin moved to the flat side ( emitter ), the values were also consistent.  They were low (20-200) and increased as the object grew closer.

    The values were measured with a 10 bit A/D pin on a Pic16F88.  Everything was kept the same other than the variations of the positive and negative connections, the resistor, and the location of the sensor pin wire on the phototransistor.

Analysis:
    So, from all of this, either the Radio Shack phototransistor is a PNP transistor, or the pin diagram mis-labels the collector and emitter.  That, or I totally screwed up how things should be wired.  Electronics forums on the web since certain that all phototransistors since the mid-60s have been NPN.  I don't think Radio Shacks transistors are that old, so I discard the possibility that they are PNP.  I based my wiring off of the line sensor from Pololu.  Those guys graduated from MIT.  I believe they know what they are doing.  That leaves the fact that Radio Shack labelled the pins incorrectly on the package.

Conclusion:
    Go with what works.  In this case, treat the flat side of the phototransistor as the emitter of an NPN phototransistor.  For my purposes, putting the resistor and sensor pin on the emitter works best.  The values jump quite a bit when the object detected is withing 6 cm.  This makes it easy to set an "danger perimeter alarm" for distance sensing robots.

Update:
    I tested this with another phototransistor.  This one is a standard QRD1114, typically used for line sensing.  It has a very short range.  Pin one is the collector and pin two the emitter.  With the collector hooked to Vcc via the 47k resistor and the emitter to ground, the sensor pin on the collector side read values that increased as the distance grew against white paper.  Against a black line at any distance, they read their maximum value of 50.  This, and all the above experiments, were done at 5V.  The values and the range were vastly smaller, from 15-50.  It seems to confirm what I theorized above.  Results were consistent with all four arrangements above.  Treated as a PNP, the values were inconsistent or unchanging.