BBC micro:bit
Radio micro:bit

Introduction

The radio module allows you to send and receive messages wirelessly between micro:bits within close range. You have probably gathered that MicroPython doesn't work with the Bluetooth on the micro:bit. Bluetooth Low Energy (BLE) is a protocol. It is a set of rules about how messages are sent between devices. This takes more memory than can be spared when running MicroPython. The hardware that is required for this is a Bluetooth radio. That can be used with other, simpler protocols and, after more great work from the MicroPython developers and contributors, access to the radio is now possible.

You either need 2 micro:bits for this or you get a buddy to work with you.

Programming

There's an enormous scope for making little networks of micro:bits. In the end, you'll be using the following statements,

from microbit import *
import radio

# Enable the radio
radio.on()

# Send a message
radio.send("Hello")

# Receive a message
message = radio.receive()

# Turn off the radio
radio.off()

The radio uses power that you might want to spare if you are working on a battery-powered project. You have to turn it on when you want it. You can also turn it off.

The MicroPython Documentation has really good material on how to use the radio as well as statements not mentioned here. You have a lot of control over how you configure the radio for your project.

Example Program - Sending

This program allows you to design an image on the display using code from the Lights Out example. When you press button B, the image data is sent by radio to another micro:bit.

from microbit import *
import radio


x = 2
y = 2
tick = -1

grid = [
    [0,0,0,0,0],
    [0,0,0,0,0],
    [0,0,0,0,0],
    [0,0,0,0,0],
    [0,0,0,0,0]
    ]


def Toggle(tx, ty):
    grid[tx][ty] ^= 1
              

def Draw(t):
    img = Image('00000:'*5)
    for cy in range(0,5):
        for cx in range(0,5):
            img.set_pixel(cx, cy, grid[cx][cy]*5) 
    img.set_pixel(x, y, (t % 2)*9)
    return img


def ImageString():

    s = ""
    for cy in range(0,5):
        for cx in range(0,5):
            s += str(grid[cx][cy]*9)
        s += ":"
    return s

radio.on()

while True:
    tick +=1
    if tick==2:
        tick = 0
    # check for movement
    dx = accelerometer.get_x()
    dy = accelerometer.get_y()
    if dx > 300:
        x += 1
        sleep(200)
    if dx < -300:
        x -= 1
        sleep(200)
    if dy > 300:
        y += 1
        sleep(200)
    if dy < -300:
        y -= 1
        sleep(200)
    # keep on grid    
    x = max(0, min(x, 4))
    y = max(0, min(y, 4))
    # check for button press
    if button_a.was_pressed():
        Toggle(x, y)
        sleep(200)
    # update screen
    i = Draw(tick)
    display.show(i)
    if button_b.was_pressed():
        radio.send(ImageString())      
    sleep(50)

Example Program - Receiving

This is a simple example that draws the image data it receives onto the matrix.

from microbit import *
import radio

radio.on()

while True:
    s = radio.receive()
    if s is not None:
        print(s)
        i = Image(s)
        display.show(i)

Challenges

  1. Rock, paper scissors could be adapted to work wirelessly with two micro:bits. The radio can be used to time things so that the images appear only after both buttons are pressed.
  2. You can adapt the two examples to make one program people can use to share images. You could also make use of the local storage to save the image data.
  3. You could do Morse Code this way. If you can write a program that can say how long a button was pressed for, you can do this. Inside an infinte loop, read the button and find out its state. The last statement of your loop is to assign that value to a variable. In between those lines you can compare the value stored in this variable with the reading just taken. If there is a difference, the button has been pressed and released. You can use running_time() to find out how much time has passed between the press and release. Have a range of tolerances for the size of a dot and a dash and compare the time taken with these to determine which you have. The rest is a journey to be had with some examples on here and in the documentation that can point the way for a determined programmer.