Micro:bit Lesson — Temperature and Light Sensors (MicroPython)

Read and display real-world data using temperature() and display.read_light_level().

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

SensorPythonRange / UnitNotes
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 > 28 and light < 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)

  1. What unit does temperature() return?
  2. What is the value range for display.read_light_level()?
  3. Why should sensor-reading code usually run inside a while True: loop?

Teacher Notes

The name of the file will be:
PX_lastname_Temperature

Files to save on Google Drive under your class and submit to Google Classroom

  1. PX_lastname_Temperature.png — Screenshot inside Python editor showing your code.
  2. PX_lastname_Temperature.py — Your Python source file.
  3. PX_lastname_Temperature.txt — Plain-text copy of your code.
  4. PX_lastname_Temperature.hex — Hex file to run on the physical micro:bit.
  5. PX_lastname_Temperature.mp4 — Short demo video of the program running on the board.

Submission requirements