AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Sensors make decisions

Week 8 · Unit 7.2: Decomposing problems · 3 lessons
1

Sensors send numbers

Computing 7: Unit 7.2, pp. 44–81
GoalName the micro:bit’s sensors and the MicroPython that reads them, and explain why a program keeps checking.
New words
sensor · a part that measures something around it, such as light, heat, sound or movementlight level · a number from 0 (dark) to 255 (very bright) that the light sensor reportsaccelerometer · a sensor that notices movement, such as a shake or a tiltinput · data that goes into a computing device, from a button, a sensor or a keyboard
Explanation

A sensor measures the world around a device and sends the result to the program as data. The newer micro:bit has a touch logo, a temperature sensor, a microphone, an accelerometer and a light sensor. The temperature is given in degrees Celsius. The light level is a number from 0 (dark) to 255 (very bright). The program uses a question about the number to decide what to do. Because the world keeps changing, the sensor must be checked again and again, so the question sits inside while True. Everyday devices work the same way: an automatic tap checks for a hand, and a car’s parking sensor checks the distance.

SensorWhat it measuresMicroPython
touch logotouchpin_logo.is_touched()
temperaturedegrees Celsiustemperature()
microphoneloud soundmicrophone.current_event() == SoundEvent.LOUD
accelerometermovementaccelerometer.was_gesture("shake")
light sensorlight level 0–255display.read_light_level()
Worked examples
Python
1from microbit import *
2while True:
3    if temperature() > 30:
4        display.scroll("Hot")
Output
scrolls Hot while the temperature is above 30
A greenhouse owner wants a warning when it gets too hot. The program asks the temperature sensor for a number and compares it with 30.
Python
1from microbit import *
2while True:
3    if display.read_light_level() > 100:
4        display.show(Image.YES)
Output
a tick appears when the light level is above 100
A reading nook has a small sign that should show a tick when there is enough light to read. The light sensor gives a number, and the program compares it with 100.
Python
1from microbit import *
2while True:
3    if pin_logo.is_touched():
4        display.scroll("Welcome")
Output
scrolls Welcome while the logo is touched
A pet shop has a tiny welcome sign. Touching the logo makes it greet customers. The input is the touch sensor and the output is the scrolling text.
Class activity

Sensor hunt (no computer). With an adult, find five everyday devices that use a sensor, such as an automatic door, a phone that turns its screen, a hand dryer or a toy that reacts to a clap. For each one write what the sensor measures, what question the program might ask and what the output is. Then choose your favourite and write its question as “if … then …”. If you have a micro:bit and an adult to help, run the light-level program and cover the display with your hand.

Practice
1
Copy the table and write the sensor that each line of code reads: touch, temperature or light.
MicroPythonSensor
temperature()?
display.read_light_level()?
pin_logo.is_touched()?
2
The temperature is 18 degrees. Then it is 31 degrees. What does this program do each time?
Python
1from microbit import *
2while True:
3    if temperature() > 25:
4        display.scroll("Warm")
3
Why must the question about a sensor be inside while True?
4
Which micro:bit sensor would you use for each job? (a) a cup that warns you when a drink is too hot (b) a toy that reacts when you clap (c) a toy that counts how often it is moved.
(a) hot drink(b) clapping toy(c) moving toy