Introduction: Python Micro:bit Light Show
In this tutorial, we will create a synchronised light-show using the LEDs on a Micro:bit.
This won't just be as a stand-alone device - we can create a network of Micro:bits that can all be controlled in unison.
Step 1: Message Broker
To help out with the networking side of this project, we are going to make use of a sophisticated, yet easy-to-use messaging protocol. This is known as MQTT and revolves around a message "Broker" that receives and forwards messages to whoever needs them.
The flow of messages is illustrated in the diagram above. This might seem unnecessarily complex, but it does allow us to send broadcast messages to many devices quickly and easily. This might be useful later on !
Step 2: Install MU Python Editor
In this tutorial, we will be using Python to program a Micro:bit. If you haven't got it already, you will need to download the MU editor. This is a simple, yet powerful Python editor and "flasher" for the Micro:bit.
Step 3: Flash Python Code to Micro:bit
Download the python script shown below and open it the MU editor (or cut-and-paste the code into MU).
Connect your Micro:bit to your computer with a USB cable and then click the "Flash" button (highlighted in red on the above screenshot).
The orange light on the Micro:bit should flash for a few seconds - when it stops, the code has been uploaded and it running on the Micro:bit !
Step 4: Test Your Micro:bit LEDs
Click on the "REPL" button at the top of the MU editor (circled in red on the screenshot)
This will open up the REPL panel at the bottom of the editor window (if it isn't open already !)
The REPL window allows to you send and receive data straight to the Micro:bit
Try typing '1' and '0' into this panel:
- 1 should turn all of the Micro:bit LEDs on
- 0 should turn all of the Micro:bit LEDs off
NOTE: You might need to press the "reset" button on the Micro:bit in order to get the LED controller code running. (The reset button is the one next to where the USB cable plugs into the Micro:bit)
Step 5: Close the MU Editor
Once you are sure the Micro:bit code is controlling the lights correctly, close the MU editor.
The next stage in this tutorial involves connecting directly to the Micro:bit - if MU is still running, it can interfere with the connection, so make sure you have quit it !
Step 6: Download Subscriber Code
Download and unzip the Subscriber code from the link below. This contains a python script that will connect to the message broker, receive incoming messages and then send control signals to your Micro:bit.
Step 7: Fill in the Device Name for Your Micro:bit
Open up the "Subscriber.py" file with any text editor you have available on your computer. Enter the device name of your Micro:bit on the line indicated in the code (on Windows, this will be "COM3" or similar. on a Mac, this will be "/dev/cu.usbmodem1421" or similar).
If you are unsure of the name of your Micro:bit device name, run the ListDevices.py script that came with the Subscriber code. This will list the device names of all of the Micro:bits currently connected to your computer.
Step 8: Fill in the IP Address for Your Broker
If you have access to an existing message broker, then just update the IP address in your "Subscriber.py" file so that your client connects to that broker.
If you would like to run your own message broker, follow the installation instructions found on the Mosquitto website. Additionally, Element 14 has a good tutorial for getting a broker up and running. Make sure you then update your "Subscriber.py" file with the correct IP address !
Step 9: Run Your Subscriber Script
The next step is to actually run your Subscriber script. Often the quickest way to do this is using a terminal window or command prompt (just make sure you CD into the directory containing your script, before you try to run it. For more information on running Python scripts, see the following pages for Windows and Mac.
When you run the Subscriber script, it should connect to your Micro:bit and then subscribe to the broker and wait for incoming messages.
Step 10: Publish Messages to Broker
For this exercise, you will only need one publisher running (no matter how many people are connecting to the message broker). If it's YOU that has to run a publisher, download the below zip file and run it using the python interpreter.
Make sure you update the IP address in the code, so that the publisher knows where to send the published messages.
Step 11: Enjoy the Light Show !!!
If everything worked correctly, the lights on your Micro:bit should flash on and off in time to the published messages.
If there are lots of people in the same room, all connecting to the same message broker, then all of their Micro:bits will flash in unison !!!
If you enjoyed this instructable, check out our other tutorials
If you love this kind of stuff, check out our degree