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.
Hardware Setup
- Connect your micro:bit module to your computer with a USB cable.
- Repeat with the second micro:bit module, either connecting it to a second computer, or to a second USB port on the same computer.
Software Setup
- In a Google Chrome or Microsoft Edge browser, go to python.microbit.org to open the micro:bit Python Editor.
Example script: send_radio_test_message
- Set the project's name to send_radio_test_message, enter the script below, and then click Save.
(See Save & Edit Scripts and Flash Scripts with Python Editor.) - Click Send to micro:bit.
(See Flash Scripts with Python Editor.)
# 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 Show serial to open the serial monitor.
(See Use the Serial Monitor, and make sure to click the Show serial near the lower-right corner in the micro:bit Python Editor, not the one in the upper-right of the simulator pane.) - 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)