Grade by GradeEssential student suppliesSign in
Applied Design, Skills, and Technologies · Design and Make: Grade 5 Designers, Makers and Coders

Coding with Sensors: Forever, If and Else

BC ADST 5: use familiar tools BC ADST 5: choose technologiesAbout 45 minutes
Learning goals
  • I can explain input, process and output in a program.
  • I can use a sensor, a forever loop and if-else to make a program react.
  • I can trace a program with different sensor values and fix a bug.

Words to know

sensor
A part that measures something, like temperature, light or sound.
input
Information that goes into a program, often from a sensor or button.
output
What the program does, like showing a message or making a sound.
if-else
A code block that does one thing if a condition is true and something else if it is false.
condition
A test that is true or false, like temp > 25.

Programs that sense the world 🌡️

You have coded with loops, events, if-then and variables. Now your code will react to the real world using sensors.

Many small coding boards (such as a micro:bit) have sensors built in:

  • a temperature sensor (°C),
  • a light sensor (a number from dark to bright),
  • a tilt sensor and buttons.

Every program like this has three parts: input (what the sensor measures), process (the decision) and output (what happens).

INPUTtemperaturesensorPROCESSif temp > 25then... else...OUTPUTscreen shows amessageThe forever loop repeats this every second.
Input, process and output in the greenhouse program.

The greenhouse helper

The Grade 5 class has a small greenhouse for spring seedlings. If it gets too warm inside, the seedlings can wilt. The class wants a sign that says when to open the vent. They use a forever loop, so the board keeps checking, and if-else, so it does one thing or the other.

forever
set temp to temperature
if temp > 25 then
show "OPEN VENT"
else
show "OK"
pause 1 second

The greenhouse helper. It checks the temperature every second.

temp (°C)Is temp > 25?Screen shows
18falseOK
25falseOK
26trueOPEN VENT
31trueOPEN VENT
Tracing the program with four temperatures. Notice that 25 is not greater than 25.
Worked example

Fixing a bug

The class wanted the sign to say OPEN VENT when the temperature is 25 °C or warmer. At exactly 25 °C, the board showed OK. Why, and how can they fix it?

  1. Trace it: temp = 25. Is 25 > 25? No, 25 is equal, not greater. So the else part runs and shows OK.
  2. The condition doesn't match the goal. The goal says "25 or warmer", which means greater than or equal to.
  3. Change the condition to temp ≥ 25 (in many block languages: temp >= 25).
  4. Test again with 24, 25 and 26: 24 → OK, 25 → OPEN VENT, 26 → OPEN VENT. ✔

The bug was using > instead of ≥. Always test the edge value (the number right on the line) to catch this kind of bug.

Test the edges

When your code compares numbers, test a value just below, exactly on and just above the line. Most bugs hide right at the edge.

Hands-on activity

Build a night light (or plan one on paper)

  1. Goal: a light that turns on when the room gets dark.
  2. Write the code: forever, if light level < 50 then turn the lights on, else turn the lights off.
  3. If you have a coding board, try it and cover the light sensor with your hand. If not, trace it on paper for light levels 10, 49, 50 and 200.
  4. Name the input, the process and the output.
  5. Change one thing: what if you want the light on at 50 too?
Joke

Why did the computer feel cold? 🥶

It left its Windows open!

Let's practise

1

What is a sensor?

2

In the greenhouse program, what is the input? What is the output?

3

Trace the greenhouse program (temp > 25) with temp = 22. What does it show?

4

Trace it with temp = 28. What does it show?

5

Why does the program use a forever loop?

6

What does the else part do?

More practice

1

Code: if light level < 50 then lights on, else lights off. Light level = 50. Are the lights on or off?

2

Same code, light level = 12. On or off?

3

You want a sound when the temperature is 0 °C or colder. Write the condition.

4

What three values should you test for the condition temp > 25?

5

Is "temp > 25" a condition? Explain.

Check your understanding

Quiz

1.

In a program, what is a sensor reading?

2.

Code: if temp > 25 then show "OPEN VENT" else show "OK". What shows when temp = 25?

3.

Why put the sensor check inside a forever loop?

4.

The goal is "on at 10 or less". Which condition is correct?

For parents and teachers

How to help at home: Free tools such as MakeCode (makecode.microbit.org) let your child code a virtual board with a simulated temperature and light sensor in a web browser, no device needed. Ask them to explain what their code does at different values.

Common mistakes: Mixing up < and >, forgetting that "greater than" doesn't include the number itself, leaving out the forever loop so the code checks only once, and not testing the edge values.