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:

micro:bit python radio documentation

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.

 

Enjoy!

 

Advertisements

About dupriestmath

I'm a former software engineer who has taught middle school math and computer science for the past 6 years. I believe every kid has the right to be a thinker. I started this blog to save resources for integrating programming in the Common Core math classroom. I also use it to save my lessons and reflections from teaching budding computer scientists! Coding has transformed how I teach and think. You'll love what it does for you. You should try it.

7 responses to “Using the micro:bit radio in Python”

  1. zamanskym says :

    Cool. I didn’t realize the microbit had a radio. End to end communication can be such a headache but if that part is done the kids can explore all sorts of things. Kindof like what the XBEE stuff did for arduino.

    • dupriestmath says :

      It is so exciting and l could tell even students who wouldn’t normally like to work with a partner were starting to wonder who they could collaborate with so they could try out ideas. It really changes the way you work!

  2. gflint says :

    Nuts. Something else to play with when I do not have time to play with all my toys as it is. That is one of the major problems with teaching tech, there is always something cool to look at and only 24 hours in a day.

  3. bobirving13 says :

    Very cool! I knew there was a radio component but hadn’t found a way to do it yet. Thanks for sharing! May I ask what grade this was, and also what their level of familiarity with Python is?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: