MicroPython — radio communication between micro:bit devices

Lesson 10 of 17 · MicroPython and the micro:bit

One of the most interesting features of the micro:bit is wireless communication with its built-in radio. In this way two or more micro:bits can exchange messages.


In this lesson you will learn:

  • how the radio module is used in MicroPython
  • how messages are sent and received
  • how to make simple communication between two devices

Switching on the radio module


For the micro:bit to communicate, you need to switch on the radio module.



from microbit import *
import radio

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

All the micro:bit devices that are in the same radio group can communicate with each other.


Sending a message


In the following example the micro:bit sends a message when button A is pressed.



from microbit import *
import radio

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

while True:
    if button_a.was_pressed():
        radio.send("Hello!")
        display.show(Image.HAPPY)
        sleep(500)
        display.clear()

Receiving a message


Another micro:bit can receive the message with the function radio.receive().



from microbit import *
import radio

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

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

If there is no message, the function radio.receive() returns None.


Example: sending numbers


The radio can also be used for sending numbers, for example for games or scoring.



radio.send("5")

On the receiving side you can turn the message into a number:



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

Comparison with the MakeCode environment


If you worked in MakeCode before, the radio blocks you used are replaced here by functions:

  • radio.on() → switch on the radio
  • radio.send() → send a message
  • radio.receive() → receive a message

MicroPython gives you more control and makes it easier to build advanced projects.



A deeper understanding of radio communication


Radio communication on the micro:bit works like a small local network. All the devices:

  • must have the radio switched on (radio.on())
  • must be in the same group (radio.config(group=number))

A group is like a "communication channel". If the devices are in different groups — they do not hear each other.


Important: radio.receive() and None

Every time it is called, the function radio.receive() checks whether a message has arrived in the meantime and returns the next message that has arrived. If there is no message, it returns None.


message = radio.receive()

if message is not None:
    display.scroll(message)

This is an example of working with the value None, which is important in Python.


Example 1 — Two-way communication

One micro:bit sends "PING", the other answers "PONG".

Device A


from microbit import *
import radio

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

while True:
    if button_a.was_pressed():
        radio.send("PING")

Device B


from microbit import *
import radio

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

while True:
    message = radio.receive()

    if message == "PING":
        display.show(Image.YES)
        radio.send("PONG")

This is the basis of client–server communication.


Example 2 — A wireless light switch

One micro:bit is a remote control, the other controls an LED or a relay.

Transmitter


if button_a.was_pressed():
    radio.send("ON")

if button_b.was_pressed():
    radio.send("OFF")

Receiver


message = radio.receive()

if message == "ON":
    pin0.write_digital(1)

if message == "OFF":
    pin0.write_digital(0)

This is the basis of a smart house.


Example 3 — Sending data from a sensor

One micro:bit measures the temperature and sends it to another device.

Transmitter (sensor)


t = temperature()
radio.send(str(t))
sleep(2000)

Receiver (display)


message = radio.receive()

if message:
    temp = int(message)
    display.scroll(str(temp) + "C")

Here we use:

  • turning a number into text (str())
  • turning text into a number (int())

Combining with earlier lessons

Radio + accelerometer


x = accelerometer.get_x()

if x > 500:
    radio.send("RIGHT")
elif x < -500:
    radio.send("LEFT")

The other micro:bit shows arrows based on the message it receives.

Radio + compass

You can send the direction of movement to another device.

Radio + counter

Sending the score of a game or the number of steps.


Mini project — A wireless reaction game

One device sends the signal "START". The other must press a button quickly and send back the reaction time.

This combines:

  • radio communication
  • variables
  • measuring time
  • comparison logic

Advanced settings of the radio module

You can set:


radio.config(
    group=3,
    power=7,
    channel=10,
    length=32
)
  • group — the group of devices
  • power — the signal strength (0–7)
  • channel — the frequency channel
  • length — the maximum length of a message

This is useful when several groups of students work in a classroom.


Common student mistakes

  • They forget radio.on()
  • The devices are not in the same group
  • They forget to check whether the message is None
  • They try to send a number without str()

Tasks for independent work

Task 1

Make wireless traffic lights (one device sends a signal, the other changes the lights).

Task 2

Make a scoring system for a game (send the number of points).

Task 3 (advanced)

Make a wireless alarm:

  • One device detects movement
  • The other shows a warning

Task 4 (challenge)

Send several pieces of data in one message:

"25,450"

(the temperature and the light at the same time)

On the receiving side split the message.


A challenge for you — can you solve this?


Make a system with two micro:bit devices:

  • The first measures the temperature and sends the value over the radio.
  • The second shows the temperature only if it is above 25°C.

Try to write the program on your own. Think about:

  • How do you send a number over the radio?
  • How do you turn the received text into a number?
  • How do you use an if condition?

If you need help or want more examples of radio communication, see a more detailed explanation here:

➡ The radio connection on the micro:bit — more examples and explanations


There you will find:

  • extended examples of communication
  • an explanation of data conversion (str ↔ int)
  • ideas for advanced projects

Try it on your own first — and only then click the link.

Mini project — Wireless light control


In this project we will use radio communication between two micro:bit devices. One device sends commands, and the other reacts and switches a light on or off.


Device 1 — Transmitter (sends commands)

When you press button A the message "ON" is sent, and when you press button B the message "OFF" is sent.


from microbit import *
import radio

radio.on()

while True:
    if button_a.is_pressed():
        radio.send("ON")
        display.show(Image.ARROW_N)
        sleep(300)

    if button_b.is_pressed():
        radio.send("OFF")
        display.show(Image.ARROW_S)
        sleep(300)

Device 2 — Receiver (receives commands)


This device receives the message and reacts:

  • If it receives "ON" → switch on the LED or show the sign ✔
  • If it receives "OFF" → switch off the LED or show the sign ✖

from microbit import *
import radio

radio.on()

while True:
    message = radio.receive()

    if message == "ON":
        display.show(Image.YES)
        pin0.write_digital(1)   # switch on the LED

    elif message == "OFF":
        display.show(Image.NO)
        pin0.write_digital(0)   # switch off the LED

    sleep(200)

What do you learn with this project?


  • How radio.send() and radio.receive() work
  • How messages can be used to control a device
  • How to connect radio communication with the GPIO pins
  • The basis for IoT and wireless systems

Ideas for extending the project


  • Add a servo motor instead of the LED light
  • Add a password (for example only the message "ON123" activates the device)
  • Send sensor values to another device
  • Make a mini wireless alarm system

This mini project is the basis for more serious STEM and competition projects that include wireless communication between devices.

An advanced project — A wireless alarm system with a sensor

In this project you combine radio communication and a sensor. One micro:bit measures a sensor value and sends a warning when it goes above a certain threshold. The other micro:bit receives the message and activates an alarm (an LED, a buzzer or a servo).


Device 1 — Transmitter (sensor)


This device reads an analog sensor (for example a light sensor or a potentiometer). When the value goes above the given threshold, it sends the message "ALARM".


from microbit import *
import radio

radio.on()
radio.config(group=23)  # the same group on both devices

THRESHOLD = 700

while True:
    value = pin0.read_analog()

    if value > THRESHOLD:
        radio.send("ALARM")
        display.show(Image.SKULL)
        sleep(500)
    else:
        display.clear()

    sleep(200)

Device 2 — Receiver (alarm)

This device receives the message and activates the alarm system.


from microbit import *
import radio

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

while True:
    message = radio.receive()

    if message == "ALARM":
        display.show(Image.NO)
        pin1.write_digital(1)  # switch on an LED or a buzzer
        sleep(1000)
        pin1.write_digital(0)
        display.clear()

    sleep(200)

What does this project introduce?


  • Combining a sensor and radio communication
  • Using radio.config(group=...)
  • Sending messages as a signal for an event
  • The basics of wireless security systems

Ideas for competitions and advanced papers


  • Add more types of messages: "ALARM", "RESET", "STATUS"
  • Send the numerical value of the sensor instead of text
  • Make a network of 3 or more micro:bit devices
  • Add encryption with a simple password

This project is an excellent basis for STEM competitions, IoT demonstrations and advanced MicroPython projects.

Hidden mini quiz — radio communication

Check whether you understand the radio module, sending and receiving messages, groups and working with data.

Open the quiz

Mini quiz: radio communication

1. Which command switches on the radio module?




2. What does it mean if two devices are in different radio groups?




3. What does radio.receive() return if there is no message?




4. Why do we use str() when sending a number?




5. Analyse the code:


message = radio.receive()

if message:
    display.scroll(message)



6. Which line sets the radio group?







Advanced mini quiz — radio communication and sensors

Check your knowledge of radio communication, sending several types of data and combining it with sensors. This quiz is more challenging and needs some thinking.

Open the quiz

Advanced quiz: radio + sensors

1. What is the correct way to check whether a message has been received from the radio module?


message = radio.receive()



2. How can we send a number and the X tilt of the accelerometer in one message?




3. How do we separate the number and the tilt on the receiving side?




4. If we want to send only when the temperature is above 30°C, where do we put the condition?




5. Which option is an example of combining the radio signal and the compass sensor?




6. What is the advantage of setting the radio group (radio.config(group=...))?




7. Which line of code lets the micro:bit send numbers over the radio?







Related lessons and useful resources


If you want to extend your knowledge of MicroPython and the micro:bit, look at the following lessons on our site:

Related lessons on SvetProgramiranja.com


Useful external resources



Recommendation: combine radio communication with sensors and actuators to make a complete wireless system. In this way you develop your understanding of IoT concepts and practical uses of MicroPython.

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:

Useful external resources