Controlling a Raspberry Pi robot using the micro:bit accelerometer
The BBC micro:bit includes an accelerometer which allows you to track the movement of the micro:bit. This means that it can be used to as a controller similar to how the Wii remote control tracks it’s movement through the air. For this project I used the micro:bit as a wired device to control my existing Raspberry Pi robot. In theory it should be possible to have it communicating via the built-in bluetooth v4 controller (also known as Bluetooth Low Energy / BLE).
As I didn’t want to be physically tethered to the robot I used another computer to act as a proxy / relay server to receive the status of the micro:bit and then pass it on to the Raspberry Pi on the robot. This computer could be any Linux computer, such as another Raspberry Pi (or a PiZero, but that will require a USB hub).
The messages between the micro:bit and the Raspberry Pi were passed using a serial connection. I had already included a web interface on the Raspberry Pi robot, I had the relay perform a url-get against the web page of the Robot using the python urllib module.
Most of the code to read the status of the accelerometer and convert that into something the Raspberry Pi could understand has been written in MicroPython on the micro:bit. It may have been slightly easier to have the proxy computer perform that, but this was more of an exercise in programming the micro:bit.
There is little to no error checking in this, which is something that should be considered in future. The serial communication did work OK most of the time, but would occasionally drop some of the information resulting in an error message.
The source code is available from github at: penguintutor github MicroPython on the BBC micro:bit. There are two files, microbit-webrobot.py which should be installed on the micro:bit (converted to a hex file first) and pc-webrobot-proxy.py which runs on the Linux computer (may need to be run as root using sudo – or setup appropriate dialout permissions).