2 years ago

DIY sunrise alarm

Use a breadboard and simple components to sense light levels and activate a loud (and blinky) alarm to wake you up each morning!

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.

You’ll need

2× LEDs
Light-dependant resistor (LDR)
330nF ceramic capacitor
Buzzer
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.

Wire up the alarm as shown

Wire up the alarm as shown

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:

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 01_sunrise_alarm.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 01_sunrise_alarm.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.

Code listing

Sunrise_alarm.py