WEMOS – Using an addressable LED strip

wemos_addressable

Equipment

  • Wemos D1
  • Addressable WS2812 LED strip

Thinking

Addressable LEDs can be useful for numerous things, whether acting as indicators, displaying images or simply providing coloured light. The can come individually or in a strip and can be ‘chained’ together with just three connections: 5v, Data and GND. This makes it very easy to build more complex displays and there are minimal connections back to the controller.

They’re easy to use on the WEMOS, you just need to add the correct library and then set the colours for the LEDs you want lit. This example aims to get you started and then direct you to other resources to expand your knowledge and skills.

Please note there are other very similar LEDs based on different chips, this tutorial is specifically for WS2812.

Doing

This assumes that you have the Arduino IDE installed and that the strip in this case is made up from WS2812b LEDs.

Let’s write the code, and first we need to add the appropriate library.

Run the Arduino IDE and select Sketch->Include Library->Manage Libraries from the menu bar.

Type neopixel into the search field to narrow down the list and you should see Adafruit Neopixel, select it and click the Install button. In my case it was version 1.1.3.

Create a simple sketch with sets the first LED to blue, the code is:

#include <Adafruit_NeoPixel.h>

#define PIN D4

Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
    strip.begin();
    strip.show(); // Initialize all pixels to ‘off’
}

void loop() {
    strip.setPixelColor(0, strip.Color(0, 0, 255));
    strip.show();
}

What happens is first we define which pin the strip is connected to. Then we create an object to represent the strip and in the setup() function we initialise the strip. The process of setting the colours of the pixels is a two step process. Firstly we need to set the values for each pixel and then we need to call the show() method to ‘write’ those values to the strip for them to be visibile. In the setup() function you’ll see that we initialise the strip and then call show, without seeming to set any values, this is because the values are all initialised to zero.

There are two lines of code in the above which need some explanation.

The first is the line that initialises the strip:

Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, PIN, NEO_GRB + NEO_KHZ800);

This calls the Adafruit_NeoPixel() function in the library that we’ve inclued. This function has three parameters:

  • The number of LEDs on the strip, 4 in this case
  • The pin that the strip is connected to on the Wemos, D4 in this case
  • Some flags which tell the library what type if strip it is and how to talk to it – these can be left as the default values above for WS2812

The second line of interest is the one that changes the colour:

    strip.setPixelColor(0, strip.Color(0, 0, 255));

This has two parameters:

  • The index of the LED to be set, this is zero based so the LED closest to the Wemos is LED zero and the next is one, etc.
  • The colour value to set the LED to

The LED effectively has 3 individual LEDs in one housing, a red one, a green one and a blue one (often abreviated as RGB). The brightness values of the red, green and blue components can be set individually and in the case of the Color() method these values are between zero and 255. Combinations of these values give us the ability to set millions of different colours on the LED. The parameters are in the order that I’ve described:

  • Red value
  • Green value
  • Blue value

RGB colour values, as they are referred to are, quite common in programming and Googling for RGB colour picker returns numerous options for sites where you can click on the colour and the corresponding RGB values will be displayed.

Next we’ll wire up the WEMOS.

There are three connections required for the addressable strip:

  1. 5v
  2. Data
  3. Gnd

WARNING: LEDs require power and may pull up to 60mA or more per LED. Therefore check the documentation for your particular strip and calculate the total current required. It may be that you need to power the strip separately to the WEMOS.

For this example we are using a short strip, only 4 LEDs so current should not be a problem. Also please note that the output pins on the WEMOS are only 3.3v and the LEDs work at 3.3v. However again this only appears to cause an issue with longer strips. The strips are directional and data will flow only in the direction of the arrows marked on the strip. Therefore the D1 should be connected to the end of the strip with the arrows pointing away from it.

The connections are as follows:

LED WEMOS
5v 5v
GND G
data D4

That’s it, upload the sketch and the first LED should light up blue.

This example is based on the strandtest example that comes with the library. Strandtest has lots of different variations of colours and display cycles. In order to use it select File->Examples->Adafruit Neopixel->strandtest and then save it somewhere on your machine. You will need to change the PIN to be D4, as in the code above, and also the first parameter to the Adafruit_Neopixel() function call to be 4, which is the number of pixels.

Have fun!