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.
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
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.1from microbit import *2 3while True:4 if button_a.was_pressed():5 display.scroll(temperature())6 sleep(100)ReferenceIdeasAPIProjectWeather Station1from microbit import *2 3while True:4 if button_a.was_pressed():5 display.scroll(temperature())6 sleep(100)Send to micro:bitSaveOpen…SimulatorTemperature21°CPress A: it scrolls 21, the temperature. Step 2: Add the units
A temperature needs its units. To join a number to some text with
+, turn the number into text first withstr().New: line 5.
1from microbit import *2 3while True:4 if button_a.was_pressed():5 display.scroll(str(temperature()) + "C")6 sleep(100)ReferenceIdeasAPIProjectWeather Station1from microbit import *2 3while True:4 if button_a.was_pressed():5 display.scroll(str(temperature()) + "C")6 sleep(100)Send to micro:bitSaveOpen…SimulatorTemperature21°CNow it scrolls 21C. Watch out Withoutstr(), the program stops with a TypeError: Python cannot add a number to text.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.
1from microbit import *2 3while True:4 if button_a.was_pressed():5 display.scroll(str(temperature()) + "C")6 if button_b.was_pressed():7 display.scroll(display.read_light_level())8 sleep(100)ReferenceIdeasAPIProjectWeather Station1from microbit import *2 3while True:4 if button_a.was_pressed():5 display.scroll(str(temperature()) + "C")6 if button_b.was_pressed():7 display.scroll(display.read_light_level())8 sleep(100)Send to micro:bitSaveOpen…SimulatorLight level120Press B: it scrolls the light level, 120. Tip The micro:bit measures light with its own lights, used backwards as sensors.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.
1from microbit import *2 3sun = Image("90909:"4 "09990:"5 "99999:"6 "09990:"7 "90909")8 9snow = Image("00500:"10 "50505:"11 "05550:"12 "50505:"13 "00500")14 15while True:16 if button_a.was_pressed():17 display.scroll(str(temperature()) + "C")18 if button_b.was_pressed():19 display.scroll(display.read_light_level())20 temp = temperature()21 if temp > 24:22 display.show(sun)23 elif temp < 18:24 display.show(snow)25 else:26 display.show(Image.HAPPY)27 sleep(500)ReferenceIdeasAPIProjectWeather Station1from microbit import *2 3sun = Image("90909:"4 "09990:"5 "99999:"6 "09990:"7 "90909")8 9snow = Image("00500:"10 "50505:"11 "05550:"12 "50505:"13 "00500")Send to micro:bitSaveOpen…SimulatorTemperature28°CSlide the temperature up to 28: a sun. 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.
1from microbit import *2 3sun = Image("90909:"4 "09990:"5 "99999:"6 "09990:"7 "90909")8 9snow = Image("00500:"10 "50505:"11 "05550:"12 "50505:"13 "00500")14 15all_on = Image("99999:99999:99999:99999:99999")16 17while True:18 if button_a.was_pressed():19 display.scroll(str(temperature()) + "C")20 if button_b.was_pressed():21 display.scroll(display.read_light_level())22 temp = temperature()23 if display.read_light_level() < 30:24 display.show(all_on)25 elif temp > 24:26 display.show(sun)27 elif temp < 18:28 display.show(snow)29 else:30 display.show(Image.HAPPY)31 sleep(500)ReferenceIdeasAPIProjectWeather Station13 "00500")14 15all_on = Image("99999:99999:99999:99999:99999")16 17while True:18 if button_a.was_pressed():19 display.scroll(str(temperature()) + "C")20 if button_b.was_pressed():21 display.scroll(display.read_light_level())22 temp = temperature()23 if display.read_light_level() < 30:24 display.show(all_on)25 elif temp > 24:Send to micro:bitSaveOpen…SimulatorLight level10Temperature21°CSlide the light level right down: every light comes on. 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.
ReferenceIdeasAPIProjectWeather Station1from microbit import *2 3sun = Image("90909:"4 "09990:"5 "99999:"6 "09990:"7 "90909")8 9snow = Image("00500:"10 "50505:"11 "05550:"12 "50505:"13 "00500")Send to micro:bitSaveOpen…SimulatorSend 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.
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.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 forgetimport music.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() // 51gives a number from 0 to 5. Light that many rows from the bottom withdisplay.set_pixel()in two loops.
The finished code
Show the whole program
1from microbit import *2 3sun = Image("90909:"4 "09990:"5 "99999:"6 "09990:"7 "90909")8 9snow = Image("00500:"10 "50505:"11 "05550:"12 "50505:"13 "00500")14 15all_on = Image("99999:99999:99999:99999:99999")16 17while True:18 if button_a.was_pressed():19 display.scroll(str(temperature()) + "C")20 if button_b.was_pressed():21 display.scroll(display.read_light_level())22 temp = temperature()23 if display.read_light_level() < 30:24 display.show(all_on)25 elif temp > 24:26 display.show(sun)27 elif temp < 18:28 display.show(snow)29 else:30 display.show(Image.HAPPY)31 sleep(500)Notes for the teacher
Timing
- 15 minutes: steps 1 and 2. The
str()error is worth letting students hit on purpose. - 20 minutes: steps 3 to 5.
- 15 minutes: step 6 and the tests. Hands warm the micro:bit faster than you might think.
Mistakes to look out for
display.scroll(temperature() + "C"): a TypeError, which step 2 explains.temperaturewithout brackets shows nothing useful: it is the function itself, not a reading.- The night light flickers if the light level is close to 30: try a lower number, or hold a hand right over it.
- The order of the
ifandelifchecks 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.