Have Yourself A WS2812B Christmas

I have an addiction to WS2812B "Neopixels", those super bright multi colour LEDs that work with almost every micro controller / computer.

I recently purchased some from eBay to see if they were any good.

I purchased a two 16 pixel rings.

alt

And a 24 pixel ring for less than £10, not bad. Delivery was slower than normal but hey ho!

alt

So now that I have them I decided to test the 24 pixel ring with my micro:bit and Micro Python and something that I had lying around from a #Pounderland summer teardown.

First things first...Soldering

The WS2812B rings have contacts on their reverse for easy soldering. I applied solder to the VCC, GND and IN contacts, readying them to receive some solid core hookup wire that I had also "tinned", applied solder to the wire for a better connection. Soldering the wires was really easy and soon I was ready to connect them to my micro:bit using three crocodile clips.

Get Connected

To connect the WS2812B ring to my micro:bit I connected to the following pins

micro:bit    WS2812B  
0            IN  
3V           VCC  
GND          GND  

Connections made I then plugged in my micro:bit to my laptop.

Cowsay "mu"

 ---- 
< mu >  
 ----
        \   ^__^
         \  (oo)\-------
            (__)\       )\/\
                ||----w |
                ||     ||

To program the ring I used Micro Python, and so I downloaded a copy of mu the Micro Python editor.

alt

Mu is a delight to use, clean and simple.
With mu I can create code and upload it to a connected micro:bit, or I can interactively work with the micro:bit via a REPL, Read Evaluate, Print, Loop session. Basically I can write code and see the results on my micro:bit...instantly.

It's Coding Time!

So the code for this project is rather simple.

Firstly we import the micro:bit library and then the neopixel library. Doing so gives us access to the GPIO pins on the micro:bit and enables us to to talk to our WS2812B / Neopixels.

from microbit import *  
import neopixel  

Next I created a variable which stores the configuration of my WS2812B pixels. They are connected to pin 0 of my micro:bit, and in this case I had 24 pixels in my neopixel ring. But you can change 24 to whatever number you have.

np = neopixel.NeoPixel(pin0, 24)  

We now move in to the main loop of the code. Inside a while True, a loop that will go round forever, we use a for loop, a loop that goes round for a certain number of times. How many times our for loop goes round is controlled by the number of neopixels we have. To work this out we use len() short for length, to count the pixels in the np variable we made earlier.

while True:  
    for pixel_id in range(0, len(np)):

For our first for loop we set all 24 of the pixels to red and we do this by using a tuple, a list of data that cannot be updated like a list, that contains the Red Green Blue values needed to make a colour. For example Red at full brightness is 255,0,0.
I saved the value to the variable red but instead of full brightness I set it to 1/4 brightness, as WS2812B are exceptionally bright!.

        red = (64,0,0)

We now write the code that will assign the colour red to each pixel one by one.

        np[pixel_id] = (red)

But we can't see the colour change!!! It's ok we need to tell the pixels to show the colour and then delay for a hundredth of a second before running the same code for the next pixel in the ring, until we reach the final pixel.

        np.show()
        sleep(10)

I then created two more for loops to change the colours to green.

    for pixel_id in range(0, len(np)):
        green = (0,64,0)
        np[pixel_id] = (green)
        np.show()
        sleep(10)

and finally blue.

    for pixel_id in range(0, len(np)):
        blue = (0,0,64)
        np[pixel_id] = (blue)
        np.show()
        sleep(10)

All of these for loops are inside the while True loop, meaning that the light sequence will go round and round, changing colours all Christmas...and possibly new year!

So now all of my code looks like this.

from microbit import *  
import neopixel

np = neopixel.NeoPixel(pin0, 24)

while True:  
    for pixel_id in range(0, len(np)):
        red = (64,0,0)
        np[pixel_id] = (red)
        np.show()
        sleep(10)

    for pixel_id in range(0, len(np)):
        green = (0,64,0)
        np[pixel_id] = (green)
        np.show()
        sleep(10)

    for pixel_id in range(0, len(np)):
        blue = (0,0,64)
        np[pixel_id] = (blue)
        np.show()
        sleep(10)

I clicked on Flash to upload the code to my micro:bit and then I sat back, relaxed and gazed into the multi-colour maelstrom of light that exploded from the ring!

Conclusion

alt I now have more flashing lights for Christmas, and so can you! Have a play with this code and see what you can make.
Also take a look at the Micro Python resources to learn more about this great board and the Micro Python language.

BONUS CONTENT!!! :)