Micro:Bit and Max

Controlling Max with a Micro:Bit coded in Python

Over the years I've experimented with a number of platforms including Raspberry Pi, Arduino and, more recently, the Micro:Bit. The thing I, personally, like about the Micro:Bit is that it has a number of sensors already built into it like an accelerometer, and a compass so it can tell which direction it's moving in and how fast.

I was inspired by Imogen Heap's MiMu Golve project but as the Micro:Bit is only £13 it's considerably cheaper than a MiMu glove, not as well spec'd but we've got to start somewhere.

So the project set out on this page, reads the data from the Micro:Bit and transfers it in the Max software via a USB cable. Ultimately the data will be transferred via Bluetooth but as this project has been started using Mu and MicroPython, which doesn't support Bluetooth, this will be developed in C/C++ at a later date.

Using MicroPython, getting the data from the Micro:Bit is fairly simple using a short piece of code which reads the sensors and compiles them into a comma separted list.

The code to the right is written in MicroPython and transferred to the Micro:Bit via USB where it runs repeatedly featching x,y,z position and a and b button presses.

The refresh rate can be tweaked to get the best balance between code execution and the speed at which controls in Max respond

from microbit import *
#set a refresh rate in msREFRESH = 50
#get datadef get_data(): x, y, z = accelerometer.get_x(), accelerometer.get_y(), accelerometer.get_z() a, b = button_a.was_pressed(), button_b.was_pressed()
#print data in a list for transmission over serial print(x, y, z, a, b)
#keep getting the datadef run(): while True: sleep(REFRESH) get_data()run()

Getting any serial data into Max is a straight-forward process, here the Serial Object is used, the baud rate is set (anyone my age will remember baud rates from our dial-up modems) and the port which the Micro:Bit is plugged into defined.

My Mac only has two USB ports so it's going to be either a or b. The baud rate is set to 115200 in the example but, again, experiment to find what suits your system/s best.

Once that's the serial data can be decoded and unpacked to sepearte outputs which can then be routed to control anything in Max!

To the left is my subpatch Serial_Receiver which does exactly this. It's implemented as a subpatch for use in any Max patch.