With the help of some exciting code and some clever electronics, you can use your Raspberry Pi to read an analogue signal from a photoresistor without the need for a conversion chip! Armed with this power, you can measure the ambient light level and trigger an effective alarm. The project is designed as an extension to the projects found in the Monk Makes Raspberry Pi Electronics Starter Kit.
The full article can be found in The MagPi 52 and was written by Henry Budden.
Light-dependant resistor (LDR)
330nF ceramic capacitor
2× 1kΩ resistors
2× 470Ω resistors
6× Male-to-female jumper wires
3× Male-to-male jumper wires
Or Monk Makes Raspberry Pi Electronics Starter Kit
STEP-01 Connect the resistors
Once you have the components, begin to make the circuit by connecting up the resistors. Do this as shown in the circuit diagram, pushing each component’s legs into the holes in the breadboard. Ensure that the bottom two resistors are 470Ω (yellow, purple, and brown), and the top two are 1kΩ (brown, black, and red).
STEP-02 Add the rest of the components
Next, add the LEDs, making sure that the long legs are connected towards the bottom of the diagram, as shown. The flat side of the LEDs should be facing towards the 1kΩ resistors. When connecting the buzzer, the longest leg should be facing the bottom of the breadboard. Finally, connect the LDR and capacitor. These can be connected in any orientation.
STEP-03 Connect to the Pi
Make sure your Raspberry Pi is turned off and unplugged before you do this. Using the three male-to-male jumper wires, connect the ground to the two LEDs, the buzzer, and the LDR as shown in the diagram. Next, use the remaining male-to-female jumper wires to connect the breadboard to the Raspberry Pi’s GPIO pins.
STEP-04 Install the software
Turn on your Raspberry Pi, and ensure it is connected to the internet. Using the Terminal, clone the GitHub repository containing the code to your Pi’s SD card using the command:
git clone https://github.com/henrybudden/rpesk-advanced/
Once the files have downloaded and you have returned to the command prompt, change into the directory containing the code by using the command cd rpesk-advanced.
STEP-05 Run the code
The time has come to run the code and test out the alarm! After checking that the circuit you have built is an exact replica of the one shown in the circuit diagram, and that it is connected to the Pi, run the command sudo python 01sunrisealarm.py. You can test that the alarm works by shining a torch at the photoresistor from within a fairly light room. The LEDs should start to flash, and the buzzer should sound. If this happens, congratulations!
STEP-06 Make it your own
The default threshold for the alarm’s activation is when the photoresistor reaches a value of 50, which works well for testing as described previously. However, in order to use the alarm to accurately detect the sunrise in the morning, this value can be changed by entering the file editor with the command nano 01sunrisealarm.py and then changing the value found on line 13. We would recommend that you use 30 for fairly accurate detection of dawn. Save this change by pressing CTRL+X, followed by Y, then the ENTER key. This code can now be run again as in step 05.
from Tkinter import * import RPi.GPIO as GPIO import time, math GPIO.cleanup() GPIO.setmode(GPIO.BCM) sunrise = 50 a_pin = 18 b_pin = 23 buzzer_pin = 24 red_pin1 = 27 red_pin2 = 22 GPIO.setup(buzzer_pin, GPIO.OUT) GPIO.setup(red_pin1, GPIO.OUT) GPIO.setup(red_pin2, GPIO.OUT) def discharge(): GPIO.setup(a_pin, GPIO.IN) GPIO.setup(b_pin, GPIO.OUT) GPIO.output(b_pin, False) time.sleep(0.01) def charge_time(): GPIO.setup(b_pin, GPIO.IN) GPIO.setup(a_pin, GPIO.OUT) GPIO.output(a_pin, True) t1 = time.time() while not GPIO.input(b_pin): pass t2 = time.time() return (t2 - t1) * 1000000 def analog_read(): discharge() return charge_time() def read_resistance(): n = 20 total = 0; for i in range(1, n): total = total + analog_read() reading = total / float(n) resistance = reading * 6.05 - 939 return resistance def light_from_r(R): return math.log(1000000.0/R) * 10.0 while True: GPIO.output(red_pin1, False) GPIO.output(red_pin2, False) light = light_from_r(read_resistance()) print light x = 0 if light > sunrise: GPIO.output(red_pin1, True) GPIO.output(red_pin2, False) while True: x = x + 1 GPIO.output(buzzer_pin, True) time.sleep(0.001) GPIO.output(buzzer_pin, False) time.sleep(0.001) if x == 250: x = 0 break GPIO.output(red_pin1, False) GPIO.output(red_pin2, True) while True: x = x + 1 GPIO.output(buzzer_pin, True) time.sleep(0.001) GPIO.output(buzzer_pin, False) time.sleep(0.001) if x == 250: x = 0 break