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:
- A value near 0: the surroundings are dark.
- A value near 255: the surroundings are bright.
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.
- Open a new MakeCode project.
- Inside
forever, build a condition: iflight level < 40thenshow icon star, elseclear screen. - In the
on button A pressedblock, addshow number (light level). - Connect the board to the computer and upload the program.
- Slowly bring your hand over the board and watch whether the threshold works.
- 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
- The micro:bit's own sensors run safely on low voltage. Even so, if you connect an external light sensor, temperature sensor or battery, do it together with an adult.
- If you want to warm the board for a measurement, warm it only with your hand; do not use fire, a stove, a kettle or any hot surface.
- Never put the micro:bit in water or hold it with wet hands.
- Use the battery pack only with suitable batteries, and connect the poles (+ / −) the right way.
Lesson summary
- The micro:bit measures temperature from its chip and light from its LED screen, both approximately.
- Temperature comes in °C, and the light level comes as a number between 0 and 255.
- We read the values with ready-made blocks in MakeCode, and with
temperature()anddisplay.read_light_level()in MicroPython. - Using a threshold and a condition, we make decisions such as "show a pattern when the light drops."
- The readings are not exact; you need to observe and tune the threshold for the surroundings.
Check questions
- What temperature does the micro:bit actually measure to work out its reading?
- What number range does the light level use, and which end shows darkness?
- What is the name of the MicroPython function that reads the light level?
- What is a "threshold" for? Explain in one sentence.
- Why can a light measurement be misleading when a bright pattern is on the screen?
Answers
- Instead of a separate sensor, it measures the temperature of the main processor chip; this value is approximate to the room temperature.
- It comes between 0 and 255; a value near 0 shows that the surroundings are dark.
display.read_light_level().- A threshold is the limit number at which a sensor value triggers a decision (for example, "if the light drops below 50").
- 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.