PicBasic Pro, PicBasic, BASIC Stamp, Microchip PIC, 8051, and Remote Control Projects

-- Infrared Object Detection & IR Applications --

This project shows how to use simple infrared components for object detection, distance measurements, and several other applications. The unique aspect of this project is its adaptive ability to adjust the power-output levels of the infrared LED to compensate for variations in ambient lighting conditions.

Ambient Light Effects On Infrared Applications

The infrared detector module used for this project was the 38kHz Panasonic PNA4602M. In dark lighting conditions the detector module increases the gain of the internal amplifier circuit. In bright lighting conditions, the internal gain is reduced. This reduction in gain is an attempt to reduce the possibility of false output pulses on the detectors output stage caused by light sources not associated with the intended data signal. The big effect of high ambient light levels is reduction of the overall operating distance of the IR LED, and decreased IR detector module sensitivity.

Modulation

Most common infrared detectors require the data signal to arrive in the form of a modulated signal. The modulation component is referred to as the carrier. One stage of the detectors internal circuitry consists of a band-pass filter. The job of the band-pass filter is to reject light energy or un-wanted data signals not modulated at the band-pass filter center frequency.

This helps keep the output stage of the detector from emitting false-pulses or dark-bits, and is a simple method for discriminating between ambient light or noise, and the intended data signal.

For this application we're not concerned with an actual data signal carrying useful information. All we need is to sample the logic output at the detectors output stage. To produce a change in the detectors output stage all we need is:

bullet

An infrared LED

bullet

A means of modulating [turning ON & OFF] the infrared LED at the carrier frequency

The output stage of this IR detector includes an internal pull-up resistor. In the resting state [no data or carrier being received] this pull-up resistor holds the IR detector modules output-pin at logic 1. The logic value [1 or 0] on the output-pin of the infrared detector can be sampled by the PIC microcontroller using the simple circuit, and code sample shown below.


Infrared Detector Circuit

The output of the IR detector module can connect to any available I/O-pin on the PIC. For this example we'll be using PortB, bit #7. Here is the code to detect a valid carrier being received by the IR detector module, and light an LED connected to PortB, bit #0. The LED in this case is used for visual verification that the infrared carrier or signal is being received by the IR detector module.

IF PortB.7 = 0 THEN  ' If a logic 0 is detected, then
    HIGH PortB.0       ' Turn ON the LED
ELSE                      ' If not, then
    LOW PortB.0        ' Turn OFF the LED
ENDIF

The output of the detector module is held at logic 1 by the internal pull-up resistor until infrared energy modulated at the band-pass frequency [38kHz] strikes the face of the detector module.

Once the modulated signal is detected, the output of the detector goes to logic 0 [ground], and indicates to the PIC that infrared energy modulated at the carrier frequency is being received by the detector module.

For this application, we're only interested in knowing when the IR energy from the infrared LED is reflected back onto the face of the IR detector module. The code example above causes the LED connected to PortB, bit #0 to turn ON when the reflected IR energy strikes the face of the detector causing the detectors output-pin to transition from logic 1 to 0. Below is the simple LED circuit used for the visual indicator.


LED Visual Indicator Circuit

When PortB.7 = 0 [38kHz carrier is detected], the LED is turned ON. When no valid carrier is detected, the LED is turned OFF. We now have a way to detect objects in the path of the IR LED close enough to reflect the IR energy back onto the detector, and an indicator to verify detection. You could replace the LED with a small relay if necessary. Pretty similar to those mysterious toilets that automatically flush when you're walking away..;o]

The 10uF capacitor used in the detector circuit helps to stabilize the output. When you build this circuit, you can remove the 10uF capacitor while the circuit is in operation, and see the effects. Weak reflections from the infrared LED, and other sources will cause very small fluctuations on the output stage of the detector module causing the indicator LED to flash quickly as an object moves closer to the detector module.

The capacitor acts as a filter, and ensures that only a strong reflected infrared signal produces the desired output of logic 0 by absorbing the small negative noise spikes. Much the same as power supply filter capacitors you see in most simple power supply designs, and it's quite effective. Without the 10uF capacitor, you'll notice very fast ON / OFF fluctuations of the LED connected to PortB.0. Experiment a little to see the effects first-hand.

The Infrared LED

The infrared LED used for this project is the Lumex OED-EL-8L. This IR LED has a 40° beam-angle, and is capable of emitting 180mW of IR energy at a peak wave-length of 940nm. Below is the circuit for the infrared LED driver.
 


Infrared LED Circuit

The 40° beam-angle of the OED-EL-8L infrared LED can narrowed by placing a small piece of heat-shrink tubing over the LED lens. For this project I chose to narrow the 40° beam-angle for more precise distance measurements, and to eliminate side target signal reflections.

The black heat-shrink tubing covering the LED lens keeps IR energy from being emitted side-ways, and affecting forward measurements. The heat-shrink on the LED leads keeps IR energy from exiting the rear of the lens element. Infrared energy is projected forward, and then reflected back onto the surface of the detector module positioned directly behind the infrared emitter.

The actual placement of the heat-shrink is covering more of the LED, but for the purpose of illustration - I have shown the LED lens protruding slightly from the heat-shrink. You can adjust the heat-shrink to cover more or less as necessary, but try to eliminate IR emissions from the rear, and side of the LED. Effectively measuring distances, and detecting objects directly in front of the emitter will be more precise with a narrow beam-angle.

The real trick, and big difference between this project and many other simple infrared detection circuits is the ability to control the infrared output power of the LED. By controlling the output power we can adapt to changing environmental conditions, and ambient light sources or noise.

As discussed previously, we know the infrared detector modules internal circuitry will attempt to increase or decrease the gain of the detection circuit proportional to the amount of ambient light striking the surface of the detector. High levels of ambient light will force the internal automatic gain-control to reduce the gain of the detection amplifier, thereby requiring a much stronger infrared signal striking the surface of the IR detector for valid detection.

Increased gain due to lower ambient light levels or dark conditions means the IR detector is operating with increased sensitivity, and hence will detect a much weaker IR signal than in a disturbed or noisy environment.

Here's the trick

The PIC microcontroller has an onboard "hardware" PWM module. This PWM module offers true multi-tasking operation, and provides an excellent way to modulate the infrared LED at the required band-pass frequency of the detector module. Multi-tasking is the ability to process multiple tasks simultaneously [at exactly the same time].

Once we have configured the PIC registers to produce our required PWM frequency, and turned the PWM output ON, our code can go about other tasks while the PIC PWM hardware continues to emit the carrier frequency without further intervention. Turn it ON, and walk away.

This multi-tasking ability allows us to modulate the IR LED at the required carrier frequency while performing other tasks such as:

bullet

Sample the output-pin of the IR detector [without stopping the PWM carrier]

bullet

Take real-time ambient light readings with a CDS photocell

bullet

Adjust the PWM duty-cycle to control IR output power

Using the information obtained from readings of ambient light levels, we can adjust the output power of the IR LED by varying the duty-cycle of the PWM signal.

This adaptive technique allows us to compensate for changing environmental conditions, and adapt as necessary to maintain a constant infrared energy output required for reliable "reflective" IR sensing under variable lighting conditions. Measuring ambient light levels is simple with the PicBasic compiler POT command. Below is the simple circuit used for measuring light levels.

The POT command charges the capacitor through the CDS photocell, and then measures the time it takes for the capacitor to discharge. The resulting value is then used to adjust the IR LED output power to compensate for changing light levels present on the infrared detector module. Note: You can also us the RCTIME command with minor adjustments to the light-sensing circuit.

Duty-Cycle vs. Output Power

The duty-cycle of the PWM signal can easily be modified by placing different values into the CCPR1L register. The high-time of the duty cycle will turn ON the LED connected to PortC.2. The low cycle will turn the LED OFF.

The Peak-Current through the infrared LED will be:

= the peak pulsed current through the infrared LED.
= the operating voltage. Here it is +5 VDC.
= the voltage drop across the infrared LED. For the OED-EL-8L this is 1.7V.
= the current-limiting series resistor used between the PortC.2 I/O-pin, and the infrared LED.

The result is 3.3mA peak pulsed current as shown below:

With a duty-cycle of 100%, the maximum peak current through the infrared LED would be approximately 3.3mA. In fact, if you connect the anode of the IR LED directly to Vcc instead of PortC.2, you would have a steady current of 3.3mA through the LED. However, we're going to adjust the high portion of the duty-cycle to vary the power delivered to the LED.

You may have already noticed that 3.3mA seems incredibly small considering that most LED's operate at 10-20mA. In fact, the OED-EL-8L LED can handle up to 60mA continuous, and much more when pulsed, but this application does not require long-range operation, and infrared isn't visible - so we're not going to be seeing emitted IR light from the LED.

For detecting objects at distances up to 6" or more, the OED-EL-8L provides sufficient output power operating at a mere 264-660uA. This is excellent for battery powered robotics applications, and just about anything with low power requirements. It's not always necessary to use the standard 10-20mA ratings used with most common LED circuits for all applications.

For even further power-savings, supply power to the infrared detector module using a single I/O-pin, and turn OFF the PWM module when not being used. I won't go into these details in this article, but it's good to know if you're planning a battery operated robot using these simple circuits & techniques.

The average current delivered to the infrared LED is important. Going over the average current will, in most cases, degrade or completely destroy the LED. This application stays well below the average current rating of the OED-EL-8L. Here's how:


= average LED current
= peak pulsed LED current
= duty factor or PWM [duty cycle]

With a maximum peak current of 3.3mA, and a duty cycle varying from 8-20%, the average current range for the infrared LED used in this application is: 3.3mA x 20% = 660uA MAX, and 3.3mA x 8% = 264uA MIN.

The Code:

'****************************************************************
'*  Name    : IR-DETECT.BAS                                     *
'*  Author  : Bruce Reynolds                                    *
'*  Date    : 10/14/2001                                        *
'*  Notes   : Infrared Object Detection using hardware PWM      *
'*          : with auto PWM / IR-LED power adjustment           *
'****************************************************************
DEFINE LOADER_USED 1 ' Use only with boot-loader program
DEFINE OSC 4        ' Default 4MHz
ADCON1 = 7          ' Port A = digital I/O
CDS  VAR BYTE       ' CDS photo-cell reading
TRISC.2 = 0	    ' Set Port C.2 (CCP1) to output for PWM drive
TRISB = %10000000   ' Set Port B.0,1,2,3,4,5,6 to outputs PortB.7 = input
TRISA.0 = 1         ' Set Port A pin 0 to input
CCP1CON = %00001100 ' Set CCP1 to PWM 
INTCON.7 = 0        ' Turn interrupts OFF
T2CON = %00000100   ' Turn on Timer2, Prescale = 1
OPTION_REG.7 = 0    ' Turn ON PortB pull-ups
PR2 = 25	    ' Set PR2 for 38.4kHz PWM [26uS period]

begin:
    POT PORTA.0,127,CDS ' Get initial light reading
    SELECT CASE CDS     ' Place value into variable CDS
    CASE IS >= 49       ' If light over 48, duty = 8% [dark]
        CCPR1L = 2      ' Duty cycle = 8% approx
    CASE IS <= 10       ' If light below or = 10, duty = 20% [bright]
        CCPR1L = 5      ' Duty cycle = 20% approx
    END SELECT          ' End case select
    IF PortB.7 = 0 THEN
        PortB.0 = 1
    ELSE
        PortB.0 = 0
    ENDIF
    GOTO begin          ' Loop continuously

The PIC used for this application was the PIC16F877-20/P. The project was developed on the new FLASH-Lab 77 board. You can use any PIC that has the onboard hardware PWM feature. The PIC16F627 or 16F628 also have the hardware PWM peripheral, and you can find more details on using the hardware PWM in our online PicBasic compiler project section HERE. Further details on configuring & using the onboard hardware PWM with the PIC16F877 can be found HERE.

I couldn't see re-hashing previous articles here to explain PWM operation, so I included the links above to individual projects here with much more detail on using the PIC hardware PWM.

Notes: As you will see once you start experimenting with this project, it has many various uses. You will also notice that you may need to adjust a few of the variables such as the scale factor used with the POT command, perhaps the PWM frequency for different IR detectors, and maybe the series resistor to compensate for different IR LEDs.

This project was designed & tested using a 40W incandescent light approximately 10" above the CDS photocell during high ambient light measurements. Dark ambient light measurements were taken with the 40W light turned OFF, under average lighting conditions, shielding the CDS photocell from ambient light with my hands. You'll need to experiment with the POT scale value to tweak the application to suit your specific environmental & lighting conditions.

Results under the above conditions, with the sample code shown:

Detection Distance: Precise detection of objects up to a maximum of 6-inches in front of the infrared LED/detector module.
Variable Light: Maximum detection distance of 6-inches is maintained under direct exposure to the 40W source, and in darkness.
Bright Ambient: Duty-cycle is approximately 20%, LED current approx 660uA.
Dark Ambient: Duty-cycle is approximately 8%, LED current approx 264uA.

Note: You won't get reliable distance measurements with this method similar to sonar or IR modules specifically designed for distance or angle applications, but it does work well enough for home hobby & simple robotics applications, and it's fun to play with.

With a little experimentation you can build a system to measure short distances by varying the IR LED output power. Combine this with an SRF04 sonar range finder for even more fun.

Until the next project - have fun - and don't blow anything up...;o]

Regards,

-Bruce


Back to the PicBasic Projects Section

Copyright © 1999-2008 Reynolds Electronics

| Contact Information |