Overview
Can you send a ghost across the room without touching it? Let's find out!
In this project you will make a ghost that can travel between two micro:bitThe micro:bit is a tiny programmable computer, about the size of a matchbox, made for learning to code. It has two buttons, a grid of little red LED lights and built-in sensors that can detect movement, light and temperature.Full entry →. Shake one micro:bit, and a ghost appears on the other one. The ghost is the message, and the micro:bits pass it along using Radio GroupA radio group is a number that works like a secret channel. Only micro:bits with the same radio group number can send messages to each other.Full entry →.
You will write a While loopA while loop keeps repeating its instructions for as long as something stays true. The moment that thing stops being true, the loop stops.Full entry → that keeps checking for messages and shakes, and use an AccelerometerAn accelerometer is a tiny part inside the micro:bit that senses movement, like tilting, shaking or being turned over. It turns that movement into numbers your code can react to.Full entry → to notice when a micro:bit is shaken. When a message arrives, you'll store it in a VariableA variable is something that can change. Think of it like a labelled box: you pop something inside, and the label tells you what it is. You can swap what's inside whenever you like.Full entry →, then use an If statementAn if statement checks whether something is true, and only runs its instructions when it is. It's how code makes decisions.Full entry → to decide what to show on the LED MatrixThe LED matrix is the grid of little red lights on the front of the micro:bit. By switching them on and off in patterns, your code can show numbers, letters and tiny pictures.Full entry →.
Each step adds a little more to your ghost game, so take your time and test as you go.
What you'll learn
- How two micro:bits can talk to each other wirelessly
- How to use an accelerometer to detect a shake
- How to use a while loop to keep checking for messages
- How to show a picture on the LED matrix
- How code runs one step at a time
What you'll need
- 2 x micro:bit
- Python coding editor
- Micro USB cable
- 2 xBattery pack (optional)
Key words
Accelerometer
An accelerometer is a tiny part inside the micro:bit that senses movement, like tilting, shaking or being turned over. It turns that movement into numbers your code can react to.
Real-world example: A bit like the sensor in a games controller that knows when you tilt it to steer, the accelerometer notices when the micro:bit moves.
If statement
An if statement checks whether something is true, and only runs its instructions when it is. It's how code makes decisions.
Real-world example: "If it's cold, put on a coat." If it's warm, you skip the coat, the action only happens when the check is true.
Importing Libraries
A library is a ready-made bundle of code that someone else has written for you. Importing a library means bringing it into your project so you can use all its ready-made commands and features, without building them from scratch.
Real-world example: It's like borrowing a recipe book from the library. Instead of working out every recipe yourself, you bring the book in and use the recipes whenever you need them.
micro:bit
The micro:bit is a tiny programmable computer, about the size of a matchbox, made for learning to code. It has two buttons, a grid of little red LED lights and built-in sensors that can detect movement, light and temperature.
Real-world example: A bit like a pocket-sized computer you can program yourself, a micro:bit can become a step counter, a thermometer or even a simple game.
LED Matrix
The LED matrix is the grid of little red lights on the front of the micro:bit. By switching them on and off in patterns, your code can show numbers, letters and tiny pictures.
Real-world example: Like a mini scoreboard made of dots, the LED matrix lights up to spell out your step count.
Radio Group
A radio group is a number that works like a secret channel. Only micro:bits with the same radio group number can send messages to each other.
Real-world example: It’s like two walkie-talkies agreeing to use channel 30, so they only hear each other.
Variable
A variable is something that can change. Think of it like a labelled box: you pop something inside, and the label tells you what it is. You can swap what's inside whenever you like.
Real-world example: A variable called score might start at 0, then change to 1, 2 and 3 as you collect points in a game.
While loop
A while loop keeps repeating its instructions for as long as something stays true. The moment that thing stops being true, the loop stops.
Real-world example: "While it's still raining, keep the umbrella up." As soon as the rain stops, you put the umbrella down, and the loop ends.
Navigating to the Python editor
- Open your favourite web browser. We recommend either Google Chrome or Microsoft Edge.
- In the address bar, type python.microbit.org.
- Close the left-hand panel by selecting the arrow pointing left.
- Delete the example code below
from microbit import *, so you have a clean space to type in.

Import the Radio Library
- Under
from microbit import *, typeimport radio, then press Enter.
This adds the radio library, so your code can send and receive messages.

Turn On the Radio
- Type
radio.on()underimport radio, then press Enter. - Type
radio.config(group=30), then press Enter.
This turns on the radio and sets the radio group. Both micro:bits need the same group number to hear each other.

Note: If you are working with more than one pair of micro:bits, give each pair a different radio group number. Groups range from 1 to 255.
Create the While Loop
- Type
while True:underradio.config(group=30), then press Enter.
When you press Enter, the cursor moves in automatically. Everything you type from now on goes inside the while loop, so the code keeps running and checking for messages and shakes.

Create the incoming Variable
- Inside the while loop, type
incoming = radio.receive(), then press Enter.
The incoming variable is a labelled box. It stores any message the micro:bit receives. If no message has arrived, the box stays empty.

Show the Ghost When a Message Arrives
- Type
if incoming:, then press Enter. The cursor will move in automatically. - Type
display.show(Image.GHOST), then press Enter. - Press Backspace once. This moves the cursor back so the next line lines up with
if incoming:.
Picture names start with a capital I, and they must match exactly, so check the spelling carefully.

Send the Ghost When Shaken
- Type
if accelerometer.was_gesture("shake"):, then press Enter. Check that this line lines up withif incoming:, not inside it. - Type
display.clear(), then press Enter. - Type
radio.send("GHOST").
When you shake the micro:bit, the ghost clears from its screen, and the word GHOST is sent to the other micro:bit.

You have now finished the Spooky Signals code. Next, you'll download it to your micro:bits and see the ghost in action.
Download your code
Your spooky signals is ready to transfer to the micro:bit.
- Plug one micro:bit into your computer using the micro USB cable.
- Select the three little dots next to Send to micro:bit.
- Select Connect, then follow the on-screen prompts.
- Select Send to micro:bit to download the code.
- Unplug that micro:bit, then plug in the other one and repeat steps 1 to 4.
Both micro:bits need the same code, so don't skip the second one!

Test your Spooky Signals
Unplug both micro:bits from the computer, and plug in a battery pack for each one. Shake one micro:bit. Its ghost vanishes, and a ghost appears on the other micro:bit's screen. Shake the other one to send the ghost back!
It's like a game of supernatural catch. Grab a friend and have a go!
Grab a friend and have a go!
Try it yourself
Challenge: Ghost Timer
Goal: Make the ghost disappear after 3 seconds.
Hint: Under display.show(Image.GHOST), type sleep(3000) at the same indent, then press Enter. On the next line, type display.clear(). Each 1000 is one second, so 3000 is three seconds.
Good to know: If the ghost doesn't vanish, check that both lines are indented to the same level as display.show(Image.GHOST).
Stuck? Quick fixes
| Problem | Try this |
|---|---|
| The editor shows an error in red | Check your spelling and capital letters, such as Image.GHOST. Check that the lines inside each if or while are indented. |
| The ghost doesn't appear on the other micro:bit | Make sure both micro:bits have the code, and that both use group 30. Keep them close together. |
| Shaking doesn't send a ghost | Check that the if accelerometer.was_gesture("shake"): line lines up with if incoming:, and that it's inside while True:. |
| The micro:bit doesn't show up when you connect | Use a data USB cable, not a charge-only one. Press the reset button on the back of the micro:bit, then select Connect again. |
| Another pair's ghost appears on your micro:bit | Give each pair a different radio group, such as 30 for one pair and 31 for another. |
