Skip to content


Energy light timer

Advanced | MakeCode, Python | Buttons, LED display, Light sensor | 13 Climate, Boolean logic, Input/output, Sensors, Variables

Step 1: Make it

What is it?

Time how long your lights are left on to track your energy use. You could also use this project to track hours of sunshine in a weather station project.

This project is part of a series created to offer problem-solving and prototyping activities designed to explore technology as a solution to the challenges of the Global Goals for Sustainable Development.

What you'll learn

  • how to use sensors and code to monitor real world events
  • place a data-logger to record reliable data
  • collect and collate data over time to spot patterns in energy use
  • interpret and analyse data in order to effect changes in behaviour
  • how variables can be changed by a user to configure a system prior to use

How to use it

  • First use the Energy light meter project to find out the readings when your lights are on and off. Make sure you do this in the same place and lighting conditions that you will place your monitoring micro:bit, and ensure daylight doesn't trigger a false reading that your lights are on.
  • Put your light reading into the code where the LIGHT variable is set. We have given you the number 100 but you will probably need to change this before flashing the code on to your micro:bit.
  • Attach a battery pack and place your micro:bit under the light you want to monitor. You should see a dot on the display when the light is off, and the display lights up when your light is on. If this doesn't work, consider using the Light meter project again to find the light level when the light is on, or move the micro:bit.
  • The micro:bit will keep timing and when you press button B it will show how long the light has been switched on in minutes.

How it works

  • The program uses a Boolean variable called timing to control the program. Boolean variables can only have two values: true or false.
  • If the light sensor reading is above the level you set, it starts the timer and lights up the LED display. If it falls below this level, it stops the timer and shows a dot on the display.
  • This project uses hysteresis to make sure the timer doesn't switch on and off too often when the light level is changing slightly around the threshold for triggering the timer. It creates a larger band around the threshold which has to be crossed before the timer is turned on or off.
  • Hysteresis is a common feature in control systems that use sensors, for example in heating systems that have a thermostat. If you set your thermostat to a certain temperature, you don't want the heating repeatedly switching on and off very quickly when the temperature hovers around the number you have set. Hysteresis prevents this happening.

What you need

  • micro:bit
  • MakeCode or Python editor
  • battery pack (recommended)

Step 2: Code it

1from microbit import *'L')
4LIGHT = 100  # <<< Enter your 'measured' reading here
9ON_IMAGE = Image('99999:99999:99999:99999:99999')
10OFF_IMAGE = Image('00000:00000:00900:00000:00000')
11timing = False
12start_time = 0
13total_time = 0
14reading = display.read_light_level()
17def show_number(n):
18    # Make number display the same as MakeCode
19    if len(str(n)) == 1:
21    else:
22        display.scroll(n)
24while True:
25    reading = display.read_light_level()
26    if reading < DARK:
27        if timing:
28            # it has just gone dark, update timer for 'on' time
29            end_time = running_time()
30            total_time += (end_time - start_time)
31            timing = False
33    elif reading >= LIGHT:
34        if not timing:
35            # it has just gone light, start the timer
36            start_time = running_time()
37            timing = True
39    if button_b.was_pressed():
40        # calculate and display cumulative time in minutes
41        minutes = total_time / 60000
42        if timing:
43            # adjust live for current ON time
44            minutes += (running_time() - start_time) / 60000
45        display.clear()
46        show_number(round(minutes))  # whole numbers only
47        sleep(500)
49    # update the display with the ON/OFF state
50    if timing:
52    else:
54    sleep(1000)

Step 3: Improve it

  • Change the patterns shown on the LED display to make the batteries last longer, or dim the display.
  • Use the time recorded to work out how much electricity you have used and what it may cost. Details are in our Energy cost calculator project
  • Use the same code to measure hours of sunlight. Use the Light meter project to calculate the light value when sun is shining on the micro:bit and when it's cloudy. You may need to make a container for your micro:bit with a translucent lid to diffuse the light and protect the micro:bit from rain, for example an old plastic food container.