Learning WS2811 & FastLED

 


WS2811 Digital Addressable Pixel String Lights

While building a previous project it was recommended that I look into using these strings of LEDs. Santa brought me a string of 50 this Xmas and so this blog post is a running commentary of my attempts to program them.

Working voltage: DC5V, 60mA

Waterproof IP67 ,Dimension: 12 mm

Power: 0.3W/LED,pack of 50pcs

RGB Dream Color changing led string lights addressable with WS2811 IC Digital addressable,can light up with multi color and single color (such as red,blue,green,purple ect)

Perfect for outdoor advertising, Christmas decoration, Cove Lighting, Path and contour lighting, Backlight for signage/ letters and so on

The string consists of 3 wires and breakdown as follows:

Red - V+

White - V-

Green - DATA

Initial Test - 26/01/22

The first thing to do was obviously to make sure that they work correctly. Googling "WS2811 arduino" presented the following image:


So this was my goal. Luckily I had a spare Arduino Uno laying around and I had saved a redundant 5V 1A power supply from the bin at work in the past, knowing that one day it would come in handy. The closest resistor I had to 470 ohm was 540 ohm.

FastLED was the first thing that google found when searching for a suitable library to use as control. It seems pretty well developed so I have decided to stick with this moving forward.

Loading an example sketch is easy enough, next time I'll have to begin focussing on the basics. How to set different colours and brightness.


First attempted project - 28/04/22

A few months later, most of my projects are packed away in anticipation of moving but I still have these LEDS available so I've decided to experiment with using them as programmable string lights.

Simple requirements to begin with. Using my arduino's prototype board I want to have 2 buttons. One that changes the colour of the LEDS in various ways and another that cycles the brightness. A selection of 10+ different colours will be enough.

This will be a good opportunity to break down the FastLED documentation and figure out the basics.

After skimming through the documentation it seemed there were a number of ways to declare the colour of an LED. Coming from a theatre background I prefer to work in RGB so went for:

 leds[i].setRGB(255, 255, 255);

Once the colour is declared it is then updated with the function:

FastLED.show();

Combining a basic sketch of button functions with a Switch...Case function I was quite quickly able to get my string lights switching through different solid colours.

Double Colours

With a bit a time left in the day I decided to try include some 2-colour combinations. I want to say "Odd colours be this....Even colours be this." To do this I used the Modulo operation (%) to return a 0 if even or 1 if odd. 

for (int i = 0; i < NUM_LEDS; i++){
         if (i % 2){
          leds[i].setRGB(255, 0, 221);   //Even
         } else {
          leds[i].setRGB(255, 100, 0); //Odd
         }
        }

Multiple Colours

Advancing further now, I explored how to make a sequence of colours...say Red, Green, Blue, Yellow. Given any number of total Leds, how would I say "every 3rd LED be Blue" for example? 

I found that Modulo can be used again. In the example above, by performing the operation:

(i % 4)

Where i is the current number in the for loop sequence and 4 is the total number of LEDs until the sequence repeats. This returns a repeating result of 0, 1, 2, 3, 0, 1, 2, 3, etc...

I could then use another switch...case statement to set each colour in sequence:

for (int i = 0; i < NUM_LEDS; i++){
          int j = 0;
          j = (i % 4);
            if (j == 0){
               leds[i].setRGB(255, 0, 0);   //0 - Red
            } else if (j == 1){
               leds[i].setRGB(0, 255, 0);   //1 - Green
            } else if (j == 2){
               leds[i].setRGB(0, 0,255);   //2 - Blue
            } else if (j == 3){
          leds[i].setRGB(255, 255, 0);   //3 - Yellow
            }

Intensity Control

Intensity is set using a number between 0 and 255 which I've been calling just before FastLED.Show()

FastLED.setBrightness(intensity);

A press of my 2nd buttons cycles the intensity value between 9 different values. Using even intervals between these steps looked odd because the dimming curve of the LEDs is not linear. Therefore I have given more steps between 0 - 50 than between 51 - 255.

Intensity steps: 05, 10, 18, 25, 51, 102, 153, 204, 255

While writing this out, I bet logarithmic intervals would have looked smoother.

1, 2, 4, 8, 16, 32, 64, 128, 255



Overall I spent an hour remembering how to use my button logic, an hour to program colours and a final hour fixing bugs.







Comments