Beginners Guide to Individually Addressable RGB LED Strips

September 5, 2018

Individually Addressable RGB LEDS

Today on the hookup we’re going to learn about individually addressable RGB LEDS and how to control them with a microcontroller like the ESP8266 based nodeMCU.

RGB LEDS are super cool, and fun, and flashy, and sometimes a bit tacky, but they can also actually be useful too.  In this video I’m going cover the basics of writing programs for individually addressable RGB LEDs, by programming a simple timer for my high school classroom.  The goal for this timer will be to count down from any custom time and gradually change color from green, to yellow, to red as the timer gets closer to zero.  I’ll use this timer in my classroom to keep track of how much time it left in the period, but the concepts I’m going to talk about are applicable to any individually addressable RGB LED project.

RGB LEDS strips come in hundreds of shapes, sizes and configurations, but they can be broken into two major categories… 3 Channel RGB strips, and Individually addressable RGB strips.  A  3 channel strip has 3 different ground circuits and one common voltage, each different ground channel controls a different color of light (Red, Green, or Blue).  By changing the resistance on those channels you can change the brightness of each channel, which allows you to produce a wide range of resulting colors.  The major downside to these strips is that you can’t pick and choose which LEDS are lit, the entire strip must be on or off, and every LED will be the same color, so you can’t really do any cool effects with them.

 

By contrast, individually addressable LEDS have a tiny microcontroller on each of the LEDs to allows each one to light up with a unique color and brightness.  The strips have a positive voltage wire, a ground, and  a data wire.  Each time the data reaches an led it is read and passed down the strip to the next led. first chip reads the incoming address as LED0, and then performs the instructions for LED0, it also passes the data on to the next chip after increasing the counter value by 1.  In other words, the first LED says “Okay, I’m LED0, the next guy who gets this message is LED1”, this message continues down the strip until there are no more LEDS left.  The important thing for you to know here is that each LED in the strip needs to be given specific instructions with their specific address.

Individually addressable RGB LED strips come in 5V and 12V, and each strip has some unique advantages.  12V LED strips are less susceptible to voltage drop and are therefore able to travel longer longer distances before the LEDS become dim and the colors become inaccurate.  If you are planning on running your strips any significant distance you’ll probably want to inject power on each end of the strip at the very least and probably somewhere in the middle as well if you’re using the 5V variety.  The upside to using a 5V strip is that you’ll be able to power your NodeMCU from the same power source without using a buck converter or some other voltage regulator.

For our project today, I’m going to be using 5V WS2812Bs because I’m not worried about voltage drop since this project is only going to be use around 60 LEDS.

In the Arduino IDE we’re going to use a very popular library for controlling these LEDS called FastLED.

To get started with your project you need to set up your LED strip in your sketch.  To do this you’ll need to do three things.

Minimal FastLED Sketch: https://github.com/thehookup/WS2812B_RGB_Classroom_Timer/blob/master/FastLED_Minimal.ino

//NEEDED TO MAKE NODEMCU WORK //
#define FASTLED_INTERRUPT_RETRY_COUNT 0
#define FASTLED_ESP8266_RAW_PIN_ORDER
// LIBRARY SECTION //
#include <span>&lt;</span>FastLED.h<span>&gt;</span>
//LED LAYOUT AND SETUP //
#define NUM_LEDS 60
//DECLARATIONS //
CRGB leds[NUM_LEDS];
//GLOBAL VARIABLES //
const int ledPin = 4; //marked as D2 on the board
//SETUP FUNCTIONS //
void setup()
{
   Serial.begin(115200);
   FastLED.addLeds&lt;WS2812B, ledPin, RGB&gt;(leds, NUM_LEDS);
}
// MAIN LOOP //
void loop()
{
   leds[11] = CHSV (96, 255, 192);
   FastLED.show();
}

First you’ll need to define how many LEDs you’re going to have in our strip.  This refers to the number of controllable nodes, so if you bought the kind of LED strip that has one controller for every 3 LEDs you’ll need to divide your total number of LEDs by 3.  For our WS2812B leds, each led can be controlled individually, so my 57 LEDS needs to be declared as 57 controllable nodes.

Second, you’ll need to declare your LED matrix, to do this you’ll type CRGB, then give a name to your led strip, and then tell it how many leds are in your array using the variable that you defined in step 1.

Third, you’ll need to set up our LED strip in the void setup by using the fastLED.addLeds function.  You’ll tell it the type of LED strip you’re are using, the data pin they are connected to, the order in which the red, green and blue leds are addressed, the name of the led matrix, and the number of LEDS it has.

Once you have your LED strip set up you can tell a specific LED to turn on at a specific color by calling the name of the led strip, then the LED you want to light in square brackets, EQUALS and then the color you want.

When selecting a color you have a few different options.  The first one is the classic RGB configuration in which you can turn on each of the red, green, and blue channels to 255 levels of brightness. Using this method you can generate around 16.8 million unique colors, but the downside is that you need to manipulate 3 variables to change the color.

Another option that allows us to manipulate color while only changing one variable is called HSV or Hue, Saturation and Value.  Hue refers to the base color… 0 is red and then all of the colors of the spectrum are represented as you go all the way back to red again at 255, saturation refers to the vibrance of the color.  If you choose 0, there won’t be any color, only white light and 255 will be the richest most saturated color.  Last is the value, which is essentially the brightness of the LEDs.

If it’s your first time working with these strips I highly recommend you compile and upload your project after making a single LED light up as a custom color.  I know the first time I got my strip working I was beyond excited and it really helped me push through some of the issues that I ran into later.

For my specific project I’m making a timer that transitions from green to yellow to red, so you can see that I need to go from a hue value of about 96 down to zero and I can keep my saturation and value numbers constant, which is nice because that means I only need to work with a single variable.

The trigger for my timer is going to be an MQTT message.  I always use the same code to receive my MQTT values and you can see that after receiving a new payload it converts the payload into both a string and an int in separate variables so I can decide which data type I want to use for each specific function.

I use home assistant and node-red for my home automation, so I have an easy way to send an MQTT message.  But if you don’t already have an MQTT server setup you could use io.adafruit.com which offers a really nice web interface and IFTTT integration without the need to run a separate raspberry pi at home.

Once a new MQTT message comes in, my code is going to do a few things.  The first thing we’ll need to do is reset our ledsRemaining to the total number of LEDS in our string, which will result in the bar filling completely.  Then  we need to calculate how often an LED needs to disappear.

This calculation is pretty simple, we just take the total number of minutes for the timer, and divide it by the total number of LEDs in our strip which will give us the fraction of a minute that each LED represents.  Since the simple timer library for Arduino wants an input of milliseconds we’ll also need to multiply by 60,000 to convert from minutes to milliseconds.  We’ll use this number of seconds to tell our program how often it should subtract an LED from the ledsRemaining variable.  In the simplest situation if we had 60 leds total in our strip and we set our timer for 1 minute each LED would last for only 1 second.

To actually light up our LEDs we could type in each LED’s specific address in the matrix and tell it which color to turn, but that would take forever and is way more work than we need to do.  Instead of doing that, we’re going to light them up using a for loop.

Basically in a for loop you declare a variable, “i” in this case… then you give it a condition that has to be true for the loop to continue, which means this loop will continue as long as the variable “i” is less than my ledsRemaining variable… and last you tell it what to do every time the loop completes which in this specific for loop I have i++, which means it will add 1 to the value of “i” each time the loop finishes.

You can see that each time the loop runs it will turn on a different LED based on the value of that variable “.”, if the ledsRemaining variable is equal to 25 it will run 25 times and turn on LEDs 0-24.  If my ledsRemaining variable is equal to 8 it will run 8 times and turn on LEDs 0-7.

The last piece of the puzzle is color changing.  As I mentioned before I’m going to manipulate the hue value of my HSV color.  When the timer is full I want to be at a value of around 96 and when it’s empty I want to be at a value of 0.  But since I only have 57 LEDs in my strip I can’t just use my LEDSremaining variable.  Instead I’m going to use the map function for Arduino.  This compares two number ranges and results in the equivalent position on a number line for the two ranges.  I’ll be changing from a range of 0-57 to a range of 0-96 in order to adjust for my color.

There’s a few other little lines of code in there to allow me to cancel a previous timer before inputting a new one, but overall the code should be pretty easy for you to comb through and figure out what’s going on and modify as needed.

Here’s what the timer looks like in action:  We can set the timer to 1 minute, and then as the LEDs disappear we could send a different timer for 5 minutes that will overwrite the previous one and reset it to the maximum number of LEDs.  And since no project IoT project would be complete without voice control we can say “Alexa, set the classroom display to 10” and it will start a 10 minute timer for us.

I hope this video will be helpful for you in making your own RGB led projects.  If you’d like to build this exact same project I’ve put links in the description for the specific products that I used.  I’d also like to hear in the comments if you prefer videos where I just build a project and leave the code in the description, or if you’d rather see videos like this one where I explain how the code works.

As we get closer to Halloween and Christmas I’m going to be making some videos about my house LED projects.  Ben from Bruh Automation already made a really good video about RGB leds that focuses on using pre-made animations, so I’m going to look at different things like using separate led arrays for different parts of the house, and custom tailoring your LED arrays for the specific features of your house so you can have animations that are custom for your house only.

Thank you again to my awesome patrons on patreon for your continued support of my channel and my projects.  I only have these video ideas planned out around 2 weeks in advance, so if you have an idea for a unique video that you’d love to see me make, let me know down in the comments and if it sounds exciting and it hasn’t already been done well on youtube there’s a good chance that I’ll make it.  If you’d like to support my channel, check out the links in the description.  If you enjoyed this video, and you’d like to see more like it please consider subscribing, and as always, thanks for watching the hookup.


US Amazon Links ####

16.4ft WS2812B LEDs: https://amzn.to/2Q6UU4u
ESP8266 NodeMCU: https://amzn.to/2nkeahq
Aluminum LED Channel: https://amzn.to/2Q3aJce
5 Amp 5 Volt Power Supply: https://amzn.to/2wIAVAd

#### Files ####

Wiring Schematic: https://github.com/thehookup/WS2812B_RGB_Classroom_Timer/blob/master/5vnodemcu.jpg
Minimal FastLED Arduino Sketch: https://github.com/thehookup/WS2812B_RGB_Classroom_Timer/blob/master/FastLED_Minimal.ino
Full Timer Sketch: https://github.com/thehookup/WS2812B_RGB_Classroom_Timer/blob/master/Classroom_Period_Timer_CONFIGURE.ino

#### Support my channel ####

Patreon: https://www.patreon.com/thehookup
Visit my website: http://www.thesmarthomehookup.com
Follow me on Twitter: @TheHookUp1

Music by www.BenSound.com

Related Posts