MicroPython — Radio projects and games

Lesson 11 of 17 · MicroPython and the micro:bit

Now that you know how micro:bit devices can communicate with each other, it is time to use that knowledge in practical projects and games. This lesson shows how to combine the radio, the LED matrix and the buttons for interesting mini projects.


What does radio.config(group=...) mean?

When we use radio communication on micro:bit devices, it is very important to set the group.


radio.config(group=23)

Why is the group important?

The micro:bit radio works like a wireless channel. Every group is a separate communication channel.

  • If two devices have the same group → they can communicate.
  • If they have different groups → they will not receive each other's messages.

An example from the classroom

If several teams work in the same room, each team should use a different group number so that the messages do not get mixed up.

For example:


radio.config(group=1)   # Team 1
radio.config(group=2)   # Team 2
radio.config(group=3)   # Team 3

Which group number can I use?

The group number can be from 0 to 255. The only important thing is that all the devices that should communicate use the same number.

If radio communication does not work, first check whether the devices are set to the same group.

What does radio.config(power=...) mean?

Besides the group, we can also set the strength of the radio signal.


radio.config(power=7)

What does power mean?

The parameter power sets the strength of the radio signal, that is, how far a message can travel.

  • 0 → the lowest strength (a shorter range, less energy use)
  • 7 → the highest strength (the longest range)

When should you use more power?

  • When the devices are far apart
  • When there are more obstacles (walls, furniture)
  • At competitions in a bigger space

When should you use less power?

  • When the devices are close
  • When you want to use less battery
  • When several teams work in the classroom

An example of a complete configuration


radio.on()
radio.config(group=23, power=6)

This sets the communication group and the signal strength. All the devices that should communicate must have the same group.

1. A radio counter


One device sends numbers, and the other shows them. Ideal for scoring or competitions.



# Device 1: sends numbers
from microbit import *
import radio

radio.on()
radio.config(group=1)

number = 0
while True:
    if button_a.was_pressed():
        number += 1
        radio.send(str(number))
        display.show(number)
        sleep(500)


# Device 2: receives and shows the numbers
from microbit import *
import radio

radio.on()
radio.config(group=1)

while True:
    message = radio.receive()
    if message:
        display.show(int(message))

2. The "Hot Potato" game


The players pass on the "hot potato" by pressing a button. If they hold it longer than a certain time, the LEDs show a losing symbol.



# a player's micro:bit
from microbit import *
import radio
import time

radio.on()
radio.config(group=1)

hot = False
start_time = 0

while True:
    if button_a.was_pressed() and not hot:
        radio.send("HOT")
        hot = True
        start_time = running_time()
        display.show(Image.SQUARE)

    message = radio.receive()
    if message == "HOT" and not hot:
        hot = True
        start_time = running_time()
        display.show(Image.SQUARE)

    if hot and running_time() - start_time > 5000:
        display.show(Image.SAD)
        hot = False
        radio.send("HOT")

3. Tips for further projects


  • Use radio.config(group=number) to make several teams
  • Combine the LED matrix with radio messages for animations
  • Try making mini games with scoring, counters and time limits
  • Always test the messages between two devices before you make a complex game

✔ Here you can also use MakeCode blocks for a first test, and then move to MicroPython for more control and flexibility.




4. An advanced project: radio reaction (a reflex game)


Two micro:bit devices compete to see who reacts faster. One device sends the signal "GO", and the other must press the button as fast as possible. The result (the reaction time) is sent back.


Device 1 – The referee


from microbit import *
import radio
import random

radio.on()
radio.config(group=1)

while True:
    sleep(random.randint(2000, 5000))
    radio.send("GO")
    start = running_time()

    while True:
        reply = radio.receive()
        if reply:
            reaction = int(reply)
            display.scroll("Res: " + str(reaction))
            break

Device 2 – The player


from microbit import *
import radio

radio.on()
radio.config(group=1)

while True:
    message = radio.receive()

    if message == "GO":
        start = running_time()

        while not button_a.was_pressed():
            pass

        reaction = running_time() - start
        radio.send(str(reaction))

✔ This combines:

  • radio communication
  • measuring time (running_time())
  • loops with conditions
  • converting a string → a number



5. A project: radio + accelerometer (tilt control)


One micro:bit sends tilt data (the x axis), the other uses it to move a dot across the LED matrix.


Transmitter


from microbit import *
import radio

radio.on()
radio.config(group=1)

while True:
    x = accelerometer.get_x()
    radio.send(str(x))
    sleep(200)

Receiver


from microbit import *
import radio

radio.on()
radio.config(group=1)

position = 2

while True:
    message = radio.receive()

    if message:
        x = int(message)

        if x > 300:
            position += 1
        elif x < -300:
            position -= 1

        position = max(0, min(4, position))

        display.clear()
        display.set_pixel(position, 2, 9)

This is the basis for making:

  • a wireless controller
  • mini racing games
  • navigation games



Mini challenge


Try to make a game in which:

  • micro:bit A sends the temperature
  • micro:bit B shows Image.SKULL (hot) if the temperature is > 30
  • otherwise it shows Image.HAPPY

An extra challenge:

  • send both the temperature and the tilt in the same message (for example "28,150")
  • separate the data with split(",")

Hidden mini quiz — radio projects

Check your knowledge

Mini quiz: radio communication

1. Why is str() used when sending numbers?




2. Which function measures time in milliseconds?




3. How do you keep the position of the dot between 0 and 4?








A teaching tip


If you work in a classroom:

  • First test simple sending of text
  • Then move on to numbers
  • Finally combine several pieces of data in one message
  • Explain to the students the difference between a string and a number

✔ The next step is making more complex team games, with scoring, time limits and combining several sensors.

The big final project — A mini multiplayer game


In this project you make a real multiplayer game between several micro:bit devices.

The game is called: "Radio Arena – Hunting the signal"


The goal of the game

  • One micro:bit is the server (the arena)
  • The others are players
  • The server sends a "target signal" at a random moment
  • The players must react as fast as possible
  • The fastest one gets a point

What do you learn?

  • radio communication
  • a mini message protocol
  • identifying players (ID)
  • measuring time
  • scoring logic

This is a final project that combines everything you have learned so far.

1. A mini radio protocol


So that several devices can communicate, we introduce a simple protocol:

MESSAGE_TYPE:VALUE

Examples:

  • START
  • GO
  • WIN:2 → player 2 has won
  • POINT:3 → player 3 gets a point

This lets us extend the game later without changing the structure.

2. The server (the arena controller)



from microbit import *
import radio
import random

radio.on()
radio.config(group=7)

players = {}
round_number = 0

while True:
    sleep(random.randint(3000, 6000))

    radio.send("GO")
    start = running_time()
    display.show(Image.TARGET)

    winner = None

    while running_time() - start < 5000:
        message = radio.receive()

        if message and message.startswith("HIT:"):
            player_id = message.split(":")[1]
            winner = player_id
            break

    if winner:
        if winner not in players:
            players[winner] = 0

        players[winner] += 1
        radio.send("WIN:" + winner)
        display.scroll("Winner " + winner)
    else:
        display.scroll("Nobody")

The server:

  • sends the signal GO
  • waits for the reply HIT:ID
  • gives a point
  • announces the winner

3. The code for a player


Every player must have their own ID.


from microbit import *
import radio

radio.on()
radio.config(group=7)

player_id = "1"   # change it for each player

while True:
    message = radio.receive()

    if message == "GO":
        display.show(Image.SQUARE)

        while not button_a.was_pressed():
            pass

        radio.send("HIT:" + player_id)

    if message and message.startswith("WIN:"):
        winner = message.split(":")[1]

        if winner == player_id:
            display.show(Image.HAPPY)
        else:
            display.show(Image.SAD)

The player:

  • waits for the signal GO
  • presses the button
  • sends HIT:ID
  • receives the information about who has won

4. Extending the game (advanced level)


Now you can add:

  • ✔ a limit of 5 points (the end of the game)
  • ✔ a sound signal before GO
  • ✔ a fake signal (FAKE) to confuse the players
  • ✔ using the accelerometer instead of the button
  • ✔ showing a score table

An example of the end of the game:


if players[winner] == 5:
    radio.send("END:" + winner)
    display.scroll("Game over")

5. Analysing the project


This project introduces important concepts:

  • Master–client architecture
  • A mini communication protocol
  • Identifying devices
  • Working with string functions (split, startswith)
  • A dictionary for the points

This is the basis for:

  • network games
  • IoT systems
  • wireless sensor networks

Think about it:

  • What if two players send HIT at the same time?
  • How do you resolve the conflict?
  • How do you add device authentication?

Radio Arena RPG — Strategy and energy


This is an advanced version of the multiplayer game. Now every player has:

  • energy (HP)
  • attack
  • defence
  • a special power

The goal of the game

  • The players attack each other wirelessly
  • Every hit reduces the opponent's energy
  • The winner is the last one left with energy > 0

This is a real small wireless RPG system.

1. The radio RPG protocol


We introduce structured messages:

ATTACK:ID:power
DEFEND:ID
SPECIAL:ID
STATUS:ID:energy
DEAD:ID

Example:

ATTACK:2:15

→ Player 2 attacks with power 15

2. The basic player code



from microbit import *
import radio
import random

radio.on()
radio.config(group=9)

player_id = "1"

energy = 100
defending = False

while True:
    message = radio.receive()

    # Attack with button A
    if button_a.was_pressed():
        power = random.randint(5, 20)
        radio.send("ATTACK:" + player_id + ":" + str(power))
        display.show(Image.SWORD)
        sleep(500)
        display.clear()

    # Defend with button B
    if button_b.was_pressed():
        defending = True
        radio.send("DEFEND:" + player_id)
        # the micro:bit has no ready-made shield picture, so we draw our own
        display.show(Image("99999:99999:99999:09990:00900"))
        sleep(500)
        display.clear()

    # Processing a received message
    if message and message.startswith("ATTACK:"):
        data = message.split(":")
        attacker = data[1]
        power = int(data[2])

        if attacker != player_id:
            if defending:
                power = power // 2
                defending = False

            energy -= power

            if energy <= 0:
                radio.send("DEAD:" + player_id)
                display.show(Image.SKULL)
            else:
                display.scroll(str(energy))

3. A special power (shaking the device)


If a player shakes the micro:bit, it starts a special attack.


if accelerometer.was_gesture("shake"):
    power = random.randint(20, 35)
    radio.send("SPECIAL:" + player_id + ":" + str(power))
    display.show(Image.DIAMOND)
    sleep(800)
    display.clear()

The special attack can:

  • have a stronger attack
  • use extra energy
  • have a cooldown (a time limit)

4. Game strategy


The game now needs thinking:

  • When should you attack?
  • When should you defend?
  • When should you save the special attack?

Possible upgrades:

  • ✔ An energy potion (recovery)
  • ✔ A limit on the special attack (1 per round)
  • ✔ Teams (group channels)
  • ✔ A mana system (energy for the special attack)

5. Advanced concepts this game introduces


  • A distributed system
  • State (a state machine)
  • Managing resources
  • A protocol with several types of messages
  • Simulating a network game

Questions for analysis:

  • What if two players attack at the same time?
  • How do you prevent spam attacks?
  • How do you synchronise energy between devices?

This is already conceptually close to real multiplayer games.

An advanced project example — A radio race


In this project two micro:bit devices take part in a wireless race. Each player increases their score by pressing button A, and the score is sent to the other device by radio. The winner is the first to reach 10 points.


Setup (required on both devices)


from microbit import *
import radio

radio.on()
radio.config(group=42)

The code for both players



from microbit import *
import radio

radio.on()
radio.config(group=42)

my_score = 0
opponent = 0

while True:

    # Increase the score by pressing A
    if button_a.was_pressed():
        my_score += 1
        radio.send(str(my_score))
        display.show(my_score)

    # Receive the opponent's score
    message = radio.receive()
    if message:
        opponent = int(message)

    # Checking for a win
    if my_score >= 10:
        display.scroll("WIN")
        break

    if opponent >= 10:
        display.scroll("LOSE")
        break

    sleep(100)

How does the project work?


  • Each device keeps its own score.
  • The score is sent as a text message.
  • The received message is turned into a number with int().
  • When someone reaches 10 points, the game ends.

Ideas for extensions (competition level)

  • Add a countdown before the game starts.
  • Add a sound signal for the win.
  • Introduce a time limit (for example 30 seconds).
  • Add a third player (a radio network).
  • Implement a simple acknowledgement of received messages (an ACK system).

This project develops your understanding of radio communication, working with numerical data and game logic. It is suitable for school competitions and STEM demonstrations.

Hidden mini quiz — Radio RPG (energy and strategy)

Check whether you understand how the radio RPG system works: messages, energy, attack and defence.

Open the quiz

Mini quiz: Radio Arena RPG

1. What does the message ATTACK:2:15 mean?




2. What happens if defence is active during an attack?




3. Which function makes the special attack by shaking the device possible?




4. What should be checked before reducing the energy?




5. What does it mean if the energy falls to 0 or less?






Related lessons and useful resources


If you want to extend your knowledge of MicroPython radio communication, look at the following lessons and resources.


Related lessons on SvetProgramiranja.com



Useful external resources



By combining radio communication, sensors and actuators you can develop serious STEM and IoT projects, as well as papers for school and regional competitions.

Related: the same topic in MakeCode and other lessons

If you worked with the micro:bit in blocks in the 5th or 6th grade, compare the blocks with the MicroPython code: