ESP8266/WS2812

From Noisebridge
Revision as of 19:22, 1 November 2015 by Thex (talk | contribs) (→‎UART)
Jump to navigation Jump to search

Using LED strips with ESP8266

Overview of an easy to use setup for connecting WS2812b LED strips with esp8266's, ideally using the ESP8266#Arduino_IDE for dev. There are various links below on different libraries, approaches and projects of doing such.

A fairly in depth look at the particulars of these LEDs and in particular how the data protocol works can be found here https://cpldcpu.wordpress.com/2014/01/14/light_ws2812-library-v2-0-part-i-understanding-the-ws2812/


Wiring

These LEDs are commonly found in strips with 30, 60, or 144 modules per meter. They are most typically configured with 3 connections at either end: ground, data and 5v. While the power connections can be connected at either end or anywhere in between, the data line is directional. Ideally the data line uses a resistor to between the GPIO pin and the strip, to drop the voltage to about 80% of the supply voltage. It is quite easy to burn out the first LED in a strip if you do not use a resistor, fortunately if this does happen, the first LED can be cut out or bypassed and the remaining LEDs will still work.

Alternatively you can power the LED strip directly with a sufficiently rated 5v DC power supply, or for shorter strips directly off of a USB connection. You can then drive teh data line directly from the GPIO of a an esp8266 at 3.3v and the lower voltage will function fine so long as you tie the grounds of the power sources together.


Software Libraries

I2S

This seems to be the most promising approach, using the specific architecture and optimizations available on the chip. Originally developed by cnlohr, JoDaNl has since made a ESP8266#Arduino_IDE friendly version available, that looks to be a work in progress, it is thus far functional.


UART

The NeoPixelBus was originally developed for use with Arduino hardware, and has since added support for the esp8266. Specifically there is a UART driven branch, while this version does work with the current version of the Arduino IDE (1.6.5) it is unclear as to how well this implementation functions overall and what the trade offs are. One advantage of this version however, is that you can use any GPIO pin, and conceivably multiple pins/strips as well.

Installation instructions are provided on the github page. The example code seemed to be a bit glitchy, below is a functional simple starter sketch.

#include <NeoPixelBus.h>

#define pixelCount 8
#define pixelPin 2  // make sure to set this to the correct pin

#define colorSaturation 128

NeoPixelBus strip = NeoPixelBus(pixelCount, pixelPin);
RgbColor red = RgbColor(colorSaturation, 0, 0);
RgbColor green = RgbColor(0, colorSaturation, 0);
RgbColor blue = RgbColor(0, 0, colorSaturation);
RgbColor white = RgbColor(colorSaturation);
RgbColor black = RgbColor(0);

const RgbColor colors[3] = {red, green, blue};

int pos = 0;

    
void setup()
{
    // this resets all the neopixels to an off state
    strip.Begin();
    strip.Show();
}


void loop()
{
    delay(1000);

    for (int i = 0; i < pixelCount; i++) {
      strip.SetPixelColor(i, colors[pos++]);
      if (pos > 2)
        pos = 0;
    }
    
    strip.Show();
}

Resources