Receive Radio Messages
Now that we’ve got one micro:bit broadcasting radio messages every 2 seconds, let’s program another micro:bit to receive those messages and display them in the terminal. Both micro:bit modules have to be running their projects at the same time. The radio receiver micro:bit will also have to be connected to a terminal so that it can print what it receives.
- Complete one of these three options:
- Option 1: Connect the micro:bit that will run this receiving project to a second computer and terminal.
- Click the Connect button in a makecode.microbit.org editor and set up the connection to the receiver micro:bit.
- Then connect to Google Chrome Labs Serial Terminal as well.
- Option 2: Disconnect the micro:bit that’s sending radio messages from your computer, and connect it to a battery supply. Then, connect a micro:bit that’s going to recieve messages to your computer’s USB port and a terminal.
- After disconnecting the radio sender micro:bit from USB, connect it to a battery supply so that it continues to run the project that sends radio messages.
- Connect the micro:bit that’s going to run the radio receiver project to your computer’s USB.
- Click the Connect button in the makecode.microbit.org editor and set up the connection to the receiver micro:bit.
- Then connect to Google Chrome Labs Serial Terminal as well.
- Option 3: Connect the micro:bit that will run this receiving program to a second USB port on the same computer. Also connect it to a terminal.
- Unplug the micro:bit that’s sending messages from its USB port.
- Connect the micro:bit that will receive messages to the USB port.
- Click the Connect button in the makecode.microbit.org editor and set up the connection to the receiver micro:bit.
- Then connect to Google Chrome Labs Serial Terminal as well.
- After the receiver micro:bit has the makecode.microbit.org editor and the terminal connection, plug the sender micro:bit to a second USB port. At this point, you should have both sender and receiver micro:bit modules connected to USB ports. Since the sender micro:bit is connected to USB, but not to a makecode.microbit.org editor, it will only draw power to run its project and send it over radio. It will not interfere with the receiver micro:bit’s programming or terminal.
- Option 1: Connect the micro:bit that will run this receiving project to a second computer and terminal.
Now, you are ready to load a project into a second micro:bit for receiving radio messages.
Example project: receive_radio_test_message
- Enter and flash the receive_radio_test_message project:
- Go to the terminal tab and make sure it’s connected.
- Verify that it displays the Receive: Testing 123… line, and adds a new line every 2 seconds.
How Receiving Messages Works
The micro:bit that’s receiving messages has to be set to the same channel as the one that’s sending messages.
Inside this on radio received loop, the statement will repeatedly check to find out if the radio received a message. It will only run through a message it received to the terminal once, and then go back to waiting for the next message.
A receivedString variable is created and set to the result that the radio receives. If a radio message was received, that result is a string. If it isnt a string then nothing will be shown and a different loop is needed to display it.
If the message variable is None at this point, the project will skip the statement that displays the message. On the other hand, if message has a string, the project will print it in the terminal. That’s because if <not< received string is empty>> only executes statements below and indented from it if on radio received copied a string into the message variable.
If the message variable does have a string, display it in the terminal with serial write line (join (“Receive: “)( message)).
Did You Know?
The radio set group (7) setting is actually the default channel. Classrooms with micro:bit modules can set up more than one broadcasting module, each set to a different channel that sends a different message. The rest of the class can run receiver projects that “tune in” to one of those broadcasting channels. For best results, keep the channels as widely separated as possible. For example, if there are five broadcasting micro:bit modules, use channels 0, 20, 40, 60, and 80. Each one should broadcast a unique message so that students can verify which micro:bit they are receiving messages from.
RF is an abbreviation for radio frequency.
The sender micro:bit is broadcasting. Broadcasting is one-to-many communication. One broadcasting unit, many receivers. True, if you just have one receiver, it’s actually one-to-one communication. However, if you have more than one micro:bit, they can also run projects that receive and print, which makes it one-to-many.
Here’s an excerpt from the micro:bit MicroPython Radio reference:
The radio set group (…) can be an integer value from 0 to 255 (inclusive) that defines an arbitrary “channel” to which the radio is tuned. Messages will be sent via this channel and only messages received via this channel will be put onto the incoming message queue. Each step is 1MHz wide, based at 2400MHz.
2400 MHz is a radio frequency that reverses the voltage applied to the antenna at 2400-million times per second. It’s another way of saying 2.4 GHz, which is 2.4-billion times per second. In terms of the numbers:
2400 M = 24 million = 2400 * 1,000,000 = 2,400,000,000
2.4 G = 2.4 billion = 2.4 * 1,000,000,000 = 2,400,000,000
Channel 0 sets the radio frequency to 2400 MHz, channel 1 to 2401 MHz, and so on up to channel 255 at 2655 MHz. In terms of GHz, that’s 2.401, 2.402, and so on through 2.483 GHz.
The channel setting also makes it possible to avoid a particular Wi-Fi network that might be crowding the band with radio traffic. Wireless routers use channels 1 through 14, with frequencies of 2412 through 2484 MHz (2.412 through 2.484 GHz).