DIY sunrise alarm

By Russell Barnes. Posted

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
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

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

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 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 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

from Tkinter import * 
import RPi.GPIO as GPIO
import time, math



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)

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):
  t2 = time.time()
  return (t2 - t1) * 1000000

def analog_read():
  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)
      GPIO.output(buzzer_pin, False)
      if x == 250:
        x = 0
    GPIO.output(red_pin1, False)
    GPIO.output(red_pin2, True)
    while True:
      x = x + 1
      GPIO.output(buzzer_pin, True)
      GPIO.output(buzzer_pin, False)
      if x == 250:
        x = 0

From The MagPi store