Send Radio Messages

The best way to get started with testing the radio connection is to make one micro:bit module repeatedly broadcast messages.  Once this script is running, the next section will have you load a script that receives radio messages and prints messages into a different micro:bit.

IMPORTANT! If you are doing this in a classroom, start with ONLY ONE micro:bit transmitting messages by running the script in this section.  All the rest of the micro:bit modules should be running the script in the next section that receives and displays the messages.

  • Connect your micro:bit module to your computer with a USB cable.
  • Open a Chrome browser and go to python.microbit.org/v/2 to match this tutorial's screencaptures.
  • Click the Connect button.  

Not sure how to connect?  Go to the Scripts that Print to Terminal page and follow the instructions in the Make Sure Online Editor Is Connected section.

Example script: send_radio_test_message

  • Enter, save, and flash the script send_radio_test_message:
# send_radio_test_message

from microbit import *

import radio

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

sleep(1000)

print("micro:bit radio sender")

while True:
    message = "Testing 123..."

    print("Send: ", message)
    
    radio.send(message)
    
    print("Done!")

    sleep(2000)
  • Click the Open Serial button.
  • Verify that the terminal repeatedly displays Send:  Testing 123… followed by Done! on the next line.
  • Either leave this script running, or be ready to power that micro:bit back up after you start running the receiver script in another micro:bit.

How send_radio_test_message Works

Let’s take a closer look at statements you probably haven’t seen in earlier activities.  The first is:

from microbit import *
import radio

This imports the micro:bit radio module so that your script can call its methods.  Like the microbit and display modules, the radio module is part of MicroPython.  So, you don’t need to manually add a module to the project.  Just type import radio, and move on!

Since the radio takes power to run, it is turned off by default.  This makes micro:bit applications that use batteries last longer.  The radio.on() method call turns on the radio.  After that, the radio can broadcast and receive messages.  Keep in mind that it will also draw more current and discharge a battery supply more quickly.

radio.on()

The radio module’s config method allows you to configure the radio in many different ways.  Some examples include radio transmit power, maximum message length, maximum number of incoming messages to store, and the channel setting.  

In this activity, we are just using the channel setting, which can be set from 0 to 83.  Keep in mind that micro:bit modules can only radio-communicate when they are set to the same channel.  Let’s say that one pair of micro:bit modules is running scripts with channel = 10, and a second pair is using channel = 20.  The channel = 10 micro:bit modules will not receive any messages sent by the channel = 20 micro:bit modules, and vice-versa.  The Did You Know section later in this tutorial has more info about channels.

radio.config(channel = 7)

(In later activities, you will use the group and address settings to set up wireless networks.  They will allow micro:bit modules to communicate in pairs or groups, even when they are sharing the same channel.)

The message = "Testing 123…" statement creates a variable that stores a string with the “Testing 123…” characters.  Next, print("Send: ", message) displays Send:  Testing 123… in the terminal.  Then, radio.send(message) uses the micro:bit radio to broadcast that “Testing 123…” text in the message variable.

message = "Testing 123..."
print("Send: ", message)
radio.send(message)

What do you think would happen if you replaced the three lines above with this?  

radio.send("Testing 123…")

Although it would transmit the same message, you’d also have to update the print statement.  When it came time to change the message, you’d also have to do it by hand in two places.  

Before repeating the loop, print("Done!") makes the terminal tell you that the script has reached the line that follows radio.send.  It’s a way to verify that the message was sent by the radio.  The sleep(2000) makes it so messages are only broadcasted every 2 seconds.  

print("Done!")
sleep(2000)