Sensors (Inputs) 1, 2
Lesson (25 mins):
Classroom Code / Project: Inputs, Inputs + LED , Light LED Strip with Inputs
Equipment: (1 for each Trainee)
Micro:bit
computer
USB cord
Skills
Sense button press
Accelerometer
Microphone
Capacitive touch
Magnetometer
Light sensor
Temperature
Keywords
events
sensor
input
output
What is a sensor?
The micro:bit has a bunch of built-in sensors.
A sensor is kind of what it sounds like—it’s an electronic component that works kind of like our own senses to take in information from outside the micro:bit.
For instance, in the same way that my ears can sense sound, the micro:bit has a microphone to sense sound. These sensors, and any other information that goes into the micro:bit (like a message over the radio or a button press), are called inputs.
How to code inputs?
Facilitator guides the students through adding the following input blocks one at a time (and showing them where the Input blocks are), uploading them to their micro:bits to test them out, and explaining how each one works.
The full code can be found here: Inputs (also shown in picture)
Remember to use terminology keywords!
“On Button A Press”
This input tells it the micro:bit to do whatever is in the code block when Button A is pressed. In this case, when button A is pressed, the micro:bit will show a smiley face on the LED matrix, wait 500 milliseconds (half a second), then clear the screen.
If you click on the “A”, there’s a dropdown menu to choose other inputs — the B button or the A+B Button together.
Facilitator guides student through writing the first code block and uploading it to their micro:bit to test. This is a good opportunity to encourage the students to try the other buttons as well.
“On shake”
One of the sensors the micro:bit has built in as an accelerometer. This is an electronic component that senses movement (acceleration, specifically). The accelerometer can sense when you shake the micro:bit and use that as an input.
If you click on “shake”, there’s a dropdown menu to choose other accelerometer-based inputs. The accelerometer can sense when you rotate the micro:bit in a certain direction, like upside-down or tilting it right to left. It can also sense a certain amount of acceleration, like 3G or 6
Facilitator guides student through writing the next code block and uploading it to their micro:bit to test. This is a good opportunity to encourage the students to try the accelerometer-based inputs as well.
“On loud sound”
Another sensor the micro:bit has built in is a microphone, to sense sound.
If you click on “Loud”, there’s a dropdown menu to choose “loud” or “quiet” sounds.
In the Sound class, you’ll learn how to change the micro:bit to react to sounds of different loudness, or even to measure how loud a sound is. In a loud environment like a classroom setting, you might want to keep it on “Loud” or it’ll react to everything.
Facilitator guides student through writing the next code block and uploading it to their micro:bit to test. They can try blowing onto the microphone, clapping, shouting, etc.
“On pin P0 pressed”
The 0, 1, 2 pins (facilitator should show what the “pins” refer to on the micro:bit) as well as the logo can sense touch.
If you click on “P0”, there’s a dropdown menu to choose other pins. If you want to use the Logo as a touchpoint, click on Inputs on the left-hand menu, scroll to the bottom, and select “On Logo Pressed”.
Facilitator guides student through writing the next code block and uploading it to their micro:bit to test. This is a good opportunity to encourage the students to try using aluminum foil or other conductive materials to activate the touch sensor as well.
Open-Ended Exploration
Open-ended exploration of using different inputs to create outputs such as lighting an LED strip or sending a message via radio.
Some examples can be found here: Inputs + LED, Light LED Strip with Inputs
For Part 1, we used the inputs to turn on the LED screen (the output). But we can use these inputs to trigger other outputs as well, such as turning on the LED strip or, if you’ve taken the Broadcasting course, to send messages.