micro:bit BLE from the Raspberry Pi

microbit logo

Bluetooth logo

Raspberry Pi logo

Overview

This is an introduction on how you can exchange information between a micro:bit and a Raspberry Pi using Bluetooth Low Engergy (BLE).

Setting up the Raspberry Pi

You will need to install the Bluezero package from Python Package Index (PyPI). These instructions are targeted at a Raspberry Pi running a Raspbian Stretch release:
sudo pip3 install bluezero

Setting Up the micro:bit

You will download the hex file from the Bitty Software website. The hex file is available at:
http://www.bittysoftware.com/downloads.html#bitty_blue

You will use the “no pairing required” hex file

The first time the micro:bit is powered up you will need to rotate it so a circle is drawn on the micro:bit. Once this is done you are ready to connect to it with the Raspberry Pi

Finding the micro:bit

If this is the first time this combination of Raspberry Pi and micro:bit have been used together then you will need to ensure the micro:bit is in the list of known Bluetooth devices on the Raspberry Pi. You will do this using bluetoothctl.

Note: There is ‘tab’ command completion on commands and addresses available within the bluetoothctl tool.
Note: As Bluetooth addresses are unique this workshop uses the representation of your Raspberry Pi and micro:bit address as xx:xx:xx:xx:xx:xx and yy:yy:yy:yy:yy:yy respectively. Please replace these with the actual address of your devices

Using bluetoothctl

You will use the command line tool “bluetoothctl” to discover our micro:bit.
On the terminal command line type:
$ bluetoothctl

You will then get the new prompt:
[bluetooth]#

You can type help to get a full list of the commands.
Check that the Bluetooth is switched on on the Raspberry Pi
Type show and check that you have Powered: yes.
For example:

Controller xx:xx:xx:xx:xx:xx
	Name: raspberrypi
	Alias: raspberrypi
	Class: 0x000000
	Powered: yes
	Discoverable: no
        Pairable: yes
	UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
	UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
	UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
	UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
	UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
	Modalias: usb:v1D6Bp0246d052E
	Discovering: no

If "Powered" is not set to "yes" then type power on

Make a note of the controller address as you will need it later

Scan for micro:bit

You will need to get bluetoothctl to search for micro:bits that are nearby.
When you see your micro:bit you can stop the search.

[bluetooth]# scan on
Discovery started
[CHG] Controller xx:xx:xx:xx:xx:xx Discovering: yes
[NEW] Device yy:yy:yy:yy:yy:yy BBC micro:bit [?????]
[bluetooth]# scan off

Connect to micro:bit

You need to specify the Bluetooth address of your micro:bit (found with the above command) to connect to it.
Because you have used the “no pairing required” hex file the micro:bit will become part of the known devices once it has been connected to and allow you to easily access it from the Python code
Once connection has happened and a "C" appears on the micro:bit you can disconnect.

[bluetooth]# connect yy:yy:yy:yy:yy:yy
Attempting to connect to yy:yy:yy:yy:yy:yy
[CHG] Device yy:yy:yy:yy:yy:yy Connected: yes
Connection successful
[BBC micro:bit [?????]]# disconnect
    

If you want to check the micro:bit has been added to the devices list then type devices

You have finished with bluetoothctl so type exit

Using Python with Bluezero Library

Sending a message to the micro:bit

This first Python exercise is to send text to display on the micro:bit.

from bluezero import microbit
ubit = microbit.Microbit(adapter_addr='xx:xx:xx:xx:xx:xx',
                         device_addr='yy:yy:yy:yy:yy:yy')
my_text = 'Hello, world'
ubit.connect()

while my_text is not '':
    ubit.text = my_text
    my_text = input('Enter message: ')

ubit.disconnect()
    

Reading a button press

Display an image of which button needs pressing to break out of the loop

import time
from bluezero import microbit
ubit = microbit.Microbit(adapter_addr='xx:xx:xx:xx:xx:xx',
                         device_addr='yy:yy:yy:yy:yy:yy')
ubit.connect()
while ubit.button_a < 1:
    ubit.pixels = [0b00000,
                   0b01000,
                   0b11111,
                   0b01000,
                   0b00000]
    time.sleep(0.5)
    ubit.clear_display()

while ubit.button_b < 1:
    ubit.pixels = [0b00000,
                   0b00010,
                   0b11111,
                   0b00010,
                   0b00000]
    time.sleep(0.5)
    ubit.clear_display()

ubit.disconnect()

    

Using micro:bit to control Turtle

This last exercise uses information from the micro:bit sensors to control the Turtle graphics

import turtle
import time
from bluezero import microbit
oogway = turtle.Turtle()
ubit = microbit.Microbit(adapter_addr='xx:xx:xx:xx:xx:xx',
                         device_addr='yy:yy:yy:yy:yy:yy')
looping = True
ubit.connect()
oogway.write('Ready')

while looping:
    if ubit.button_a > 0:
        oogway.setheading(ubit.bearing)
        time.sleep(0.25)
    elif ubit.button_b > 0:
        ubit.pixels = [0b10001,
                       0b10001,
                       0b01110,
                       0b10001,
                       0b10001]
        oogway.write('Exit')
        looping = False
    else:
        x, y, z = ubit.accelerometer
        if y > 0.5:
            oogway.backward(10)
        if y < -0.5:
            oogway.forward(10)

ubit.disconnect()
turtle.bye()

    

license