Saturday, October 3, 2015

Neopixels / WS2812 Intro

What are Neopixels.

Neopixels (a name used by Adafruit) are officially called WS2812 leds. They are RGB leds but have a little extra. The extra is an addon chip build into the led itself. This makes it possible that the led only uses 1 lead (called D-in) instead of the usual 3 leads for R,G and B. But it gets even better. We daisy-chain the leds, meaning that you attacht the D-in from a led to the D-out from the previous led. This way we only need to use 1 pin of our microcontrololer to control many leds. Controlling means choosing the RGB color the WS2812 has to display. But it still gets better. As each led has its own control chip the led knows where it is in the lineup. Therefore we can give each WS2812 a different RGB color. This gives us endless possibilities. And the best part is that these Neopixels work with Arduino, Attiny85 and even the famous ESP8266 board. 


Let me give you an example that demonstrates how efficient these neopixels can be used.
An Attiny85 has just 5 usable pins. You can attach 5 leds to those pins or attach 1 RGB led and two switches or sensors. However we can attach 100 neopixels to that same Attiny85 and still have 4 pins left for switches and/or sensors. And the Attiny has just 8k memory. Now imagine how many Neopixels can be attached to an Arduino or to an ESP8266 with its enormous memory........

A closer look.

Neopixels are sold in rings with 12, 16 or 24 leds, strips with 8 to an infinite number of these leds, matrixes with 8x8 leds, or just as individual leds.

In this short tutorial I will be concentrating on the individual leds. But all info can easily be adapted to the rings or strips.

If you buy the WS2812 leds as individiual leds they are sold as blocks of 10 leds attached to eachother.




To use them you carefully have to break them where they are attached to eachother. Now the picture above makes them seem large leds. In fact they are very small as the picture below shows you.





Now let's have a closup look at an individual led:



You can clearly see 3 indivual silver islands which in reality are the R,G and B led. And on one of them you can see a tiny black chip which is wired to the silver parts.

Now this chip is the real genius in the WS2812. It not only tels the led which colour(s) should be put on or off but also tells the led which number it has in the lineup of leds. This will be clear when you look at the back of the led. It looks like this:




As you can see each Neopixel has 6 connections: 2 x GND, 2 x +5V, 1 Din and 1 Dout. It has no R,G or B connection. Choosing the colors is done by the chip.

The connection of the Led is as follows.

At the Din side of the chip the GND is connected to GND of your project and +5V to the 5 volt lead of your project. You may use a lower (like 3.3) voltage but that wil dim the leds. The actual Din pin is connected to your micro-controller. However it is not directly connected to the microcontroller. 

Adafruit (which has an excellent guide on the Neopixels) suggest you should connect a 300-500 ohm resistor between the pin of your microcontroller and the first Neopixel in line.
Further you should attach a large capacitor (1000 microfahrad at 6.3 volts or higher) across the GND and + of the power supply.

This will make the setup look like this:




Schematically it looks like this:




So what if you need more leds. Well here is the breadboard layout and schematic:





As you can see you attach the Data-In from the new led to the Data-OUT from the previous led. And Each led has its own +5Volt and GND connections. In real life you can pass them through as each led conveniently has 2 5Volt and 2 GND connections. The next picture will show you an example:



Obviously this also shows you that however small the Neopixels are it is possible to solder wires to them. Quite easily to be frank.



I use a clothespin to hold them while I solder them:



The next pictures clearly show you how bright they are and how easily they can be fed by 3 AAA batteries. And multiple Neopixels can easily be controlled by an Attiny85.





Power Usage:

Neopixels can work at Voltages as low as 3.3 volts and up to 6 volts. So this makes them ideal for projects that are build with Arduino's, Attiny's or even ESP8266 processors. However if you drop the voltage to 3.3 volt they will be less bright. So best practice is to operate them at 5 Volt and power an ESP8266 over a regulator.

Each Neopixel will draw about 60 miliamps when they are at full bright white. About the same like normal leds (20 miliamps per colour). So 4 Neopixels at full bright white will draw about 240 milliamps. However that will almost never happen.
At a minimum they will draw 20 milliamps (just one colour at its brightest).

So a rule of thumb is to multiply the number of Neopixels by 20 and divide the outcome by 1000.

In this case 4 Neopixels will at avarage draw: (4 x 20) / 1000 = 0.08 = 80 milliamps.

Arduino


To use the Neopixels with Arduino there is a great Library from Adafruit. You can find it here:
https://github.com/adafruit/Adafruit_NeoPixel

So the usage is very simple as the next program shows:




The program first calls the Neopixel library. Then the neopixels are attached to PIN 1 of the Attiny85. However you can use any digital pin for this. If you want to use the PWM function though you have fewer choices: just PIN 0 and PIN 1.

The variable NUMPIXELS defines how many Neopixels are in the Chain. I used 7. And then the most important statement:

pixels.setPixelcolor(R,G,B) 
in which the R,G abd B values can be between 0 and 255. As the Neopixels are attached to the Attiny85's PWM pins we can regulate the intensity of each colour which gives theoretically 16 million colors. In reality much less variations will be visisble for the human eye. But just experiment.

Here is the Breadboard layout:



As you can see I just added an Attiny85 with the power leads and the Neopixels connected through a resistor to PIN 1. So we need just 1 pin from the Attiny85 and can adress loads of neopixels !!!!!

ESP8266 with Arduino IDE

An enormous popular processor is the ESP8266. And luckily we can use it with the Arduino IDE and there is a library for it. You can get the Arduino Library from Adafruit at their Github repository.
Installing can be done the old-school way by downloading the Library and putting it in the Library folder of your Arduino IDE. Another way is to use the Arduino IDE Library Manager. In the Library Manager search for Adafruit Neopixel.



Please be aware that I was using NodeMCU version V0.9. This version has a 5 volt connection at the pin at the bottom right side in this Fritzing schematic. In your particular case that might no longer be the case. The new NodeMCU's have V-IN at that pin. So then you do need to attach the red power lead to the 3.3 volt pin which is the right upper pin in this schematic. The neopixels worked flawlessly at 3.3volts, so that will present no problem.


I attached my string of neopixels to GPIO2 (at pin D4) of my NodeMCU and downloaded the same code with the Arduino IDE. And it worked flawlessly. All the leds started blue and then one led at a time was changed to red and then back to blue.

You can test my code or use the examples which are included in the Adafruit Neopixel Library. They work flawlessly. It demonstrates how well the Arduino IDE works for the ESP8266. The strandtest example shows some very interesting effects.

ESP 8266 with LUA


As you will know by now the NodeMCU (or just the ESP8266) can also programmed with the LUA language. And LUA already has build-in support for Neopixels. So that makes programming even easier as we do not have to load libraries first.

As a setup we use the same schematic as seen above.

Now open your favorite editor: ESPlorer and just type in a snippet the following code (or copy it):

ws2812.writergb(4, string.char(0, 0, 255):rep(7))


Press the RUN button and all 7 Neopixels will turn blue
Now edit the snippet in the following line (or just copy it in):

ws2812.writergb(4, string.char(0, 255, 0, 255, 255, 255))


And the first 2 neopixels will change color in green and white.

Again change the snippet into:

ws2812.writergb(4, string.char(0, 255, 0, 255, 255, 255):rep(3))
 

And the first 6 Neopixels will change color in greens and whites and the last Neopixel will remain blue. The code rep(3) just makes sure that the previous RGB values are reapeated 3 times.

Neat huh ???
And easy understandable.


Ok that's it for now.
Till next time. And have fun.

Luc Volders