Third party cookies may be stored when visiting this site. Please see the cookie information.

PenguinTutor YouTube Channel

PyconUK 2016 - Sending secret messages with a micro:bit

Another year and another Pycon UK. For 2016 the UK's community Python Conference was held in Cardiff, Wales. It's also the fourth year that they've included a special Children's day on the Saturday. This included a day of kids coding on Raspberry Pi computers (Pi-Top CEED) and this year each child received a micro:bit to provide a new way of coding.

The final workshop session was a open "work on a project" session and many of the children decided to use their new micro:bits in their project. My son's idea was to use two micro:bits (luckily I'd taken an extra one with me) to be able to send messages between the micro:bits. This would allow him to send secret messages to his friends in class [not that we'd encourage such things :-)].

Pycon UK 2016 - kids lightning talks

So we set about writing the code using using the Mu editor on the Raspberry Pi and then using the editor to flash the code directly to the micro:bit. This is a much better way to write code to the micro:bit compared to having to generate the hex file and then drag it onto the micro:bit.

The code is fairly simple thanks to the provided radio module. Essentially it looks for button presses on the micro:bit and depending upon whether A, B or A+B are pressed then it sends a different message. The other micro:bit listens for the signal and then displays a message using the LEDs on the front of the micro:bit. We only had about an hour to get the basic code working, so we didn't include the ability for bidirectional messages, which would probably be a bit hard for such a young programmer. The python code is included below:

Transmitting micro:bit


from microbit import *
import radio

radio.on()

while True:
    a = button_a.was_pressed()
    b = button_b.was_pressed()
    if a and b:
        radio.send("boring")
        display.scroll("Boring")
    elif a:
        radio.send("fun")
        display.scroll("Fun")
    elif b:
        radio.send("playtime")
        display.scroll("Playtime")

    sleep(100)

Receiving micro:bit


from microbit import *
from microbit import display
import radio

radio.on()

while True:
    received_text = radio.receive()
    if (revieved_text == 'boring'):
        display.scroll ("This lesson is boring")
        sleep (100)
    elif (received_text == "fun"):
        display.scroll ("This lesson is fun")
        sleep (100)
    elif (received_text == "playtime"):
        display.scroll ("Shall we meet at playtime")
        sleep (100)
    else:
        display.clear()

As you can't see there aren't many comments in the code, but then it should be fairly obvious to see how it works. The sending and receiving of messages is handled by the radio module and the main thing to consider with the button press detection is that it includes the test of both a and b being pressed first (otherwise the if would see both buttons being pressed as "a" being pressed).

My son and gave a short demonstration of the project during the lightning talks on the main PyconUK stage. This is included in the video below: