1 Attachment(s)
12F629 LDR - Light Dependant Resistor
Hi all
I've been dabbling with an LDR and a PIC 12F629 (any PIC should work though) and found it so straight forward and simple that I thought some forum users might enjoy it and come up with some more interesting ideass.
This really does add a whole new dimension to the traditional day/night sensor. You could start logging the amount of actual daylight etc.
LDR's can be setup using either one of two very simple circuits.See this link for more info, I found it very helpful and interesting.
http://www.doctronics.co.uk/ldr_sensors.htm
Here is the code
Code:
*************************************************************************************
'12f629 to demo the use of an LDR by DENNIS
'see ldr test scehmatic
'USES ONLY 1 PIC PIN
' +---+--+---+
' VCC 1 +--+ 8 GND
' OSC1,X1,GP5 2 7 GP0,CIN+,PGD
' OSC2,X1,GP5 3 6 GP1,CIN-,PGC
' VPP,/MCLR,GP3 4 5 GP2,T0CKI,INT,COUT
' +----------+
' 12Cxxx, 12Fxxx
'
' I tested circuit in daylight and indoors, make sure you have enough light and dark separation
' Adjust the 10K potentiometer for the correct sensitivty
' For reverse operation just change IF GPIO.2 = 1 then dark << replace with 0
' This circuit does not require any crucuial timing and can be moved to any PIC
'************************************************************************************
define OSC 4 '4MHz
DEFINE OSCCAL_1K 1 ' Set OSCCAL for 1k -HELPS tuning the crystal for more accurate timing
CMCON = 7 'set digital mode
'ansel = 0 'GPIO.2 and GPIO.3 set as digital - disable if using 12629 for LDR
'TRISIO = %76543210 ' port GPIO 0=output and 1=input
TRISIO = %0000100 ' GPIO.2 is an input - all the rest are outputs
' Variables
LED var GPIO.0
' end of variables
Start:
If GPIO.2 = 1 then night
HIGH led 'Turn on/off whatever you like by changing this line/replacing LED
goto start
Night:
Low led 'Turn off LED (or device)
goto start
END
In my experiment I found the best light source to be real daylight, and walking around with the breadboard as I walked in and out the door the LED would switch off (in the dark- indoors) or on (outside in daylight).
To alter the sensitivity the 10K pot can be adjusted, I found that halfway worked well.
You can reverse the operation , in other words, off at night and on during the day by just changing the 1 in this line to a 0
Code:
If GPIO.2 = 1 then night
The schematic is attached.
Hope this helps someone, and post back any info about how it worked for you or what could be changed or added :-)
Kind regards
Dennis
1 Attachment(s)
Please find the corrected schematic here
Here it is .. the schematic corrected to show +5V and GND rails :-)
Kind regards
Dennis