1. NeoPixel Tutorial


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.

Rapidtables is a great website to get colour codes from. If you have a colour in mind go to Rapidtables to get the RGB values.

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;

[pastacode lang=”python” manual=”from%20microbit%20import%20*” message=”Import Statement” highlight=”” provider=”manual”/]

Next we will need to import the NeoPixel library. Type;

[pastacode lang=”python” manual=”import%20neopixel” message=”Import the NeoPixel Library” highlight=”” provider=”manual”/]

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;

[pastacode lang=”python” manual=”np%20%3D%20neopixel.NeoPixel(pin13%2C%2012)” message=”Create a NeoPixel Object Attached to pin 13 with 12 LEDs” highlight=”” provider=”manual”/]

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;

[pastacode lang=”python” manual=”while%20True%3A%20%20%23%20start%20of%20infinite%20program%20loop%0A%20%20%20%20np%5B0%5D%20%3D%20(233%2C%20108%2C%2045)%20%20%23%20Set%20the%20colour%20for%20np%200%2C%20(RED%2C%20GREEN%2C%20BLUE)%0A%20%20%20%20np.show()%20%20%23%20show%20the%20colour%0A%20%20%20%20sleep(300)%20%20%23%20pause%20for%20300mS%0A%20%20%20%20np%5B0%5D%20%3D%20(0%2C%200%2C%200)%20%20%23%20turn%20the%20np%20off%0A%20%20%20%20np.show()%0A%20%20%20%20np%5B1%5D%20%3D%20(233%2C%20108%2C%2045)%0A%20%20%20%20np.show()%0A%20%20%20%20sleep(300)%0A%20%20%20%20np%5B1%5D%20%3D%20(0%2C%200%2C%200)%0A%20%20%20%20np.show()%0A%20%20%20%20np%5B2%5D%20%3D%20(233%2C%20108%2C%2045)%0A%20%20%20%20np.show()%0A%20%20%20%20sleep(300)%0A%20%20%20%20np%5B2%5D%20%3D%20(0%2C%200%2C%200)%0A%20%20%20%20np.show()%0A%20%20%20%20np%5B3%5D%20%3D%20(233%2C%20108%2C%2045)%0A%20%20%20%20np.show()%0A%20%20%20%20sleep(300)%0A%20%20%20%20np%5B3%5D%20%3D%20(0%2C%200%2C%200)%0A%20%20%20%20np.show()%0A%20%20%20%20np%5B4%5D%20%3D%20(233%2C%20108%2C%2045)%0A%20%20%20%20np.show()%0A%20%20%20%20sleep(300)%0A%20%20%20%20np%5B4%5D%20%3D%20(0%2C%200%2C%200)%0A%20%20%20%20np.show()%0A%20%20%20%20np%5B5%5D%20%3D%20(233%2C%20108%2C%2045)%0A%20%20%20%20np.show()%0A%20%20%20%20sleep(300)%0A%20%20%20%20np%5B5%5D%20%3D%20(0%2C%200%2C%200)%0A%20%20%20%20np.show()” message=”Do Some Cool Stuff in a While Loop” highlight=”” provider=”manual”/]

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;

[pastacode lang=”python” manual=”while%20True%3A%0A%20%20%20%20for%20pixel_id%20in%20range(0%2C%206)%3A%0A%20%20%20%20%20%20%20%20np%5Bpixel_id%5D%20%3D%20(233%2C%20108%2C%2045)%0A%20%20%20%20%20%20%20%20np.show()%0A%20%20%20%20%20%20%20%20sleep(300)%0A%20%20%20%20%20%20%20%20np%5Bpixel_id%5D%20%3D%20(0%2C%200%2C%200)%0A%20%20%20%20%20%20%20%20np.show()” message=”Tidy the Code up with a Loop” highlight=”” provider=”manual”/]

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.

[pastacode lang=”python” manual=”def%20leftLights(Red%2C%20Green%2C%20Blue)%3A%0A%20%20%20%20for%20pixel_id%20in%20range(0%2C%206)%3A%0A%20%20%20%20%20%20%20%20np%5Bpixel_id%5D%20%3D%20(Red%2C%20Green%2C%20Blue)%0A%20%20%20%20np.show()%0A%0Adef%20rightLights(Red%2C%20Green%2C%20Blue)%3A%0A%20%20%20%20for%20pixel_id%20in%20range(6%2C%2012)%3A%0A%20%20%20%20%20%20%20%20np%5Bpixel_id%5D%20%3D%20(Red%2C%20Green%2C%20Blue)%0A%20%20%20%20np.show()” message=”Stick the Code in a Function” highlight=”” provider=”manual”/]

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;

[pastacode lang=”python” manual=”while%20True%3A%0A%20%20%20%20leftLights(32%2C%200%2C%200)%0A%20%20%20%20sleep(500)%0A%20%20%20%20leftLights(0%2C%200%2C%200)%0A%20%20%20%20rightLights(0%2C%200%2C%2032)%0A%20%20%20%20sleep(500)%0A%20%20%20%20rightLights(0%2C%200%2C%200)” message=”Call the Function” highlight=”” provider=”manual”/]

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;

[pastacode lang=”python” manual=”%23%20NeoPixel%20example%20(Looped%20%2B%20function)%20for%20the%204tronix%20Bit%3ABot%20and%20BBC%20Micro%3ABit%20%20%20%0A%23%20Author%20David%20Bradshaw%202017%0A%23%20Demonstrates%20how%20to%20use%20the%20neoPixel%20library%0Afrom%20microbit%20import%20*%0Aimport%20neopixel%20%20%23%20Neopixel%20Library%20so%20we%20can%20control%20the%20NeoPixels%20lights%0A%0Anp%20%3D%20neopixel.NeoPixel(pin13%2C%2012)%0A%0A%0Adef%20leftLights(Red%2C%20Green%2C%20Blue)%3A%0A%20%20%20%20for%20pixel_id%20in%20range(0%2C%206)%3A%0A%20%20%20%20%20%20%20%20np%5Bpixel_id%5D%20%3D%20(Red%2C%20Green%2C%20Blue)%0A%20%20%20%20%20np.show()%0A%0A%0Adef%20rightLights(Red%2C%20Green%2C%20Blue)%3A%0A%20%20%20%20for%20pixel_id%20in%20range(6%2C%2012)%3A%0A%20%20%20%20%20%20%20%20np%5Bpixel_id%5D%20%3D%20(Red%2C%20Green%2C%20Blue)%0A%20%20%20%20np.show()%0A%0A%0Awhile%20True%3A%0A%20%20%20%20leftLights(32%2C%200%2C%200)%0A%20%20%20%20sleep(500)%0A%20%20%20%20leftLights(0%2C%200%2C%200)%0A%20%20%20%20rightLights(0%2C%200%2C%2032)%0A%20%20%20%20sleep(500)%0A%20%20%20%20rightLights(0%2C%200%2C%200)” message=”Full Code Listing” highlight=”” provider=”manual”/]

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.