micro:bit projects and workshops
Exercise 1: Measuring temperature
Exercise 2: Alarm
Welcome to the exercise "Alarm on the micro:bit", where you will learn how to make a simple detection system using a pressure sensor and a radio connection. This exercise is a perfect introduction to the basic concepts of programming micro:bit devices, as well as to the practical use of sensors and communication between devices.
Before you start, we recommend that you look at the following resources for additional information and inspiration:
- Instructions for the exercise "Pressure switch alarm" on the official micro:bit website - Learn how to make a simple alarm using a micro:bit and pressure sensors.
- Radio communication on the micro:bit (SR) - A detailed explanation of how communication between two micro:bit devices works.
Through this exercise you will develop your alarm system step by step and gain a deeper understanding of working with the micro:bit. Enjoy learning and exploring!
Task: An alarm for catching a thief
Imagine the following situation: your house is equipped with a modern security system, but you want to add another layer of protection. At the entrance to the garage you want to install an alarm that will warn you when someone tries to break in. The micro:bit device will help you design a simple but effective security system using a radio connection.
The problem we are solving:
- When a motion detector or a person presses a button on the first Micro:bit device, an alarm is activated on the second Micro:bit device, which is inside the house.
- The alarm should make a sound (buzzer) and show a visual signal on the screen so that you immediately know that an intruder is nearby.
Your task:
Make two Micro:bit devices:
- Detector: A device that detects the intruder and sends the radio message "alarm".
- Alarm: A device that receives the message, activates a sound signal and shows a visual notification.
Concrete steps for solving the problem:
- Intruder detector:
- The first Micro:bit should detect a press of button A.
- When button A is pressed, the device should send the radio message "alarm" on a specific channel.
- To make the system visually intuitive, after sending the message it shows a smiling face (it symbolizes that the message was sent successfully).
- Alarm in the house:
- The second Micro:bit should listen for radio messages on the same channel.
- When it receives the message "alarm", the device shows a surprised face on the screen, turns on the buzzer on pin P0 for one second, and then turns off the sound and clears the screen.
Materials used:
- Two Micro:bit devices.
- A buzzer connected to pin P0 of the second Micro:bit device.
- USB cables and power for both devices.
This task simulates a real alarm system and can be applied in various situations. For example, it can serve as a warning at home, as a security system for a school or as part of creative projects for young programmers.
Now get coding and make your first security alarm!
1. Hardware preparation
- Prepare two micro:bit devices.
- For Micro:bit 2, connect a buzzer or a speaker to pin P0 and GND (optional, for the sound alarm).
2. Programming Micro:bit 1
- Open the MakeCode editor (link).
- Arrange the blocks to implement the following:
- In the on start block, set the radio channel to the same number as on the other device (e.g. channel 1).
- Add the functionality: when the user presses button A, the device sends the message "alarm".
MicroPython code for micro:bit 1
from microbit import * # Import the module for controlling the Micro:bit device
import radio # Import the module for radio communication
radio.on() # Turn on the radio module
radio.config(channel=1) # Set the radio channel to 1
while True: # Infinite loop for continuously checking the state
if button_a.is_pressed(): # Check whether button A is pressed
radio.send("alarm") # Send the message "alarm" over the radio
display.show(Image.HAPPY) # Show a happy face on the screen
sleep(500) # Pause of 500 milliseconds
display.clear() # Clear the screen
3. Programming Micro:bit 2
- Also in the MakeCode editor, open a new project.
- Arrange the blocks to implement the following:
- In the on start block, set the radio channel to the same number (e.g. channel 1).
- Add the logic so that, when the device receives the message "alarm", it shows an animation and activates the sound.
MicroPython code for micro:bit 2
from microbit import * # Import the module for controlling the Micro:bit device
import radio # Import the module for radio communication
radio.on() # Turn on the radio module
radio.config(channel=1) # Set the radio channel to 1
while True: # Infinite loop for continuously listening for radio messages
message = radio.receive() # Receive a message over the radio
if message == "alarm": # Check whether the message "alarm" was received
display.show(Image.SURPRISED) # Show a surprised face on the screen
pin0.write_digital(1) # Turn on the buzzer on pin P0
sleep(1000) # Pause of 1 second while the buzzer is working
pin0.write_digital(0) # Turn off the buzzer
display.clear() # Clear the screen
4. Testing
- Turn on both micro:bit devices.
- On Micro:bit 1 press button A. Check whether Micro:bit 2 reacts by showing the animation and activating the alarm.
Extending the task
- Add an option for Micro:bit 2 to send a confirmation back to the first device when it receives the message "alarm". - Make a system with several devices on the same radio channel for more complex simulations.
Project 3: Reaction speed
Goal of the project:
Make a device that measures the user's reaction speed using a micro:bit device and a simple switch made of aluminium foil. The project includes making a physical switch, writing a program for the micro:bit and testing the user's reaction time. Task description: the user should create a system that measures the reaction time to a light signal. The system will use the micro:bit device to generate the signal and measure the time, while a switch made of aluminium foil will serve as a "button" for registering the user's response.
Materials needed:
- micro:bit device (two devices, if you want an additional display on a second micro:bit).
- A USB cable for connecting the micro:bit device to the computer.
- Cardboard.
- Aluminium foil (three pieces).
- A sponge or foam material for separating the pieces of foil.
- Wires (cables with crocodile clips).
- Scissors and glue.
Steps for building the project
1. Making the switch:
- Cut three pieces of aluminium foil of about 10x10 cm.
- Glue onto the cardboard:
- The first piece of foil (A) – connect it to pin P0 on the micro:bit.
- The second piece of foil (B) – connect it to the GND pin on the micro:bit.
- The third piece of foil (C) – put it on the opposite side of the cardboard so that it is insulated (e.g. with a sponge).
- Arrange the cardboard and the foil so that foil (C) can connect foils (A) and (B) when the cardboard is pressed.
- Put the switch on the floor or on a table – it is ready to use.
2. Programming the micro:bit device:
- The main micro:bit program:
- Initialize pin P0 as an input.
- Show a symbol (e.g. a circle) on the micro:bit screen after a random period of time (1-5 seconds).
- When the user presses the switch, measure the time between showing the symbol and activating the switch.
- Show the result (time in milliseconds) on the micro:bit screen.
MicroPython code:
from microbit import *
import random
while True:
display.clear()
sleep(random.randint(1000, 5000)) # random pause of 1 to 5 seconds
display.show(Image.SQUARE) # signal: press the switch!
start = running_time() # the moment the signal appeared (ms)
while not pin0.is_touched(): # we wait for the foil to connect P0 and GND
pass
reaction = running_time() - start
display.scroll(str(reaction)) # reaction time in milliseconds
sleep(2000)
3. Testing the device:
- Connect the micro:bit device to the computer or to an external power source.
- Put the switch under a carpet, a doormat or on a flat surface.
- When the micro:bit shows the symbol, quickly press the switch.
- Read the reaction time on the micro:bit screen.
What does this exercise involve?
In this exercise you use aluminium foil as a physical switch. The game is designed to measure your reaction when you touch or "hit" the switch made of foil. The goal is to make a simple electrical contact that the micro:bit can detect.
Connecting the pins and the aluminium foil
- Materials needed:
- A micro:bit.
- Aluminium foil (shaped into pieces).
- Crocodile clips (or wires for connecting).
- Optionally, tape for fixing.
- How is it connected?
- One piece of aluminium foil is connected to one of the digital pins of the micro:bit, e.g. P1.
- The other piece of foil is connected to the ground pin (GND).
- When you "hit" the foil (or connect it with your finger), the electric circuit between P1 and GND is closed.
What happens from the point of view of physics?
1. Closing the electric circuit
- When you touch the aluminium foil connected to P1 and the foil connected to GND, the electric circuit is closed through your body.
- Your body works as a conductor here. Although the human body has a relatively high resistance, the voltage of 3.3V (used by the micro:bit) is enough for the signal to pass.
2. Detecting the signal
- The micro:bit detects a change of state on P1. Under normal conditions, P1 can be in the HIGH state (3.3V) when it is not connected to ground.
- When you connect P1 and GND (by touching the pieces of foil), the voltage on P1 drops to 0V (LOW), because the pin is connected directly to ground.
- This change is used as the signal for detection.
3. The role of the aluminium foil
- Aluminium foil serves as a simple conductor. Its large surface makes it easy to connect with the body (hand or fingers), which makes closing the circuit easier.
- When the foil is connected to the pins, it behaves like a switch: connecting the pieces of foil simulates pressing a physical button.
How does the game work?
- The micro:bit records the moment when it detects that the electric circuit is closed (touching the foil).
- The reaction time is measured by the micro:bit recording the time interval between the starting state and the moment the switch is activated.
- You can "hit" the foil as many times as you want, and the micro:bit will record your reactions.
Additional tasks (optional)
- Display on a second micro:bit device:
- Connect two micro:bits using radio communication.
- Show the reaction time or turn on an alarm on the second device.
- Extending the functionality:
- Add more switches for a team game.
- Save the results and show the average reaction time.
Exercise 4: Measuring speed
Problem description:
Imagine that you have set up an experiment for measuring the speed of an object (for example a car, a bicycle or a runner). The goal of this task is to use two Micro:bit devices to measure the speed of an object passing between two points.
The problem we are solving:
The first Micro:bit detects the object passing the first point, while the second Micro:bit detects it passing the second point. The devices communicate over a radio connection. The speed is computed from the time the object needs to travel the distance between the points.
Task specification
- Device 1 (Start sensor):
- This Micro:bit device detects the start of the measurement.
- When button A is pressed, the device records the start moment (start_time) and sends the radio message "start".
- Device 2 (Stop sensor):
- This Micro:bit device waits for the message "start".
- When it detects the object passing (by pressing button B), it records the end moment (stop_time) and computes the total travel time.
- Based on a predefined distance (e.g. 5 metres), the device computes the speed of the object and shows it on the screen in metres per second (m/s).
- Predefined distance:
- Assume that the distance between the two points is 5 metres.
Steps for solving the task
1. Setting up device 1 (Start sensor)
Connect the first Micro:bit to the computer.
Write a program that does the following:
- Turns on the radio and sets the communication channel.
- When button A is pressed, records the current moment (
start_time) using the built-in function for measuring time. - Sends the radio message
"start"to the second Micro:bit device to mark the start of the measurement. - Shows an arrow symbol (
Image.ARROW_W) as a visual signal that the measurement has started.
2. Setting up device 2 (Stop sensor)
Connect the second Micro:bit to the computer.
Write a program that does the following:
- Turns on the radio and sets the communication channel.
- Receives the message
"start"and shows a clock symbol (Image.CLOCK) as a visual signal that the device is ready for measurement. - When button B is pressed, records the current moment (
stop_time). - Computes the total travel time as the difference between
stop_timeandstart_time. - Based on the defined distance (5 metres), computes the speed with the formula:
speed = distance / time. - Shows the computed speed on the screen in m/s.
3. Testing
- Put the first Micro:bit at the starting point and the second one at the end point (a distance of 5 metres).
- Start the program on both devices.
- Press button A on the first device to start the measurement.
- When the object passes the end point, press button B on the second device.
- The speed of the object is shown on the screen of the second device.
Exercise 5: Watering the garden
Exercise 6: Ultrasonic sensor
Check your knowledge
Open the quiz
Projects and workshops
1. How does the micro:bit recognize that two pieces of foil are connected?
2. Which MicroPython statement reads the time since the device was turned on, in milliseconds?
3. Why is a motor (pump) not powered directly from a micro:bit pin?
