How to connect a WS2812B addressable LED strip to an Arduino
The development of LED-based lighting technology continues apace. Yesterday it seemed like a miracle that RGB ribbons controlled by a controller, the brightness and color of which can be adjusted by remote control. Today, there are even more possibilities on the market.
LED strip based on WS2812B
Differences of addressable LED strip from standard RGB is that The brightness and color ratio of each element is adjustable separately. This allows you to get lighting effects that are not available for other types of lighting fixtures. The LED strip is controlled in a well-known way - by pulse width modulation. The peculiarity of the system is that each LED has its own PWM controller. The WS2812B chip is a tri-color light-emitting diode and a control circuit, combined in one package.
The elements are combined into a tape on the power supply in parallel, and are controlled via a serial bus - the output of the first element is connected to the control input of the second, and so on. In most cases the serial buses are built on two lines, one of which transmits strobes (synchronous pulses), and the other - data.
The control bus of the WS2812B chip consists of one line - it transmits data. The data is encoded as pulses of a constant frequency, but with different frequencies. One pulse is one bit. The duration of each bit is 1.25 µs, the zero bit consists of a high level of 0.4 µs and a low level of 0.85 µs. The unit looks like a high of 0.8 µs and a low of 0.45 µs. A parcel of 24 bits (3 bytes) is sent to each LED, followed by a pause in the form of a low level for 50µs. This means that data for the next LED will be transmitted next, and so on for all elements of the chain. The data transfer ends with a 100 µs pause. This means that the ribbon programming cycle is complete and the next set of data packets can be sent.
This protocol allows a single line for data transmission, but requires precise timing. The discrepancy is not more than 150 ns. In addition, the noise immunity of this bus is very low. Any interference of sufficient amplitude can be perceived by the controller as data. This imposes limitations on the length of the wires from the control circuitry. On the other hand, it provides the ability to to verify that the ribbon without additional devices. If you apply power to the luminaire and touch the contact pad of the control bus with your finger, some LEDs may turn on and off chaotically.
Technical characteristics of WS2812B elements
To create lighting systems based on the addressable ribbon it is necessary to know the important parameters of light-emitting elements.
LED dimensions | 5x5 mm |
PWM modulation frequency | 400 Hz |
Current consumption at maximum brightness | 60 mA per element |
Supply voltage | 5 volt |
Arduino and WS2812B
The popular Arduino platform in the world allows you to create sketches (programs) to control addressable ribbons. The capabilities of the system are wide enough, but if at some level they are no longer enough, the acquired skills will be enough to seamlessly switch to C++ or even assembler. Although, it is easier to get the basic knowledge on Arduino.
Connecting a WS2812B based ribbon to an Arduino Uno (Nano)
At the first stage, a simple Arduino Uno or Arduino Nano board is enough. Later on, more complex boards can be used to build more complex systems. When physically connecting the addressable LED strip to the Arduino board, you have to make sure that a few conditions are met:
- due to low noise immunity, the data line connection conductors should be as short as possible (you should try to make them within 10 cm);
- Connect the data wire to a free digital output of the Arduino board - it will be indicated later by the software;
- Due to the high power consumption it is not necessary to power the strip from the board - separate power supplies are provided for this purpose.
It is necessary to connect the common power supply wire of the luminaire and the Arduino.
Fundamentals of WS2812B software control
It has already been mentioned that to control the WS2812B chips you need to form pulses with a certain length, withstanding high accuracy. In the Arduino language there are commands to form short pulses delayMicroseconds и micros. The problem is that the resolution of these commands is 4 microseconds. That is, it is not possible to form the time delays with a given accuracy. It is necessary to go to C++ or Assembler tools. It is possible to control the addressable LED strip via Arduino with the help of libraries specially created for this purpose. You can start with the program Blink, which makes the light-emitting elements blink.
FastLed
This library is universal. In addition to the addressable ribbon, it supports many devices including SPI controlled ribbons. It has a wide range of features.
First you must connect the library. This is done before the setup block, and the string looks like this:
#include
The next step is to create an array for storing the colors of each light emitting diode. It will have the name strip and dimension of 15 elements.
CRGB strip[15]
In the setup block you must specify the ribbon the script will work with:
void setup() {
FastLED.addLeds< WS2812B, 7, RGB>(strip, 15);
int g;
}
The RGB parameter sets the sequence of colors, 15 means the number of LEDs, 7 is the number of the output assigned to control (it is better to assign a constant to the last parameter as well).
The loop block starts with a loop that sequentially writes to each section of the Red (red glow) array:
for (g=0; g< 15;g++)
{strip[g]=CRGB::Red;}
Then the generated array is sent to the illuminator:
FastLED.show();
Delay 1000 milliseconds (seconds):
delay(1000);
Then you can turn off all elements in the same way, writing black in them.
for (int g=0; g< 15;g++)
{strip[g]=CRGB::Black;}
FastLED.show();
delay(1000);
After compiling and loading the sketch the ribbon will flash with a period of 2 seconds. If you want to control each color component separately, then instead of string {strip[g]=CRGB::Red;} several strings are used:
{
strip[g].r=100;// set the luminescence level of the red element
strip[g].g=11;// the same for green
strip[g].b=250;// the same for blue
}
NeoPixel
This library works only with NeoPixel Ring LEDs, but it is less resource-intensive and contains only the essentials. In Arduino the program looks like this:
#include
As in the previous case, the library is plugged in, and the lenta object is declared:
Adafruit_NeoPixel lenta=Adafruit_NeoPixel(15, 6);// Where 15 is the number of elements and 6 is the assigned output
The lenta is initialized in the setup block:
void setup() {
lenta.begin ()
}
In the loop block all elements are lit red, a variable is passed to the tape and a 1 second delay is created:
for (int y=0; y<15;y++)// 15 - number of elements in the luminaire
{lenta.setPixelColor(y, lenta.Color(255,0,0))};
lenta.show();
delay(1000);
The lenta stops glowing by writing black:
for (int y=0; y< 15;y++)
{ lenta.setPixelColor(y, lenta.Color(0,0,0))};
lenta.show();
delay(1000);
Video lesson: Samples of visual effects using addressable ribbons.
After learning how to flash LEDs, you can continue the lesson and learn how to create color effects, including the popular "Rainbow" and "Northern Lights" with smooth transitions. The WS2812B and Arduino addressable LEDs give you almost limitless possibilities for this.