Motion Activated Light Sensor
My friend Blauvster pointed me to some cheap LED strip lights for sale on Amazon. Later he asked how hard it would be to hook up a PIC with a motion and light sensor to turn the lights on when it's dark and motion is sensed. This should be a pretty simple handy device to use several locations around the house, so here goes.
In order to check for ambient light in this circuit we'll be using a photoresistor. If you're unfamiliar with photoresistors, they are devices in which the resistance across the device changes with the amount of light shining on the device. So the brighter the light on the photoresistor the less resistance it provides. SocietyOfRobotics.com has a great tutorial with example circuits and some very useful equations to help you figure out resistor values and how to connect a photoresistor in your circuit.
The equation they use to find the static resistor, R=sqrt(PR_dark*PR_light), will get you close to the value you'll need. My plan is to get a potentiometer close to this value in order to fine tune the circuit. I got a pack of GL5528 photoresistors on Ebay. They range from 1M Ohms in darkness to 10K Ohms in bright light. using these values in the provided equation works out to about 100K Ohms. Plugging 100K into voltage divider equations gives 0.45 Vout at 10K Ohms and 4.55 Vout at 1M Ohm. This is well beyond the limit thresholds for our PIC16F84 controller calls out 0.8V and bellow as a Low signal and 2V and above as a High signal. You'll want to double check the specs and do some experiments with the photoresistors you pick out before settling on a static resistor or potentiometer.
PIR Motion Sensor
You can pick up a Parallax PIR infrared motion sensor from Radioshack for about $10. This is pretty cheap and ready to use out of the box. It has a jumper for selecting high and low sensitivity which, according to the data sheet, switches between 30 foot and 15 foot motion detection. It also has a 3 pin connector for power, ground, and motion detection output. The output goes logic high and stays there as long as motion is detected.
|PIR Motion Sensor Module|
Note: I have seen similar sensors on ebay for about $2.50. The specs on these sensors will almost undoubtedly be different from the Radioshack Parallax model, but should work in a similar fashion. Here's a nicely packaged, easy to use solution from Amazon.
- 5V Logic Power
- 12V LED Power: Solid State Relay & Transistors
PIC Code and Programming
- Check for motion(PORTB<7:4> interrupt-onchange)
- If NO motion, turn OFF lights
- If motion, Check if lights are ON
- If lights are ON, keep ON
- If lights are OFF, check for ambient light
- If ambient light, keep lights OFF
- If NO ambient light, turn lights ON
A change-in-state interrupt will wake the chip from sleep mode. We then run through the listed tests here, adjust the light control output accordingly, and return the controller to sleep.
Assembly code for the PIC16F84
;-----------------------------------------------------------------------------------------------; ; Motion Light Sensor ; -------- ; ; Author: Elliott Long ; Date: Aug. 2012 ; ; 4MHz internal resonator used ; ; PORTB Change Interrupt used ; PORTB, 4 = Motion Detecter Input Change on interrupt ; PORTB, 5 = Photoresistor Input ; PORTA, 1 = LED Light Strip Output ; PORTA, 2 = Power Latch Output ;-----------------------------------------------------------------------------------------------; ;Constants STATUS equ 03h RP0 equ 05h PORTA equ 05h PORTB equ 06h INTCON equ 0Bh OPT equ 81h OPT equ 81h TRISA equ 85h TRISB equ 86h C equ 00h Z equ 02h Delay1 equ 0Ch Delay2 equ 0Dh Delay3 equ 0Eh Time equ 13h Brightness equ 14h ORG 0000h GOTO STARTUP ORG 0004h ;Interrupt routine MOVF PORTB,1 ; you must read PORTB upon Int-on-chg ;if NO motion turn OFF lights BTFSS PORTB,4 GOTO LIGHTOFF ;If PORTB,4 is high(motion) this is skipped ;MOTION DETECTED ;if lights are ON, keep ON BTFSC PORTA,1 GOTO RTN ;If PORTA,1 is high(lights ON) we return leaving them ON ;LIGHTS OFF BTFSC PORTB,5 GOTO RTN ;if PORTB,5 is high (ambient light) we return leaving lights OFF ;if NO ambient light, turn lights ON LIGHTON BSF PORTA,2 ;Set the Power Latch Output PORTA, 2 BSF PORTA,1 ;Set Light Output PORTA,1 GOTO RTN LIGHTOFF BCF PORTA,2 ;Set the Power Latch Output PORTA, 2 BCF PORTA,1 ;Clear Light Output PORTA,1 RTN BCF INTCON,0 ;Clear Interrupt Flag RETFIE ;Startup STARTUP BCF STATUS,RP0 ;Bank 0 CLRF PORTA ;Init PORTA CLRF PORTB ;Init PORTB BSF INTCON,7 ;Global Interrupt Enable BSF INTCON,3 ;PortB Change Interrupt Enable BCF INTCON,0 ;Clear Interrupt Flag BSF STATUS,RP0 ;Bank 1 MOVLW 30h ;Set PORTB 4&5 as Input 110000 MOVWF TRISB ;Setup RB MOVLW 00h ;Set PORTA outputs MOVWF TRISA ;Setup RA as outputs BCF STATUS,RP0 ;Bank 0 BCF STATUS,C BCF INTCON,0 ;Clear Interrupt Flag MOVF PORTB,1 ;Read PortB to itself ;Loop through testing LOOP: ;Main Program which just loops to sleep SLEEP ;BCF INTCON,0 ;Clear Interrupt Flag GOTO LOOP END
This programming works with the circuit always powered. When the PIR sensor attached to pin 4 triggers on motion it causes an interrupt on the PIC which wakes the PIC up from sleep mode. The program then checks for if the LEDs are already on and if so goes back to sleep. If the LEDs are off the program checks the photoresistor input on pin 3 to see if it's dark in the area. If it's dark the program turns the LEDs on and goes back to sleep. If it is not dark the LEDs are left off and the PIC goes back to sleep.
Blauvster threw out the idea of having the entire circuit only draw power when it's dark. This way when the area becomes dark the circuit turns on. Then when motion is sensed the power to the circuit is latched on by a secondary source controlled by the PIC. This way when the LEDs are turned on power to the circuit is not killed. As far as programming is concerned this is a simple addition of turning on the output to RA2 before turning on RA1 to power the LEDs and then turning off that output just after turning off the LED outputs. In progress...
- Enjoy and feel free to report any problems or suggestions to firstname.lastname@example.org.