Wednesday, February 27, 2013

PIC 16F628 and PNA4602 IR detectors

Each pin on the PIC 16F628 can only source 25mA.  To drive the IR leds I needed to use a transistor.  I selected a BC549 as it was listed as a signal transistor.  The voltage drop across each led was approx 1.2V so I should be able to drive 3 leds on a 5V line with a transistor.  Since I needed at least 5 IR leds (2 front, 1 left, 1 right, 1 back) I would need two transistors.  Next I had to calculate the minimum resistor value I could have that wouldn't result in more than 50mA being put through each led (the maximum an led could handle).  Initially I went with a very conservative resistor value of 1K. Doing number I found the minimum resistance as below:

2 IR leds

v/I = R
(5-(1.2*2)) / 50mA = R
52ohm = R


3 IR leds

v/I = R
(5-(1.2*3)) / 50mA = R
28ohm = R

For my initial tests I used just a single transistor with 3 IR leds connected.  Next was connecting up a single IR detector to the PIC.  I initially wanted to trial just one detector and a few IR leds in order to do range tests.  More on that later.

I used some PNA4602 detectors (the TSOP4038 seems more common now) which I had purchased from Solarbotics some time ago.  Yes, I was buying parts years before I got around to using them.  As widely recommended I put a 0.1uF capacitor across the power rail close to the detector.  The output of the detector went to pin RB4, one of the interrupt capable pins.  I setup in the ISR section that when the detector registered a hit, to turn left (thus changing one of the bicolor leds I've substituted for motors during testing).  Due to the blinking led loop I had in the main loop I could see when the detector (and thus ISR) was triggered.  Ie when I was in the ISR section the blinking led wouldn't be blinking.  Thus I had a double visual check that the detector was working as one of the dual colour leds I was using instead of motors would change color and in addition the blinking status led would stop blinking.  Now for range tests.

On my breadboard the IR leds and IR detector were approx a hand length apart and both pointing up at the ceiling.  Also I replaced the initial 1K resistor with my Variable resister box so I could adjust the power sent to the IR leds and thus the detector range.  So holding a ruler upright I slowly lowered my hand down the ruler.  I was looking for two things.  When the blinking led (from the main loop) stopped blinking, which would show the ISR had been entered.  The second was when the dual color led changed color from red to a solid green (not just a flickering green).  The results are below.

ResistanceDetectFull-Green
930ohm35cm18cm
530ohm43cm30cm
140ohm60cm+45cm

In the final circuit I plan to use a 1K variable resistor with the minimum safe resistor in series so I can tune on the fly.  The two front IR leds will be on one variable resistor, the left/right/rear IR leds will be on a second variable resistor.