BBC micro:bit
Bluetooth Mouse & Keyboard

Introduction

The built-in bluetooth does not currently work with MicroPython. There are some bluetooth breakout boards that can be used for this though. This project uses Adafruit's Bluefruit EZ-Key HID breakout to send keyboard and mouse commands over bluetooth. You will need a bluetooth enabled PC or bluetooth dongle to test this.

This isn't a cheap component. It costs £15-20. It has a lot of features, is easy to communicate with and tolerates a range of different voltages for input. It also works without a micro-controller with built-in, reprogrammable input pins. I had one of these already and wanted to see if I could get the UART functions of the micro:bit working.

Circuit

Not much to connect here.

Bluefruit Circuit

Plug in your micro:bit now. Press the button on the Bluefruit and pair it with your computer.

Programming - Keyboard

You can send strings of characters and they will get sent to the computer as keyboard input.

from microbit import *

uart.init(baudrate=9600, bits=8, parity=None, stop=1,tx=pin0)

while True:
    b=0
    dx = 0
    dy = 0
    if button_a.was_pressed():
        uart.write('Button A was pressed')        
    elif button_b.was_pressed():
        uart.write('Button B was pressed')                
    sleep(50)

There are non-printing characters. This program sends backspace and enter key presses to the PC when the A and B buttons are pressed.

from microbit import *

uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=pin0)

while True:
    if button_a.was_pressed():
        uart.write(bytes([0x08]))        
    elif button_b.was_pressed():
        uart.write(bytes([0x0d]))
    sleep(50)

Here is a table of the other characters that you can send.

HEXKeyname
0x01Insert
0x02Home
0x03Page Up
0x04Delete
0x05End
0x06Page Down
0x07Right Arrow
0x08Backspace
0x09Tab
0x0AEnter
0x0BLeft Arrow
0x0CDown Arrow
0x0DEnter
0x0EUp Arrow
0x0F - 0x1AF1 - F12
0x1BEsc
0x1CCaps Lock
0x1DScroll Lock
0x1EBreak
0x1FNum Lock
0x20-0x7EPrintable Ascii
0x7FToggle iOS Keyboard
0xE0Left Control
0xE1Left Shift
0xE2Left Alt
0xE3Left GUI
0xE4Right Control
0xE5Right Shift
0xE6Right Alt
0xE7Right GUI

Programming - Mouse

This isn't so easy to get right with the accelerometer - an analogue joystick works a lot better for this. It shows you how to send mouse commands though,

from microbit import *

def MouseCommand(buttons, mousex, mousey):
    d = bytes([
    0xFD,0x00,0x03,
    buttons,mousex,mousey,
    0x00,0x00,0x00
    ])
    uart.write(d)
    return

    uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=pin0)

while True:
    b=0
    dx = 0
    dy = 0
    if button_a.was_pressed():
        b = 0x01        
    elif button_b.was_pressed():
        b = 0x02
    if abs(accelerometer.get_x())>200:
        dx = accelerometer.get_x() // 40
    if abs(accelerometer.get_y())>200:
        dy = accelerometer.get_y() // 40    
    MouseCommand(b,dx,dy)
    sleep(10)

Challenges

  1. Make a touch controller for a game. You have 3 touch pins, enough for left-right-fire or left-right-jump.
  2. You can send the keyboard signals when you like. Sending random text to a sibling's PC at random intervals is quite possible.
  3. You can use this with phones too. Pehaps you might make a remote control. Search for Adafruit's guide to the product and you'll be able to work out how to send the multimedia commands.