Home · Academy · Robotics & Coding · Sensors and Actuators · The Temperature Sensor

The Temperature Sensor

Learn to read a temperature sensor and give a warning using a threshold.

LESSON COMPASS

What will you use this page for?

Core idea

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.

Evidence to produce

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

Control trap

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…

Next connection

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.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteThe Light Sensor
ContentStandard lesson · 1,643 words
Last updated

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.

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:

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

Lesson summary

Check questions

  1. What does a temperature sensor turn the surrounding heat into?
  2. What is the most common unit for temperature, and what is its symbol?
  3. What does "threshold" mean, and what is it used for in a temperature warning?
  4. If you want to measure a surface's temperature without touching it, which type of sensor is suitable?
  5. What are the two most important safety rules when doing a temperature experiment?

Answers

  1. Into a number we can read and compare.
  2. Degrees Celsius, with the symbol °C.
  3. 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).
  4. An infrared (contactless) sensor.
  5. 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.

Start QuizBack to Sensors and Actuators
QUESTION POOL

Reinforce this lesson with 10 questions

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