One-sentence summary
A temperature sensor turns the heat around it into a number, and we can read that number and make a decision based on a threshold.
Why does it matter?
We can tell whether a room is warm or cold with our hands, but a robot or a microcontroller cannot feel it. We have to give it a number it can measure. That is exactly what a temperature sensor does: it turns the heat of the air or of a surface into a number, measured in degrees.
In the previous lesson we saw that a light sensor turns the brightness around it into a number. A temperature sensor applies the same idea to a different quantity. The quantity you read changes, but the logic stays the same: the sensor reads, the program decides, the output responds.
Temperature measurement is everywhere in daily life. A thermostat heats a home, a fridge keeps its inside cold, a phone protects itself when it gets too hot. Under all of these there is a temperature sensor and a simple rule.
What a temperature sensor does and how it works
What does it measure?
A temperature sensor measures how hot or cold the air or surface it touches is. It gives us the result as a number. The unit of this number is usually degrees Celsius (°C). For example, room temperature is about 22 °C, the point where ice melts is 0 °C, and boiling water is around 100 °C.
How it works (the simple idea)
Most electronic temperature sensors rely on a single idea: the electrical behaviour of some materials changes with heat. For example, the resistance a material offers to current may go up or down as it warms. The sensor measures this small change and turns it into a temperature number.
You do not have to memorise this inner detail. The only thing you need to know is this: the sensor turns a physical change inside it into a clean number that you can read.
Types (conceptual)
There are different sensors for different jobs. You do not need to use them all; it is enough to know your options.
- Thermistor: Small and cheap. Its resistance changes as it heats up. Very common in simple projects.
- Digital sensor (for example DS18B20, DHT11/DHT22): Gives the temperature directly as a number; some also measure humidity. Easy to wire and read.
- Infrared (contactless) sensor: Measures a surface's temperature from a distance, without touching it. Useful for checking a hot surface without putting your hand on it.
- Built-in sensor on the board: Some micro:bit and development boards already have a small temperature sensor, so you can experiment without wiring any extra part.
How do we read the value?
The board has a ready-made command that reads the sensor for you. You call the command, and it returns the current temperature as a number. You store that number in a variable and use it however you like.
Reading the built-in sensor on a micro:bit looks like this:
from microbit import *
while True:
temp = temperature() # read the degrees
display.scroll(temp) # show it on screen
sleep(2000) # wait 2 seconds
Here the temperature() command gives us a number. If it returns 24, that means "about 24 degrees Celsius".
Making decisions with a threshold
What is a threshold?
Reading a single number is not enough on its own; the real work is comparison. We set a limit and call it a threshold. When the measured value crosses the threshold, we make something happen. This is the "If … then" condition meeting sensors.
Say our threshold is 30 °C. The rule is: *"If the temperature goes above 30 degrees, sound the buzzer."*
In pseudocode:
Start
Repeat:
temp = read the sensor
If temp > 30
sound the buzzer (too-hot warning)
Otherwise
keep the buzzer silent
Wait a short time
micro:bit example: a hot-warning
Let us turn the same idea into real code. When the temperature goes above 30 degrees, show a warning on the screen and give a beep:
from microbit import *
import music
THRESHOLD = 30
while True:
temp = temperature()
if temp > THRESHOLD:
display.show(Image.NO) # warning sign
music.pitch(880, 300) # short beep
else:
display.show(Image.HAPPY) # everything normal
sleep(1000)
In this program three parts work together: the sensor reads, the condition decides, and the output (screen and sound) responds. If you want to change the threshold, you only change the value of THRESHOLD; everything else stays the same.
Two everyday examples
Example 1: A thermostat
A thermostat at home keeps reading the room temperature. You set a target, for example 22 °C. The thermostat's rule is simple: *"If the room drops below 22 degrees, turn the heater on; if it rises above, turn it off."* Here 22 °C is a threshold. It is the same "If the temperature is …" rule you wrote, just inside a bigger device.
Example 2: The fan in a computer or phone
Inside a computer, next to the processor, there is a small temperature sensor. When the processor gets too hot, the sensor notices and the fan speeds up, or the device slows itself down. The goal is to keep the temperature below a safe threshold. It is the same logic as your buzzer example: when the threshold is crossed, a response is triggered.
Mini practice
Imagine a "greenhouse alert system". A plant greenhouse should get neither too cold nor too hot. Write a rule with two thresholds:
- If the temperature drops below 10 °C, give a "TOO COLD" warning.
- If the temperature rises above 35 °C, give a "TOO HOT" warning.
- In between, show "NORMAL".
Try it first in pseudocode:
temp = read the sensor
If temp < 10
show "TOO COLD"
Otherwise if temp > 35
show "TOO HOT"
Otherwise
show "NORMAL"
When you are ready, turn this into real code on a micro:bit. You can show the warnings on screen with commands like display.scroll("COLD"). Try different thresholds, and warm the sensor with your hand (by cupping it) to watch how the value changes.
Common mistakes
Confusing the unit
If you do not know which unit the sensor's number is in, your rule becomes meaningless. Before you write a threshold, check that it works in Celsius. The number 30 makes sense if it is 30 °C; but if you assume the sensor gives a different unit or a raw value, your warning will fire at the wrong time.
Making the threshold too tight
If you set the threshold exactly equal to the measured value (for example == 24 instead of > 24), the sensor might read 25 one moment and drop to 23 the next, and your rule may never match. The temperature value keeps wobbling slightly, so greater than / less than comparisons are safer than equality.
Forgetting the board's own heat
A micro:bit's built-in sensor sits close to the board's own processor, so it can read a little differently from the true room temperature. This is not a fault; it is about where the sensor is. If you need an accurate reading, an external sensor is the better choice.
Safety note
- Do not touch hot surfaces. When you test a temperature sensor near a hot object, do not put your hand on that object. The risk of a burn is real. If you want to measure a hot surface, use a contactless (infrared) sensor or ask an adult for help.
- Use only low-voltage sources. All experiments are done with low-voltage sources such as a battery, USB, micro:bit or Arduino. Never connect the sensor to mains (wall socket) electricity, and never bring it near the hot or live parts of an oven, heater or stove.
- Do not push the sensor beyond its range. Every sensor has a temperature range it can handle. Putting it directly into fire, boiling water or very hot surfaces will damage it. Temperature experiments should always be done together with an adult.
- Keep it away from liquids. A wet electronic part can break; do not dip it in water.
Lesson summary
- A temperature sensor turns heat into a number we can read; the unit is usually degrees Celsius (°C).
- The working idea is simple: the electrical behaviour of some materials changes with heat, and the sensor measures that change.
- Thermistors, digital sensors, infrared sensors and the board's built-in sensor are the main types.
- By setting a threshold and using the rule "if the temperature crosses the threshold, warn", we can make automatic decisions.
- Not touching hot surfaces and using only low-voltage sources are the core safety rules.
Check questions
- What does a temperature sensor turn the surrounding heat into?
- What is the most common unit for temperature, and what is its symbol?
- What does "threshold" mean, and what is it used for in a temperature warning?
- If you want to measure a surface's temperature without touching it, which type of sensor is suitable?
- What are the two most important safety rules when doing a temperature experiment?
Answers
- Into a number we can read and compare.
- Degrees Celsius, with the symbol °C.
- A threshold is a limit value we choose. When the measured temperature crosses this limit, it makes the program respond (for example, sound the buzzer).
- An infrared (contactless) sensor.
- Not touching hot surfaces, and using only low-voltage sources (battery/USB/micro:bit/Arduino); doing the experiments together with an adult.
Source and verification note
For “The Temperature Sensor”, verification focuses on whether the relationship between What a temperature sensor does and how it works and How it works (the simple idea) remains consistent across examples. Sensor readings can change with the model, supply voltage and environment. Thresholds in the lessons are therefore examples; a real project should use a measurement table and calibration.
Next lesson
The Distance Sensor: We will learn to measure how far away an object is using sound waves, and to make a robot stop before it hits an obstacle.