Picaxe 08M2+ PIR and Relay put together for a automatic light switch

Greyling

New Member
Here is a project I made to turn on my basement light when someone enters the laundry room. I chose the PICAXE 08M2 for this because it was just the right little chip for the job. When I first started this project I thought it would be a straight forward "easy" thing to do, well, it was not! There were little issues like the relay closing between sessions and annoying little timeing problems in my first attempt. I ended up throwing out the that plan and decided to give the FlowCharting method a try. After a few attempts I finnaly came up with this diagram.
View attachment 18531


I used two tasks here. One that just checks the Passive Infra-Red sensor over and over. And one task that acts like a control center. That is, when the first loop is triggered , it decides if it should turn on the light based on some criteria. One tricky thing I ad to overcome was if the system had already been triggered and a person as still in the room should the timer be reset or simply run out and then re-evaluate the situation. Designing something like this is not as easy as it looks at first. There are many considerations to take in to account. This flowchart seems to work OK so I decided to turn it into code. Here it is....
'
-----------------------------------------------------------------------------------------
symbol varA = b0
symbol varB = b1
symbol varC = b2
symbol varD = b3
symbol varE = b4
symbol varF = b5
symbol varG = b6
symbol varH = b7
symbol varI = b8
symbol varJ = b9
symbol varK = b10
symbol varL = b11
symbol varM = b12
symbol varN = b13
symbol varO = b14
symbol varP = b15
symbol varQ = b16
symbol varR = b17
symbol varS = b18
symbol varT = b19
symbol varU = b20
symbol varV = b21
symbol varTEMPBYTE1 = b22
symbol varTEMPBYTE2 = b23
symbol varTEMPBYTE3 = b24
symbol varTEMPBYTE4 = b25
symbol varTEMPBYTE5 = b26
symbol varTEMPBYTE6 = b27
symbol varTEMPWORD1 = w11
symbol varTEMPWORD2 = w12
symbol varTEMPWORD3 = w13
}


main:
Cell_7_2:
let dirsC = 23

Cell_7_3:
pause 1000 'Sanity delay, give time to initialize the PIR

Cell_7_5: ' Start searching for movement
if pinC.3=1 then

goto Cell_7_7
end if
Cell_7_9: ' No movement = LED off (RED), keep searching
low C.1
high C.2
goto Cell_7_5

Cell_7_7: ' Movement = LED ON (GRN), keep searching.
high C.1
low C.2
goto Cell_7_5

Cell_4_1:
start1:

Cell_4_2: 'Set the run timer for 60 secs in varD
let varD = 60
Cell_4_4: ' Watch for movement, if true then...
if pinC.3=1 then

goto Cell_4_5
end if
goto Cell_4_4

Cell_4_5: ' ...Set the RUN timer to 0 secs.
let time = 0 '
Cell_4_6: ' Start the RUN timer
enabletime
Cell_4_8: 'Run the sub that turns on relay and the then come back.
gosub prc_LightsON
Cell_4_10: ' Check the RUN timer, if NOT expired, keep checking until true.
if time >= varD then
goto Cell_4_11
end if
goto Cell_4_10

Cell_4_11: 'RESET the RUN timer to 0
let time = 0
Cell_4_12: ' Check once more for movement, if not, shut off lights, keep looking.
if pinC.3=1 then

goto Cell_4_5
end if
Cell_7_12:
gosub prc_LightsOFF
goto Cell_4_4

Cell_1_9: ' ********* PROCEDURES ****************************************
prc_LightsOFF:
Cell_1_11: ' LIGHTS OFF
low C.4
Cell_1_12:
return
Cell_1_4: ' LIGHTS ON
prc_LightsON:
Cell_1_6:
high C.4
Cell_1_7:
return

#no_data 'reduce download time
-------------------------------------------------------------------------------------------------

Now that I had the code done, I needed to build a schematic for the circuit. I used a FREE program from DesignSpark.com The software can make schematics and PCB boards.

View attachment 18532


...And from the schematic I made this prototype to test it out. This was a pretty simple circuit to make. It uses one input for the PIR sensor and three outputs. Two for the Bi-color LED and one for the Relay.




Everything went pretty smooth in testing so next thing was design a PCB for it.

 
Top