The requirement is to get the RPI camera with pir motion detection and led light to turn its led light on only when it is dark so that the camera can see at night time.
The photoresistor resistance changes with different levels of light and measures
> 1MΩ in total darkness.
= 2KΩ when the round circuit box LED light is on at night (measured with the photoresistor positioned in picture)
= 240Ω with bright clouds,
< 60Ω in sunshine
Since RPI has no analog pins to measure resistance it will need to do it with its digital pins.
There are two solutions I am proposing, one uses 555 hysteresis the other solution uses a capacitor.
The following article is what I have based the capacitor solution on.
http://www.raspberrypi-spy.co.uk/2012/08/reading-analogue-sensors-with-one-gpio-pin/
I have modified the circuit to use an input pin to measure when the capacitor goes high and an output pin to drain the capacitor using a transistor as I didn't like his idea of draining the capacitor into the output pin. I am also using a 100uF Capacitor instead of his 1uF for better resolution of measurements, as explained in the following calculation.
Resolution of different day/night states
night
t = R * C
t = >1MΩ * 100uF
t = >100s
night with LED on
t = R * C
t = 2000Ω * 100uF
t = 0.2s
cloudy day - referred to as cloudy
t = R * C
t = 240Ω * 100uF
t = 0.024s
sun shine
t = R * C
t = < 60Ω * 100uF
t = < 0.006s
My time resolution is about 50ms because I only have 1 delay of 50ms at end of my main loop driving all the gpio logic of the RPI. This is so that the cpu is not thrashed and for power conservation.
What this means is, I will be able to distinguish between cloudy, night with LED on and night no light.
I won't be able to distinguish between cloudy and sun shine unless I increase the Capacitor F or decrease the delay in the while loop to less than cloudy time. I do not need to distinguish between cloudy and sunshine so I will leave it as is.
In the circuit diagram I have simulated RPI GPIO input pin with 50KΩ Resistor and the RPI GPIO output pin with 2KΩ Resistor and manual switch. The manual switch closed is simulating the GPIO Output pin going high and switch open is simulating GPIO Output pin low.
The output pin is set low to open the switch and charge up the capacitor.
The output pin is set high to close the switch and cause the capacitor to discharge.
The Input pin gets set high with a Voltage above 2V.
The Input pin gets set low with a Voltage below 1V.
Reference
http://www.mosaic-industries.com/embedded-systems/microcontroller-projects/raspberry-pi/gpio-pin-electrical-specifications
I had to put a 35KΩ dropdown resister on the input pin to get the