Skip to content


Energy cost calculator

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

Step 1: Make it

What is it?

The third of three projects to help you gather data about your energy use. Learn how to work out the cost of energy and make a timer that measures how much electric lights cost to run.

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.


Coding guide

What you'll learn

  • How to use mathematics to convert units: watts to kilowatts and minutes to hours
  • How to calculate energy used in kilowatt hours (kWh) from power measured in watts (W) and time.
  • How variables can be used to store values for a user to configure a system prior to use.

How to use it

The previous project, the Energy light timer, measured how long a light is switched on. This special version of the same project works in a similar way, but it calculates and displays the cost of the energy used by a light bulb rather than the time.
To use it, you need to know 3 numbers:

  • The light reading when the light is switched on. This is the number you found using the first project, the Energy light meter and used in the Energy light timer project
  • What wattage your lighting is. This is usually printed on the lightbulb.
  • And how much you pay for your electricity, the unit cost per kWh

The program stores these three numbers in variables which you need to edit before using this project.

Use the Light meter project as before to get a number to put in to the LIGHT variable.

Look at the light bulb to find out how many watts it uses. Remember light bulbs can get very hot and are usually connected to mains electricity, so ask an adult to do this for you. Put that number in the WATTS variable.

Finally put in the cost per kWh in the COSTPERKWH variable. You may need to ask an adult to find this number for you, or find out who supplies your energy and research their unit cost online.

Flash this program onto a micro:bit and attach a battery pack. Place this timer near a light source, and the micro:bit will use the information you gave it about light intensity, light bulb wattage and the cost of your electricity combined with readings from its built-in light sensor and the processor’s timer to monitor the cost of the electricity used over a period of time.

You can take cost readings at the same time every day or every week by pressing button B. Re-set it by pressing the reset button on the back of the micro:bit and check it’s gone back to zero by pressing button B again.

Remember this is just one lightbulb, so consider the cost across a whole building for a year.
Now you have some data about energy use, how would you go about changing people’s behaviour to save energy and money, and perhaps help combat climate change?

How it works

  • The program times how long a light is left switched on using the micro:bit's light sensor.
  • The amount of energy you use is calculated by multiplying the power of the lightbulb by the time. This is measured in kilowatt hours (kWh). Because a lightbulb's power is usually measured in watts, not kilowatts, the program converts from watts to kilowatts by dividing by 1000:
    watts ÷ 1000 = kilowatts
    So for example, a 60 watt lightbulb uses 0.06 kilowatts of power:
    60 W ÷ 1000 = 0.06 kW
  • The program also converts the time units. To convert time from seconds to hours divide the time in seconds by 60. So:
    600 minutes ÷ 60 = 10 hours
  • To work out the cost of energy used in a given period of time, the program multiplies the energy use in kilowatt hours by the unit cost per kilowatt hour.
  • If my energy costs £0.16p per kWh, leaving a 60 watt lightbulb on for 10 hours will cost me ten pence:
    0.6 kWh × 16p = 9.6p

What you need

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

Step 2: Code it

1from microbit import *'L')
4LIGHT = 114  # <<< Enter your 'measured' reading here
5WATTS = 1000  # <<< Enter your bulb wattage here
6COSTPERKWH = 0.18  # <<< Enter unit electricity cost here
11ON_IMAGE = Image('99999:99999:99999:99999:99999')
12OFF_IMAGE = Image('00000:00000:00900:00000:00000')
13timing = False
14start_time = 0
15total_time = 0
16reading = display.read_light_level()
19def calc_cost(m):
20    # Calculate cost of electricity for this number of mins
21    kw = WATTS / 1000.0  # answer as a decimal
22    hours = m / 60.0  # answer as a decimal
23    kwh = kw * hours
24    cost = kwh * COSTPERKWH
25    return cost  # as pounds and pence
27def show_number(n):
28    # Scroll number as 3 decimal places
29    display.scroll("%.3f" % n)
31while True:
32    reading = display.read_light_level()
33    if reading < DARK:
34        if timing:
35            # it has just gone dark, update timer for 'on' time
36            end_time = running_time()
37            total_time += (end_time - start_time)
38            timing = False
40    elif reading >= LIGHT:
41        if not timing:
42            # it has just gone light, start the timer
43            start_time = running_time()
44            timing = True
46    if button_b.was_pressed():
47        # calculate and display cumulative cost in pounds and pence
48        minutes = total_time / 60000
49        if timing:
50            # adjust live for current ON time
51            minutes += (running_time() - start_time) / 60000
52        display.clear()
53        show_number(calc_cost(minutes))
54        sleep(500)
56    # update the display with the ON/OFF state
57    if timing:
59    else:
61    sleep(1000)

Step 3: Improve it

  • Use multiple micro:bits to measure costs of lighting in different places.
  • Use radio to transmit cost data to another micro:bit and reset the timer remotely.