IR Sensor Project

Revised 05-23-07

The objective of this project is to design, construct and program a controller that will activate a relay whenever a train is detected by a reflective IR sensor.  The length of time that the relay remains closed can be adjusted from 0 to 20+ seconds.

An indicator LED will show when the relay is closed. 

A jumper can be set to put the unit into a test mode that does not activate the relay.  This save wear on the relay when adjusting or aligning the sensor.

 

Schematic:

Click on this thumbnail to see the schematic

 

Program Logic:

When the sensor sees reflected IR the control circuit waits to see if there are a number of consecutive "hits" before it confirms a detected train.  When that happens the relay is closed and stays closed until the time set by the potentiometer has passed.

It the test jumper is closed by shorting its pins the relay is removed from the circuit and the light alone indicates that something has been detected.

Sensor

The IR sensor is at the right end of the board.  The IR emitter is in the black tube at the right.

Bottom view

 

Sensor Mount

The sensor can be mounted on standard 1/2" CPVC fittings.  Here is one way to mount it.  The additional fittings allow the sensor head to be tilted and rotated so that it will only detect a train on a nearby track.

 

Control Board

The microcontroller is in the lower left.  The relay is the white object top center.  The delay adjustment potentiometer is the black device to the right of the relay.  The sensor unit plugs into the board on the left.  The four pins at the bottom of the board are for programming.  The blue and white wire at the bottom are for testing.

The test jumper is show installed in this photo.  It is the black item just in front of the white relay.  Remove it to activate the relay.

The sensor plug must be inserted as in the photo with the orange wire to the right.  The heat shrink tape wrap is marked with a silver line as seen below.  Please make sure this plug is properly inserted before applying power as inserting it incorrectly can destroy the sensor.

 

 

Relay Connection

The DPDT relay contacts are accessed by the two sets of three leads coming from below the board.  The brown wire is common.  The yellow is normally open and the grey is normally closed.

Time Delay

The potentiometer varies the time that the relay is held in from 0 seconds to 25.5 seconds.  If the pot is turned all the way clockwise there is no delay.  All the way counter clockwise is 25.5 seconds.  The detent in the center is around 12.7 seconds.

Software

'IR sensor system - 5-22-07
'v 1.0 has all components working well

Include "modedefs.bas"
OSCCON = %01100111 'MUST BE USED to set clock speed =$67
cmcon=7 'allows you to use pins as digital rather than analog
ansel=0 'allows you to use pins as digital rather than analog
@ DEVICE PIC16F88, INTRC_OSC_NOCLKOUT, WDT_OFF, LVP_OFF, PWRT_ON, PROTECT_ON, BOD_ON
DEFINE OSC 8
OSCCON = $70
' var porta.0 'pin 17
'ADCIN 1 var porta.1 'pin 18
Serial_out var porta.2 'pin 1
SW1 var porta.3 'pin 2
' var porta.4 'pin 3
' var porta.5 'pin 4
' var porta.6 'pin 15
Sounder var porta.7 'pin 16

'IR_LED var portb.0 'pin 6
IR_LED con 2 'pin 6
Time_Adjust_Pot var portb.1 'pin 7
SW2 var portb.2 'pin 8
Relay var portb.3 'pin 9
Lamp1 var portb.4 'pin 10
Sensor var portb.5 'pin 11
' var portb.6 'pin 12
' var portb.7 'pin 13
switchflag var byte 'keeps track of which sensor was hit
flashcount var word 'used to keep flashing while end check is done
'irLED con 2 'gives 38 KHz on pin #5 on chip
temp var word
temp2 var byte
hits var word
'pgmdelay con 100
serialin var portb.0
trisb=%00100110
trisa=%00001010
temp=0
hits=0

hpwm IR_LED, 127,38000
serout serial_out,n9600,[10,13,10,13,"Very Top v2.0",10,13,10,13,10,13]
pause 500
high sounder:low relay
low lamp1:

top:

adcin 1, temp 'pin 18
temp=temp*10

if sensor=1 then
for temp2=1 to 100
if sensor =0 then
' serout serial_out, n9600,[ "Front Sensor Glitch ",#temp2,10,13]
goto top:
endif
next temp2
goto SawSensor
endif
if sensor=0 then low lamp1:low relay
goto top

SawSensor:
adcin 1, temp 'pin 18
temp=temp*10
hits=hits+1
serout serial_out, n9600,[ "Sensor Seen ",#temp, " HITS= ", #hits,10,13]
if sw1=0 then
high relay
high lamp1
pause temp
endif
high lamp1
goto top: