Using the micro:bit radio in Python
I’ve been having fun getting to know the micro:bit with my students this year. I often plan lessons based on what they tell me they’d like to learn, and they were really intrigued by the idea of radio communication between micro:bits. So I decided to learn about it. There is a “firefly” tutorial on the documentation page here:
But I felt what I wanted to learn was even simpler than that. I just wanted to know how to send simple messages, like numbers and text, between micro:bits. I ended up making my own little tutorial and maybe someone else will find it useful.
First, I had the students copy this program and download it to their own micro:bit.
from microbit import * # must include these two lines to use the radio import radio radio.on() # any channel from 0 to 100 can be used for privacy. radio.config(channel=99) while True: if button_a.was_pressed(): # send this message over the radio. Up to 32 bytes OK. radio.send('HAPPY') sleep(200) if button_b.was_pressed(): radio.send('SAD') sleep(200) # if there's a message in the queue, retrieve it. Up to 3 messages # can be in the queue at once, and if it's full, messages are dropped. msg = radio.receive() # ALWAYS CHECK for None.. if msg != None: # as long as there is a message, display something if msg == 'HAPPY': display.show(Image.HAPPY) sleep(200) display.clear() elif msg == 'SAD': display.show(Image.SAD) sleep(200) display.clear()
After the students downloaded the program, of course they started fiddling with the buttons to see if anything would happen. The buttons don’t seem to do anything on their own device, but they would notice their device would randomly show smiley faces and sad faces. Eventually a pattern starts to emerge and students realize their button-presses are affecting the other micro:bits in the room. After a few minutes I ask the students to try and make my micro:bit happy, and they all press button A. They make my micro:bit sad by pressing button B. If a student or two can make inferences from the code, they change the code and make it send messages other than “HAPPY” or “SAD” and then my micro:bit, and the others, start scrolling strange messages. It’s hilarious and chaotic.
So next we look at the example code and dig into how the radio works. We analyze the program that’s already on their micro:bits and then, I help the kids write a very basic skeleton program that just selects a channel, sends a message and scrolls all received messages on the display. Students could use it with friends to send secret messages during class. They had fun making their skeleton programs and sending messages to me and each other.
Some students took it farther and started setting up a protocol for their micro:bits – a little agreed-upon system of communication between them. If one message is received, play a tune. If another is received, sparkle the LED’s and send a message back. This is a great direction to take future lessons – to chat about how we can make computers communicate with each other so the communication is efficient, flexible and free from errors in different situations.
I envision using the radio commands when we learn about looping and iteration. I’ve seen fun examples of games that use multiple micro:bits and think there is a lot of potential there!
I made a video with the basics of the lesson – maybe someone else will find it useful if you’re using Python with your micro:bit.