Your Bit:Bot has 12 multi-colour light emitting diodes (LEDs) 6 on the left side and 6 on the right side. These use the Red, Green, Blue (RGB) colour model and their colour is changed by changing the intensity of the RGB components. These Neopixels have a driver chip built into them allowing us to control them using just 1 pin from the Micro:bit. Without this driver we would need 3 PWM outputs from the Micro:Bit to control each LED.
We will use the Adafruit NeoPixel library to control these LEDs. The first example will light the LEDs individually then the second example will have the same result but use a for loop to make the code neater. We will then put the code in a funciton that can be reused in later projects. You will be able to download the code from the bottom of this page and from the Resources page.
Don’t be intimidated, it might seem that there is a lot of writing on this page but its not that bad, work through it at your own pace and try to understand what the code is doing. It is in 3 sections and the first section is the longest. The last 2 sections will introduce for loops and functions using pretty much the same code.
Firstly open Mu and start a new project by clicking the new button. Click the save button and save your project where you want it to be stored.
Now we’re going to import the libraries we need to use, firstly we will import the Micro:Bit library, this will be done for all Micro:Bit projects. Type;
Next we will need to import the NeoPixel library. Type;
Make sure that when your typing the code their is no white space at the start or end of the lines.
Now we’re going to create a NeoPixel object. This object will be stored in memory and represents the LEDs that are connected to pin 13 of the Micro:Bit. Type;
What we’re doing here is telling the Micro:Bit to create a NeoPixel object that represents real life LEDs connected to pin 13 of the Micro:Bit. We’re also telling it that there are 12 LEDs all connected to pin 13.
Next we are going to make each left LED light up orange for 300mS. We will skip straight to the program loop because we don’t need any global variables or functions. Type;
Click the Check button at the top of your Mu screen to make sure there are no errors. If you have errors correct them and click Check again. Keep doing this until all errors have gone then plug your Micro:Bit into the Bit:Bot and connect your Micro:Bit to your computer via the USB port and click Flash. This will upload the code to the Micro:Bit.
The Micro:Bit will reset once the code is uploaded. Make sure you switch the batteries on, on the Bit:Bot and you should see the left hand side NeoPixels light up one after the other in a whitey orange colour.
Everything that’s after np is a function of the NeoPixel library, so for instance np.show() is a function of the NeoPixel library. When we created the NeoPixel object np we created an array of NeoPixel objects the number in the brackets i.e. np[2] is how we select each individual LED. For instance np[0] is the first LED and np[5] is the last LED on the left hand side of Bit:Bot.
Now play about with the above code and make sure you understand how it works. Alter it to make the right hand side lights come on too, change the colour and try and alter it so the LEDs don’t go off after coming on and only go off when all LEDs are lit.
If your having problems getting the code to run download the zip file from the bottom of this page (it contains all examples from this tutorial) unzip it and open the relevant example in Mu, then upload to the Micro:Bit. Ensure that you type things using the same case if I’ve used upper case you use upper case and vice versa other wise the code will not work.
Lets go LOOPY!
You would have noticed that a lot of the code in the program loop is repeated with the NeoPixel number incrementing by 1 each time. We can tidy the code up by putting this in a for loop. Delete the program loop and type;
The above code can be used to replace the previous code that didn’t use a loop. It does the same thing however it is a lot easier to read and takes up less space. At the start of the for loop an integer variable called pixel_id is created. The in range part of the code tells Micro:Bit to execute the loop 6 times. After each loop iteration the variable pixel_id will be incremented by 1. This will in turn select the next LED. When pixel_id == 5 then it will go back down to 0 on the next loop iteration. If the 6 was swapped for 12 the LEDs on the right hand side would also come on in turn.
Enter the above code into Mu, check it then upload it to the Micro:Bit and watch it execute. If you have any issues download the code from the links at the bottom of this page.
Wrap it up in a Function
To make our code reusable and easier to read we can put the above loop into a function. NeoPixels latch on or off which means we can have multiple LEDs on simultaneously. If we didn’t use the line np[pixel_id] = (0, 0, 0) the LEDs would have stayed on once illuminating for the first time. We will use this idea encapsulated in a function to control which LEDs will come on and when.
Delete the program loop and we will write two simple functions that will light all of the left or all of the right LEDs.
As you can see a loop is used however we have used argument to decide what colour the LEDs should be. In this example all LEDs on the left or the right must be the same colour and all LEDs on either the left or the right will all be on or off. As you can see for the left LEDs we’re iterating through np[0] – np[5] and for the right LEDs we’re iterating through np[6] – np[11].
We can then invoke this function from the main program loop. Under the above code type;
The above code will turn the left lights red for 500mS then turn them off, then it will turn the right lights blue for 500mS. This will loop until the Micro:Bit is switched off. The full program is shown below;
Notice the spacing with the above code 2 lines between functions and the indentation that allows the Micro:Bit to know which lines of code belongs to which statements, etc.
This function can be improved giving more control over the LEDs but for now it will suffice. Save the above code in a convenient location so we can reuse it later. The functions developed here will be used in the rest of the example projects.
Well done you’ve written your first program in MicroPython, learned how to control multi-colour LEDs using the NeoPixel library, learned how to write for loops, while loops and write functions. We have covered a lot in this tutorial; don’t worry if you don’t fully understand everything, experiment with the code rewrite it, tweak it and make it do different things. When your ready move onto the next example project Light Detector Tutorial.
Code Files
All of the code from this tutorial can be downloaded from the following link;
The above zip archive also contains some extra pieces of code making the LEDs light up in cool ways. I have included a more advanced lighting function and a program loop that will light the LEDs like a rainbow. Have a play and see what you can discover.