BBC micro:bit

Weather Station

Measure the temperature and the light, show a sun or a snowflake, and switch on a night light in the dark.

Day 5 45 to 60 minutes 6 steps Key Stage 3
Start step 1
Warm today: the weather station shows a sun.

The micro:bit can feel how warm it is and how bright the room is. Today you will turn it into a weather station: it tells you the temperature and the light level, shows a sun when it is warm and a snowflake when it is cold, and lights up like a night light when the room goes dark.

You will learn

  • Read the temperature and light level sensors
  • Turn numbers into text with str()
  • Compare readings with > and <
  • Make the micro:bit react to the world on its own

You will need

  • A computer with Chrome or Edge
  • A micro:bit and a USB data cable, or the simulator

Words to know

temperature
How hot or cold something is, measured here in degrees Celsius.
light level
How bright it is, from 0 in the dark to 255 in bright light.
str()
Turns a number into text, so it can be joined to other text.
threshold
The value that decides a change, such as 24 degrees for the sun.

Step by step

  1. Step 1: Take the temperature

    Start a new project called Weather Station. temperature() reads the micro:bit's thermometer, in degrees Celsius. Press A to scroll it. In the simulator, the Temperature control sets how warm the pretend micro:bit is.

    main.pyMicroPython micro:bit editor
    from microbit import * while True:    if button_a.was_pressed():        display.scroll(temperature())    sleep(100)
    Press A: it scrolls 21, the temperature.
  2. Step 2: Add the units

    A temperature needs its units. To join a number to some text with +, turn the number into text first with str().

    New: line 5.

    main.pyMicroPython micro:bit editor
    from microbit import * while True:    if button_a.was_pressed():        display.scroll(str(temperature()) + "C")    sleep(100)
    Now it scrolls 21C.
    Watch out Without str(), the program stops with a TypeError: Python cannot add a number to text.
  3. Step 3: Measure the light

    display.read_light_level() measures how bright it is, from 0 in the dark to 255 in bright light. Press B to scroll it.

    New: lines 6 and 7.

    main.pyMicroPython micro:bit editor
    from microbit import * while True:    if button_a.was_pressed():        display.scroll(str(temperature()) + "C")    if button_b.was_pressed():        display.scroll(display.read_light_level())    sleep(100)
    Press B: it scrolls the light level, 120.
    Tip The micro:bit measures light with its own lights, used backwards as sensors.
  4. Step 4: Sunny or snowy?

    Now the station reports the weather by itself. Every half a second it reads the temperature: above 24 degrees it shows a sun, below 18 a snowflake, and in between a happy face. Draw the sun and snowflake pictures at the top.

    New: lines 2 to 13 and 20 to 27.

    main.pyMicroPython micro:bit editor
    from microbit import * sun = Image("90909:"            "09990:"            "99999:"            "09990:"            "90909") snow = Image("00500:"             "50505:"             "05550:"             "50505:"             "00500") while True:    if button_a.was_pressed():        display.scroll(str(temperature()) + "C")    if button_b.was_pressed():        display.scroll(display.read_light_level())    temp = temperature()    if temp > 24:        display.show(sun)    elif temp < 18:        display.show(snow)    else:        display.show(Image.HAPPY)    sleep(500)
    Slide the temperature up to 28: a sun.
  5. Step 5: A night light

    When the room goes dark, light every light. Check the light level first, so the night light wins over the weather pictures.

    New: lines 15, 16 and 23 to 25.

    main.pyMicroPython micro:bit editor
    from microbit import * sun = Image("90909:"            "09990:"            "99999:"            "09990:"            "90909") snow = Image("00500:"             "50505:"             "05550:"             "50505:"             "00500") all_on = Image("99999:99999:99999:99999:99999") while True:    if button_a.was_pressed():        display.scroll(str(temperature()) + "C")    if button_b.was_pressed():        display.scroll(display.read_light_level())    temp = temperature()    if display.read_light_level() < 30:        display.show(all_on)    elif temp > 24:        display.show(sun)    elif temp < 18:        display.show(snow)    else:        display.show(Image.HAPPY)    sleep(500)
    Slide the light level right down: every light comes on.
  6. Step 6: Test it for real

    Send it to your micro:bit. Hold it in your hands for a minute to warm it up. Then cover it with your hand to make it dark.

    Send it, then warm it in your hands or cover it up.
    Tip The micro:bit measures the temperature of its own processor, so it often reads a degree or two warmer than the room.

Test it

Tick each one when it works. If one does not, look back at the step it comes from.

Stretch challenges

Finished early? Try these. Each one has a hint if you get stuck.

  1. Fahrenheit

    Show the temperature in Fahrenheit too, on button B held with A.

    Show a hint

    Fahrenheit is Celsius × 9 ÷ 5 + 32: temperature() * 9 / 5 + 32.

  2. Warm or cold alarm

    Play a sound when it gets too hot, as a fridge alarm would.

    Show a hint

    Inside the hot branch, music.play(music.BA_DING). Do not forget import music.

  3. Light meter

    Show the light level as a bar: one row lit for dim light, all five rows for bright light.

    Show a hint

    rows = display.read_light_level() // 51 gives a number from 0 to 5. Light that many rows from the bottom with display.set_pixel() in two loops.

The finished code

Show the whole program
main.pyMicroPython micro:bit editor
from microbit import * sun = Image("90909:"            "09990:"            "99999:"            "09990:"            "90909") snow = Image("00500:"             "50505:"             "05550:"             "50505:"             "00500") all_on = Image("99999:99999:99999:99999:99999") while True:    if button_a.was_pressed():        display.scroll(str(temperature()) + "C")    if button_b.was_pressed():        display.scroll(display.read_light_level())    temp = temperature()    if display.read_light_level() < 30:        display.show(all_on)    elif temp > 24:        display.show(sun)    elif temp < 18:        display.show(snow)    else:        display.show(Image.HAPPY)    sleep(500)
Notes for the teacher

Timing

  1. 15 minutes: steps 1 and 2. The str() error is worth letting students hit on purpose.
  2. 20 minutes: steps 3 to 5.
  3. 15 minutes: step 6 and the tests. Hands warm the micro:bit faster than you might think.

Mistakes to look out for

  1. display.scroll(temperature() + "C"): a TypeError, which step 2 explains.
  2. temperature without brackets shows nothing useful: it is the function itself, not a reading.
  3. The night light flickers if the light level is close to 30: try a lower number, or hold a hand right over it.
  4. The order of the if and elif checks decides what wins: the night light must come first.

Going further

A real weather station logs readings over time. The micro:bit V2 can save data with its data logging feature (import log), a good extension for a keen pair.