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 method 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 methods 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;

from microbit import *

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

import neopixel

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;

np = neopixel.NeoPixel(pin13, 12)

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 methods. Type;

while True:  # start of infinite program loop
    np[0] = (233, 108, 45)  # Set the colour for np 0, (RED, GREEN, BLUE)
    np.show()  # show the colour
    sleep(300)  # pause for 300mS
    np[0] = (0, 0, 0)  # turn the np off
    np.show()
    np[1] = (233, 108, 45)
    np.show()
    sleep(300)
    np[1] = (0, 0, 0)
    np.show()
    np[2] = (233, 108, 45)
    np.show()
    sleep(300)
    np[2] = (0, 0, 0)
    np.show()
    np[3] = (233, 108, 45)
    np.show()
    sleep(300)
    np[3] = (0, 0, 0)
    np.show()
    np[4] = (233, 108, 45)
    np.show()
    sleep(300)
    np[4] = (0, 0, 0)
    np.show()
    np[5] = (233, 108, 45)
    np.show()
    sleep(300)
    np[5] = (0, 0, 0)
    np.show()

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;

while True:
    for pixel_id in range(0, 6):
        np[pixel_id] = (233, 108, 45)
        np.show()
        sleep(300)
        np[pixel_id] = (0, 0, 0)
        np.show()

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 Method

To make our code reusable and easier to read we can put the above loop into a method. 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 method to control which LEDs will come on and when.

Delete the program loop and we will write two simple methods that will light all of the left or all of the right LEDs.

def leftLights(Red, Green, Blue):
    for pixel_id in range(0, 6):
        np[pixel_id] = (Red, Green, Blue)
    np.show()

def rightLights(Red, Green, Blue):
    for pixel_id in range(6, 12):
        np[pixel_id] = (Red, Green, Blue)
    np.show()

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 method from the main program loop. Under the above code type;

while True:
    leftLights(32, 0, 0)
    sleep(500)
    leftLights(0, 0, 0)
    rightLights(0, 0, 32)
    sleep(500)
    rightLights(0, 0, 0)

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;

# NeoPixel example (Looped + Method) for the 4tronix Bit:Bot and BBC Micro:Bit   
# Author David Bradshaw 2017
# Demonstrates how to use the neoPixel library
from microbit import *
import neopixel  # Neopixel Library so we can control the NeoPixels lights

np = neopixel.NeoPixel(pin13, 12)


def leftLights(Red, Green, Blue):
    for pixel_id in range(0, 6):
        np[pixel_id] = (Red, Green, Blue)
     np.show()


def rightLights(Red, Green, Blue):
    for pixel_id in range(6, 12):
        np[pixel_id] = (Red, Green, Blue)
    np.show()


while True:
    leftLights(32, 0, 0)
    sleep(500)
    leftLights(0, 0, 0)
    rightLights(0, 0, 32)
    sleep(500)
    rightLights(0, 0, 0)

Notice the spacing with the above code 2 lines between methods and the indentation that allows the Micro:Bit to know which lines of code belongs to which statements, etc.

This method 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 methods 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 methods. 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;

NeoPixelExamples.zip

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 method and a program loop that will light the LEDs like a rainbow. Have a play and see what you can discover.

Comments have been enabled if you have any feedback or any NeoPixel code to upload.

2 thoughts on “1. NeoPixel Tutorial

  1. Avatar
    Y8 Computer Club says:

    Hi,
    Thanks for the site. We are trying the Neopixels examples but all our lights always stay white. We have downloaded your zipped files and tried those as well but still, we only get white lights!
    Any suggestions David?
    Thanks
    🙂

    • David Bradshaw
      David Bradshaw says:

      If you copy and paste NeoPixelLooped.py into Mu and upload it to the Micro:Bit the lights on the right hand side of Bit:Bot will turn on one after the other and light a whitey orange colour. To change the colour you need to change the RGB values of the neoPixel object, to do this change the values in the square brackets on line 13.

      So for instance change the line to

      np[pixel_id] = (100, 0, 0)

      and your neoPixels will light up red. np[pixel_id] = (0, 100, 0) for green and np[pixel_id] = (0, 0, 100) for blue.

      The RGB values are the intensity of each colour component; if they’re all equal the light will be white i.e. [20,20,20] will give you a dim white light where as [200,200,200] will give you a bright white light. Experiment to get different colours; the RainBow.py example is a good one to look at to see how to make different colours.

      If your still having issues reply to this post.

Leave a Reply

Your email address will not be published. Required fields are marked *