ūüö• Arduino and LED address strip WS2812B

In this article, we will learn how to work with addressable LED RGB tape WS2812B. The tape consists of RGB pixels WS2812B in an LED 5050 package (the physical dimensions of each element are 5 √ó 5 mm).

Each pixel contains red, green and blue LEDs and a PWM controller, with which you can control the brightness of each LED and get a lot of different colors from the three main ones.

Since each WS2812B consists of three LEDs and a PWM controller, it is better to call them pixels rather than LEDs. In the photo on the right, you can see the device of this pixel.

A little about the characteristics of the address LEDs WS2812B:

  • Power supply: 5 ¬Ī 0.5 V;
  • Current consumption: ~ 20 mA one LED, i.e. ~ 60 mA whole pixel.

At the beginning, we need to connect the LEDs to the Arduino. Make it extremely simple. Contacts + 5V and GND are connected to the positive and negative power supply, respectively. Din is connected to an Arduino digital pin, by default it will be the 6th digital pin, but youcan use any other.

In addition, it is advisable to connect the ground of the Arduino with the ground of the power source. It is undesirable to use the Arduino as a power source since the + 5V output can provide only 800 mA of current. This is enough for no more than 13 pixels of the LED strip.

On the other side of the tape, there is a Do output, it connects to the next tape, allowing you to control the tapes in a cascade manner, like one. The power connector at the end is also duplicated.

 

Next, we will deal with the management of the tape. A description of the control protocol is present in the Datasheet for WS2812B.

All pixels are by default connected to each other in series. Din input of each of them is connected to the output Do of the next. The control signal must come to the first one.

Control commands are given in packets of 3 bytes, one for each of the three colors. Between packets, there is a pause with a duration of 50 ¬Ķs, a pause of more than 100 ¬Ķs means the end of the transmission.

The duration of any bit is 1.25 ¬Ķs. A bit ‚Äú1‚ÄĚ is encoded by a pulse with a duration of 0.8 őľs and a pause of 0.45 őľs. The ‚Äú0‚ÄĚ bit is 0.4 and 0.85 őľs. Time differences up to 150 ns are possible. Such a packet must be sent for each pixel in the LED strip. The diagram shows the desired signal.

Most Popular Libraries:

FastLED

Supports all versions of Arduino and a variety of data transfer protocols (not only for our tape). The programming language in which it is written is pure C.

Adafruit NeoPixel

The library is designed to work with LED rings NeoPixel Ring, the development and production of Adafruit. It works slower, has less power, but contains only the most necessary. Written in C, assembly language and some Wiring. Supports the entire line of Arduino.

CONNECTING WS2812B IN ARDUINO IDE

Let's try both libraries and compare them. Let's write a standard Blink sketch so that the tape lights up red for half a second and turns off for the same interval.

By default, the number of pixels in the tape is 30, but if necessary it can be changed in the sketch.

An example using the FastLED library:

#include "FastLED.h"
#define LED_COUNT 30
#define LED_PIN 6
CRGB strip[LED_COUNT];

void setup()
{
FastLED.addLeds<WS2812B, LED_PIN, RGB>(strip, LED_COUNT);
}

void loop()
{
for (int i = 0; i < LED_COUNT; i++)
{
strip[i] = CRGB::Red; 
}
FastLED.show();
delay(500);
for (int i = 0; i < LED_COUNT; i++)
{
strip[i] = CRGB::Black;
}
FastLED.show();
delay(500);
}

The sketch will occupy 3758 bytes in program memory of Arduino and 187 bytes of RAM.

Now let's try the same thing with the Adafruit NeoPixel library:

#include "Adafruit_NeoPixel.h"

#define LED_COUNT 30

#define LED_PIN 6

Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup()
{
strip.begin();
}

void loop()
{
for (int i = 0; i < LED_COUNT; i++)
{
strip.setPixelColor(i, strip.Color(255, 0, 0)); 
}
strip.show();
delay(500);
for (int i = 0; i < LED_COUNT; i++)
{
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
strip.show();
delay(500);
}

The sketch will take 2592 permanent and 40 bytes of Arduino RAM.

As you can see, the FastLED library is more resource-intensive. In addition, using it in Arduino with 2 KB of RAM (such as, for example, UNO), you can control up to 600 pixels of the LED strip. This is due to the fact that 3 bytes of memory are reserved for each pixel.

But in Adafruit NeoPixel the minimum necessary functions and less memory consumption. Which one to use is your choice.

  • Jul 28, 2019
  • Category: Arduino
  • Comments: 0
Leave a comment

Please note, comments must be approved before they are published

Shopping Cart
0
No products in the cart.