Showing posts with label codebug. Show all posts
Showing posts with label codebug. Show all posts

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.





Thursday, November 12, 2015

Codebug - Flashing LED tree made with conductive paint

Last year I made some conductive paint using this Instructable.  For me 50:25 by weight, paint to graphite powder works great. Make sure to stir in slowly otherwise the graphite powder will fly.
With this mix the paint is easy to apply yet has good conductivity.
It worked great and I used it at a Raspberry Jam to make flashing LED eyes for Halloween.

Since then the paint has been abandoned until my eldest daughter mentioned she was making circuits in school yesterday and I said I'd try to make them flashing LED Christmas Tree pictures.
This got a thumbs up from my daughters so the pressure was on.

Using Inkscape I drew a simple Christmas Tree with a place to put 7 LEDs.
Nice and simple and easy to print on A4 card.  I made the baubles the shape of a physical LED so you can see which way to place the LEDs on the sheet. All the negative (ground, cathode) sides in the middle making it easier to connect all to GND later.
PNG Version.
Links to PDF and SVG 

With the drawing done I added the LEDs.  6 to the green tips and a yellow one in the star.  I had 2mm LEDs to hand so that's what I used.  For the real thing I plan to use 5mm as the effect should be better.

I found pre-bending the LED leads at right angles so they are pointing down when in places made it easier to  get the LED to stand up at the front.  I also made small holes using a pin to push the LED leads through.  It's hard to push LEDs through 300gsm card as the leads don't have a point.

Once this was done I turned over the card.  Left the ground side for each LED pointing down and turned the positive side (anode) out at right angles pointing to the edge.  Once done I used a small piece of tape to keep the LEDs in place.
Tip.  Push the leads down to they push against the card. It's means the leads lie flat making them easier to hold on with just paint.

Back of the card
As I mentioned the conductive paint had spent the last year drying so was a bit sticky meaning the paint job is less than smooth.  I have since revived the paint with a little bit of water so for the kids it should be much easier to paint.

The bar down the middle is the ground tied to all the LEDs.  The other lines are for the positive side of each LED.
If you want all wires to be at the bottom then you may have to adjust the SVG in Inkscape to only print one Tree per A4 landscape card.

To test I thought I'd use a Codebug I got in the Kickstarter and hardly used it since. They are no on sale at CPC Farnell
With the help of Twitter people (I didn't declare the legs as outputs at the top of my code) I put together the following program. Link to CodeBug site for project

CodeBug code
Once all this was done the LEDs flashed. Yeah!.

For this project I wired up the LEDs so the top and bottom on one side are paired with the middle one on the other side and vice versa and the star LED on a leg of it's own.
This only uses three  (3) legs on the CodeBug,s o the 4th one (3) in the code is redundant.  I just left it in there in case I can figure out a more exciting way to use 4 outputs to flash 7 LEDs.

Very Short video of the final tree.


Next I want to wire up to a Raspberry Pi to enable interactive input and independent control of all the LEDs and maybe play a Christmas tune as well.

Sidenote; Some might be asking where the current limiting resistor is.  It's the paint.  The conductive paint has a higher resistance than wire and so it is acting like the current limiting resistor.