Home · Academy · Robotics & Coding · micro:bit · Measuring Temperature and Light

Measuring Temperature and Light

Learn to read the micro:bit's temperature and light sensing and decide using a threshold.

LESSON COMPASS

What will you use this page for?

Core idea

Using the sensors inside the micro:bit, we can read the surrounding temperature and light approximately, show these values on the screen and make simple decisions with a threshold (a limit value).

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

Thinking the reading is exact The micro:bit's temperature and light values are approximate . Do not treat them as a precise thermometer or light meter. They are great for comparison and thresholds, not for exact measurement. Trying to read light while the screen is on Because the light measurement uses the LED screen,…

Next connection

Communicating with Radio: Two micro:bits exchanging messages and sensor values wirelessly over radio.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteThe Accelerometer
ContentStandard lesson · 1,579 words
Last updated

One-sentence summary

Using the sensors inside the micro:bit, we can read the surrounding temperature and light approximately, show these values on the screen and make simple decisions with a threshold (a limit value).

Why does it matter?

In the previous lesson we saw that the micro:bit senses movement and tilt with its accelerometer. The board does not only feel motion; it can also detect the temperature and light around it. Reading these two values is an easy way for the board to "notice" what is happening nearby.

In the Electronics module we built a light sensor (LDR) and a temperature sensor as separate parts. Here is the good news: the micro:bit can make both of these measurements without any extra parts, using circuits already inside it. So we can experiment quickly without wiring, then move on to external sensors later if we want.

By the end of this lesson you will know how to read a sensor value, show it on the screen and make decisions with a threshold, such as "if the light has dropped, do this." This is the first step toward projects like an automatic night light or a temperature warning.

How does the micro:bit sense its surroundings?

Temperature: really the chip's temperature

The micro:bit does not have a separate temperature sensor part. Instead, the board measures the temperature of its main processor chip. Because the chip slowly reaches the same temperature as the room around it, this value is approximately equal to the room temperature.

For that reason the micro:bit's temperature reading is conceptual: it is not as precise as a real thermometer, and if the chip heats up a little the value can read a few degrees high. Even so, it is good enough to answer "did it get warmer or cooler?" The value comes as a whole number in degrees Celsius (°C).

Light: the LED screen's second job

The micro:bit does not have a separate light sensor either. The interesting part is that the 5×5 LED screen is briefly used like a light sensor. LEDs normally give off light, but for a very short moment they can be run in reverse and measure the light falling on them.

The board turns this measurement into a number for us. The light level is a value between 0 and 255:

So it is not an exact "lux" measurement but a gradual "how bright is it?" value. This is a nice example of an analog reading: light does not change all at once, it rises and falls slowly.

Reading values and showing them on the screen

The first step in using a sensor is seeing its value. The micro:bit's scrolling text is perfect for this.

With MakeCode

In MakeCode we use ready-made blocks. The block sequence below scrolls the temperature on the screen again and again:

forever
  show number (temperature °C)
  pause 0.5 seconds

Showing the light level follows the same idea:

forever
  show number (light level)
  pause 0.5 seconds

The "show number" block writes the value across the screen from left to right. The pause block adds a short gap so the numbers do not blur together.

With MicroPython

We can write the same idea in text-based Python. The logic is exactly the same; only the way we write it changes:

from microbit import *

while True:
    display.scroll(temperature())
    sleep(500)

temperature() gives the chip's temperature in °C. For light we use the display.read_light_level() function:

from microbit import *

while True:
    display.scroll(display.read_light_level())
    sleep(500)

display.scroll(...) shows the value by scrolling it, and sleep(500) waits for 500 milliseconds (half a second).

Making decisions with a threshold

Seeing a value is nice, but the real work is done by the threshold. A threshold is a number that lets us say "if it crosses this limit, do this." Here we use the condition structure we remember from the Algorithms lesson.

Example 1: An LED pattern when the light drops (night light)

Goal: When the surroundings get dark, the micro:bit shows a pattern; when it is bright, the screen turns off. Just like a street lamp.

MakeCode block sequence:

forever
  if <light level < 50> then
    show icon heart
  else
    clear screen

The same idea in MicroPython:

from microbit import *

while True:
    if display.read_light_level() < 50:
        display.show(Image.HEART)
    else:
        display.clear()
    sleep(200)

Here 50 is the threshold we chose. When you cup your hand over the board and reduce the light, a heart appears; when you take your hand away, the screen clears. You may need to change the threshold number to match how bright your room is.

Example 2: A temperature threshold (room/greenhouse warning)

Goal: When the temperature rises above a certain degree, the board gives a warning. You can think of it as a mini "greenhouse thermometer" next to a plant.

MakeCode block sequence:

forever
  if <temperature °C > 30> then
    scroll "HOT"
  else
    show icon yes

The MicroPython version:

from microbit import *

while True:
    if temperature() > 30:
        display.scroll("HOT")
    else:
        display.show(Image.YES)
    sleep(500)

Because the chip warms up a little, there can be a few degrees of drift from the real room temperature. Keep this in mind when choosing the threshold, and observe the value by scrolling it first.

Mini practice

Let's build a "smart desk lamp." When the light drops the board shows a star, and when it gets bright the star goes off; also, pressing button A shows the current light value.

  1. Open a new MakeCode project.
  2. Inside forever, build a condition: if light level < 40 then show icon star, else clear screen.
  3. In the on button A pressed block, add show number (light level).
  4. Connect the board to the computer and upload the program.
  5. Slowly bring your hand over the board and watch whether the threshold works.
  6. Make the threshold (40) larger or smaller for your room to find the best value.

If you like, rewrite the same program in MicroPython and compare the two versions. The logic should stay the same; only the form should change.

Common mistakes

Thinking the reading is exact

The micro:bit's temperature and light values are approximate. Do not treat them as a precise thermometer or light meter. They are great for comparison and thresholds, not for exact measurement.

Trying to read light while the screen is on

Because the light measurement uses the LED screen, a reading can be misleading when a bright pattern is always showing. Taking the measurement in a moment when the screen is blank gives a more accurate result.

Never tuning the threshold

A threshold that works in one room may not work in another. Light and temperature depend on the surroundings, so you should observe the value first and choose the threshold to match.

Forgetting the pause/sleep

If you scroll continuously with no gap, the numbers become unreadable. A short pause makes it easier to follow the values with your eyes.

Safety note

Lesson summary

Check questions

  1. What temperature does the micro:bit actually measure to work out its reading?
  2. What number range does the light level use, and which end shows darkness?
  3. What is the name of the MicroPython function that reads the light level?
  4. What is a "threshold" for? Explain in one sentence.
  5. Why can a light measurement be misleading when a bright pattern is on the screen?

Answers

  1. Instead of a separate sensor, it measures the temperature of the main processor chip; this value is approximate to the room temperature.
  2. It comes between 0 and 255; a value near 0 shows that the surroundings are dark.
  3. display.read_light_level().
  4. A threshold is the limit number at which a sensor value triggers a decision (for example, "if the light drops below 50").
  5. Because the light measurement uses the LED screen, the value read while the screen is full of a bright pattern may not reflect the real surrounding light accurately.

Source and verification note

For “Measuring Temperature and Light”, verification focuses on whether the relationship between How does the micro:bit sense its surroundings? and Light: the LED screen's second job remains consistent across examples. MakeCode and MicroPython names can vary slightly by version. Test in the simulator first; when external components are connected, check the board’s pin and voltage limits separately.

Next lesson

Communicating with Radio: Two micro:bits exchanging messages and sensor values wirelessly over radio.

Start QuizBack to micro:bit
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.