Objective
Students will read temperature (°C) and ambient light (0–255) from the BBC micro:bit and display values on the LED matrix.
MicroPython
Sensors
Loops
What You’ll Learn
- Use
temperature()to read Celsius temperature. - Use
display.read_light_level()to read ambient light (0–255). - Store readings in variables and display them.
- Run continuous monitoring with
while True:.
Sensor Reference
| Sensor | Python | Range / Unit | Notes |
|---|---|---|---|
| Temperature | temperature() |
Integer °C | Reads from the micro:bit’s on-board sensor (approximate ambient temp). |
| Light Level | display.read_light_level() |
0–255 | Higher numbers ≈ brighter environment. |
Tip: Values are approximate and influenced by hand position, device case, and ambient airflow/lighting.
Example Program
This program alternates between showing temperature (°C) and light level on the LED display.
from microbit import *
while True:
temp = temperature() # read temp in Celsius (int)
light = display.read_light_level() # read brightness (0–255)
# show temperature
display.scroll("T:" + str(temp) + "C")
sleep(2000)
# show light value
display.scroll("L:" + str(light))
sleep(2000)
How to run: Open the MicroPython editor (or Mu), paste the code, flash to the micro:bit. Watch the LED display scroll values.
Student Challenges - Must complete this
Challenge A — Reaction Display
- If light level < 60 → show a sad face.
- Else → show a happy face.
from microbit import *
while True:
light = display.read_light_level()
if light < 60:
display.show(Image.SAD)
else:
display.show(Image.HAPPY)
sleep(400)
Challenge B — Temperature Warning
- If
temp > 28→ flash “HOT!”. - If
temp < 18→ flash “COLD!”.
from microbit import *
HOT = 28
COLD = 18
while True:
t = temperature()
if t > HOT:
display.scroll("HOT!")
elif t < COLD:
display.scroll("COLD!")
else:
display.show("-") # comfortable
sleep(800)
Challenge C — Light-as-Dots
Map light level to a number of dots (0–5) and show that many pixels.
from microbit import *
def dots(n):
# build a simple left-to-right row image with n bright pixels (0–5)
n = max(0, min(5, n))
row = [9 if i < n else 0 for i in range(5)]
return Image(":".join(
["".join(str(p) for p in row)] * 5
))
while True:
lvl = display.read_light_level() # 0..255
count = int((lvl / 255) * 5 + 0.5) # 0..5
display.show(dots(count))
sleep(350)
Extension Ideas
- Combine sensors: show HOT + DARK alert when
t > 28andlight < 50. - Log values to serial (use
print()) and graph in Mu. - Add A/B buttons to toggle between temperature and light views.
Check for Understanding (Exit Ticket)
- What unit does
temperature()return? - What is the value range for
display.read_light_level()? - Why should sensor-reading code usually run inside a
while True:loop?
Teacher Notes
- Accuracy: The temperature sensor reads board temperature; allow ±2–3 °C variance from room temp.
- Lighting: Shielding the matrix with a finger or turning classroom lights on/off will clearly change readings.
- Differentiation: Offer numeric thresholds to some students and mapping/visualization (Challenge C) to others.
- Troubleshooting: If values don’t update, confirm the program is still running and not stuck in a long blocking call.
The name of the file will be:
PX_lastname_Temperature
Files to save on Google Drive under your class and submit to Google Classroom
PX_lastname_Temperature.png— Screenshot inside Python editor showing your code.PX_lastname_Temperature.py— Your Python source file.PX_lastname_Temperature.txt— Plain-text copy of your code.PX_lastname_Temperature.hex— Hex file to run on the physical micro:bit.PX_lastname_Temperature.mp4— Short demo video of the program running on the board.
Submission requirements
- Show the real micro:bit hardware running your program to Mr. Cusack.
- Get the secret code word after you demo.
- Include that exact code word in your Google Classroom submission comments.