Skip to content

Activity

Light alarm

Advanced | MakeCode, Python | LED display, Light sensor, Radio, Sound | Communication, Energy, Iteration, Light, Relational operators, Selection, Sensors

Step 1: Make it

What is it?

A radio-controlled remote alarm so when you know someone’s turned the lights on – or opened a drawer or bag.

micro:bit showing angry face with radio waves coming out and a light source shining on it

How it works

  • Like the Sunlight sensor project, this uses the micro:bit’s LEDs as a light sensor to measure how much light is falling on the display.
  • Using a loop, every 10 seconds the sensor program sends a radio message to say if it’s light or dark. It uses selection to transmit a ‘lights on’ message if the light measurement is more than (>) 50, and ‘lights off’ if it’s less than (<) 50. You may need to change these values depending on the lighting conditions where you are.
  • Pressing input button A shows a light measurement on the LED display output which will help you set up your alarm, so you can change 50 to the best number for your environment.
  • The alarm program reacts whenever it gets a message. If the message is equal to ‘lights off’ then it clears the LED display, but if it is equal to ‘lights on’ then it shows an angry face and plays the ‘BADDY’ alarm sound.
  • Make sure your sensor and alarm use the same radio group – any number between 0 and 255.

What you need

  • 2 micro:bits, at least one with a battery pack
  • something precious to keep safe
  • optional headphones, buzzers or powered speakers for alarm

Step 2: Code it

Sensor / transmitter:

1from microbit import *
2import radio
3radio.config(group=5)
4radio.on()
5
6while True:
7    if button_a.was_pressed():
8        display.scroll(display.read_light_level())
9    if display.read_light_level() > 50:
10        radio.send('lights on')
11    else:
12        radio.send('lights off')
13    sleep(10000)
14

Alarm / receiver:

1from microbit import *
2import music
3import radio
4radio.config(group=5)
5radio.on()
6
7while True:
8    message = radio.receive()
9    if message:
10        if message == 'lights off':
11            display.clear()
12        elif message == 'lights on':
13            display.show(Image.ANGRY)
14            music.play(music.BADDY)
15

Step 3: Improve it

  • Combine this with the Tilt alarm project to sense when something is being moved OR if the lights go on.
  • Make the alarm work the other way so it goes off if someone turns the lights off.
  • Adapt it to monitor energy use: use a variable to track the number of seconds the lights are left on or trigger an alarm only after they’ve been on too long.