Monday, December 14, 2015

RGB LED pixel ring with Raspberry Pi, Crumble and CodeBug


I've seen the Adafruit RGB LEDs in a number of projects and thought they were very cool. From doing some research they are controlled using a WS2812 and there are a number of sellers selling strips and rings based on the same driver and RGB LEDs.

On a recent trawl of eBay I came across the following RGB LED Ring 24 Bit WS2812B 5050 RGB LED with Integrated Driver 
24 pixel RGB LED ring

When it arrived I'd to solder a few wires to it.  5V, GBD and DI (in).  These pixels are controlled using a custom one wire protocol, so all 24 or more LEDs can be controlled with 1 pin.  There is also a DO (out) which allows you to chain the rings. Taking the DO from the first and attaching to the DI of the second.

Over the past year I've acquired a Crumble and a CodeBug in addition to the Raspberry Pi
Both the Crumble and CodeBug use a block based programming system and have the ability to control RGB LEDs, so to test the ring I started with the Crumble as it was nearest to me. I put together a short little program and it worked.  The code below is v2 with a small animation on collision.  For the Crumble it's connection D that you attach the LEDs to.


Crumble Code

Once I was sure it worked I noticed that CodeBug had an RGB LED example that I hadn't seen before. They call them GlowBugs. The web interface has the option for enabling the Star which attaches to the header and 5 or 10 GlowBugs. I enabled 10, did the code to make 10 change colour and it worked perfectly.  The great thing with the CodeBug is it is programmed using a web interface, nothing to install. When you do a download you just drag and drop the file to the CodeBug which is seen as a drive and that's it. No drivers or funky settings needed.
Since I had nothing to lose I set the counter to 24 to see if it would drive all 24 LEDs. Again success. So, the CodeBug can handle more than the pre-selected 10.  For the CodeBug it's connection 3 that you attach the LEDs to. You have to click the cog in the emulator  pane on the left and enable the GlowBug for them to work.






CodeBug Code 

That's 2 platform down.  Just the Raspberry Pi to go.
As mentioned above the protocol for these LEDs is custom and my understanding is timing specific and so getting it to work on the Raspberry Pi may be more difficult.

Then Twitter and the great +ForToffee  came to my rescue. with the following Tweet.

So, the UnicornHat from Pimoroni also uses the WS2812, brilliant. These guys usually have good libraries and code so I found it on GitHub

From the README.md I used the curl command to do the install.

\curl -sS get.pimoroni.com/unicornhat | bash

This was on an updated Jessie on a Raspberry Pi 2 and it installed perfectly.
I went to the examples folder and ran demo.py.  This is expecting an 8x8 matrix, but even on a 24 pixel ring it looked great.
One thing not noted, pr that I couldn't find is that the LEDs are connectioned to BCM pin 18, the hardware PWM pin.

Once it worked I then had to figure out how to address each pixel individually. 
As you'd expect the UnicornHat code is all designed for an 8x8 matrix and not a circle, so running the LEDs from 1-24 caused the actual LEDS to light up out of sequence..I thought I was going to have to do some kind of look up to match the X,Y for the matrix code with the individual LEDs but then looking at the GitHub repo there was more details on the WS2812 code.

At the bottom of the README.md was the following reference.

Based Upon rpi_ws281x

Unicorn HAT is based upon a modified, Pi 2 compatible version of the RPi ws281x Library by Jeremy Garff.
The library was modified by Richard Hirst.


So, I checked Richard Hirst's GitHub and found an example that didn't look too complicated.

OK, at first sight it looked complicated, but after reading it a bit I figured it out.
Colours are done in HEX
Line 16 in the code sets the number of LED. I changed this to 24 and ran the code.
Worked first time.


Looking at the code again.
Lined 25 - 35 are just setting up some preset colours.  

So, change line 16 to set the number of LEDs and lines 25-35 if you want to pre-set colours.

Other than this all code up to line 67 doesn't need to be touched.
It just works and my motto is if it aint broke don't fix it.

The try: just cycles through the LEDs setting their colours 

The two key commands in this block are. 

ws.ws2811_led_set(channel, i, color)
This sets a colour for LED i (Note LED numbers start from zero)

resp = ws.ws2811_render(leds)
This command actually does the setting.
So, if you want to change a number of LEDs at the same time use the ws.ws2811_led_set command for each LED and then run a single ws.ws2811_render command to actual set the LEDs.

After modifying the code I could control each LED.



In summary these RGB LED rings work just like Neopixel with the WS2812 driver. 
They work great with Crumble, CodeBug and Raspberry Pi. Coding is easy and special set up is only required for the Raspberry Pi, but with the UnicornHat library install it's really easy to get working with python.

Note:
These LED rings if driving a lot of the LEDs with bright colours can use a lot of power. I'd recommend powering from an external 5V supply. The Crumble specifically will not power them from the USB through the Crumble.
I have a Crumble battery holder for 3 x AA batteries that I used for each of the setups to power the LEDs, For the Crumble and CodeBug the batteries also powered the boards as well.  While for the Raspberry Pi I used an external 5V supply for the Raspberry Pi and the batteries for the LEDs making sure to connect the ground pins together to ensure that the whole circuit has the same GND reference.
I don't know how the actual UnicornHat powers the LEDs when on the Raspberry Pi

Side Note:
I haven't tested with Arduino yet, but since these things have been controlled by Arduino for years already I expect it to just work.  I have a Digispark ATTINY85 that is really small and is suppose to work as well since it's basically a minimal Arduino compatible.  So, will give that a try.