Hi everyone and welcome to this mini series on FastLED over the next few videos my aim
is to introduce programming addressable leds using the FastLED library from lighting your
first pixel to the more complex palette, wave and noise functions. After that it's up to you guys.
If there's an explanation of something that you'd like to see please leave it in the comments below
and I might add that to the list. I'll aim to get these videos out on a regular basis, but
real life unfortunately often gets in the way so I'll see what i can do. Today we're going to cover
a number of introductory topics, they are what FastLED is for and why you might want to use it.
the hardware you need, how to connect it up, and a few notes on power delivery, how to start your
code, the setup function and the FastLED loop and finally just writing your first simple pattern.
Straight from fastled.io which is the the website of FastLED: FastLED is a
fast, efficient, easy to use arduino library for programming addressable led strips and pixels
such as the WS series neopixels and more, but to start with what actually is an addressable LED?
Traditional leds are usually a single color and what you might call dumb. If you wanted to control
more than a few of them you'd very quickly run out of output pins on your microcontroller. This
is where addressable leds such as the WS2812B come in. These leds are usually rgb which means
that each pixel contains red, green and blue leds each of which can be controlled separately.
Combining together various amounts of red, green and blue means the led can show any color you like.
As great as this is, the real magic comes when you want to control a whole bunch of these leds.
Instead of having a separate wire going to each led we can communicate with hundreds of leds at
once from a single pin on the microcontroller. The exact way data is transmitted is a bit beyond the
scope of this introductory video, and it's really not something you need to care about as the FastLED
library handles all this complicated stuff for you. Once you understand FastLED you can
start creating your own patterns or even progress to making a matrix or a slightly terrifying mask.
To get started with FastLED there are two main things we need, a microcontroller
such as an arduino, and an addressable led strip. If we want to run more than a few leds you'll also
need a power supply, but we won't be using one for the moment. FastLED supports a bunch of different
microcontrollers including pretty much every arduino board. I've got an uno here, a nano, it also
supports the ATTiny, I have the ATTiny85 just there and the teensy which i don't have. It supports the
ESP8266 if you need wi-fi. It doesn't officially support the ESP32, I've got one of those there
but I've used these a lot for projects where we need a little bit more grunt and I haven't
had any problems. However other people have found issues with flickering with this board with
their lights. There is I believe a github fork of FastLED with a little bit more support for ESP32
and if i can find that i will link it in the description below. Choosing which led strips to use
can seem pretty daunting, there are many different types available at greatly varying price points.
Here are some things to consider when buying an led strip. First of course make sure that the led
chipset is actually supported by FastLED. There is a list of supported chipsets on the FastLED
github page. The most common ones currently are the three wire WS2812b WS2815 and WS2811 and
the four wire APA102 amongst many many others. Make sure that you get an rgb strip like the led
shown on the left rather than the rgbw as shown on the right. So rgbw strips have red green and
blue leds and an additional white led in each pixel. These strips give out a much nicer white
light than just rgb by itself but unfortunately at this time FastLED doesn't support these strips.
Here's to hoping that support comes along for those eventually. For the easiest start go for a
5 volt powered strip. 12 volt strips are available but this complicates the power requirements. You
need to move to these eventually maybe, if you need long led runs because you get less voltage drop
across the 12 volt strip. The strips also come in different densities that is the number of leds per
meter, the most common densities seem to be 30, 60, and 144 leds per meter. I tend to go with 60 leds
per meter for general use. Remember though the more pixels you have the higher your power requirements
are going to be and that is something we need to think about. Finally do you need waterproofing
or not? If you're not going to need it don't go for it. I've got two different strips here this one is
not waterproof and you can see the copper contacts there very easy to solder to on the
top. This is a waterproof strip it's covered in a sort of silicone rubber coating and the
problem with this is and you need to remove the coating if you need to solder to these contacts.
You can solder to contacts on the back here if we peel back some of the adhesive here
you'll be able to see that there are some other contacts but these are covered by this strip of
adhesive so you have to remove the adhesive as well like so, in order to actually get to these
contacts. So to make everything nice and simple I recommend if you don't need waterproofing just
go for one of the non-waterproof strips as they are much easier to do. My recommendation for a basic strip
to start with would be a non-waterproof 5 volt strip with something like a WS2812B chipset and
about 60 leds per meter so medium density. I'll put a link to one of these in the description below.
Of course you're allowed to use whatever you like i just find this one of the easiest ones to use.
The YouTube channel the hookup tested some of the most common strips about a year ago
and made a really good video about it and i've also linked that down in the description in case
you want to take a look at that. The final thing to think about here is how you're going to power
your led strip. If you only have a few leds you can just use the 5v pin on your microcontroller
which is what i'll be doing in the the examples in this series because only be using a few leds
and you can hook this up as seen on this diagram. For anything more than a few leds however you
will need a proper power supply. Often you also want the usb hooked up as well as you
write your programs this is completely fine as long as you follow the diagram shown here.
It's really important that you connect the ground of the power supply to both the microcontroller
and the led strip. If you're done using the usb it's also fine to power the microcontroller and
the strip from the same 5v power supply and this should be hooked up as shown.
Long strips can demand large amounts of current and it's important to know what you're doing here
The maximum current draw for a WS2812B led is 20 milliamps per color per led. Each led has three
colors for 60 milliamps per led total so say we have 60 leds per meter we have a 5 meter strip
in theory this could draw up to 18 amps. However this is only the case if we have every led set
to white at full brightness which almost never happens in reality. For long strips
like this which draw high current there's a considerable voltage drop along the strip
and this can cause the leds at the far end to become much redder as there's not enough
voltage to light the blue part of the led. Here I have a strip that's particularly bad for voltage
drop and i'm only powering it from one end. Every color here is set to white but due to the voltage
drop the color gets more and more red as we spiral into the middle and the voltage decreases.
We can solve this problem using something called power injection and this is where we connect the
power supply to the strip every one meter or so as shown in the diagram. Finally if you're
using a four wire chipset like the APA102 the same wiring diagrams apply but you'll need to
connect one more wire from the clock pad on the strip to any digital input on the microcontroller
as you can see in the diagram here. Don't panic about all this though and for our purposes the
power consumption will be low so let's hook up some hardware and make some pretty lights.
We're going to start off simple here so i'm going to connect my parts together on this breadboard.
I'm going to use an arduino nano as the micro controller because they're cheap and easy
to get hold of and i'm also gonna use this led strip which has 18 leds and these
are WS2812B leds and they have a separation of 60 leds per meter so it's medium density.
Now i'm going to connect the data pin here to pin D2 and the data pin should be connected through a
resistor i usually use a 330 ohm resistor but some around 500 ohms something along those lines say is
completely fine. So let's put that in now. When you're connecting up your led strip you want to
look carefully and make sure that the data is flowing the correct way so if you look at the
green line here this is the data input and you can see the arrows are pointing along the strip
this way so that means the data needs to flow that way. If you try and put data in from this end
you're going against these little tiny arrows it's not going to work, so make sure the arrow
is going the right way. I'm going to run this power wise actually just from the 5 volt pin
of the um nano itself so let me just put some jumpers in here for a moment.
Now i don't necessarily recommend running any strips longer than a few leds from the actual
power pin of the nano itself, you need an external power supply for that but for simplicity for
now this is what we're gonna do. It's a good idea to put a capacitor across the the five volt
and ground connections just here so i've got a 1000uF electrolytic cap. Size doesn't
really matter that much but you know 1000uF or so is completely fine. Make sure
you get the polarity correct so there's usually a stripe down the negative side of the capacitor so
make sure that faces towards ground. I'm going to put it between five volts and ground like that and
then let's hook up the strip here so usually these come with wires pre-soldered to the end of them
the white one here is going to go to ground which is this one just here, the red strip is five
volts and that's going to go to this connection just here and we have the green wire which is
our signal wire and that's going to connect to the end of this this resistor just here.
Okay so we have our hardware all set up and connected, let's head over to the arduino
IDE, dive in and let's get coding. So I've plugged the nano in via usb.
Make sure if you go to the tools menu here make sure your correct board is selected
and the correct com port is selected. If you haven't done so already
you'll need to install the FastLED library so go to tools, manage libraries, give that a
second to pop up and then you need to search up here for FastLED. Now I've already got this
installed, you can see here at the top I've got version 3.3.3 which is the most current version
at the time of recording this. So the very first line here is
#include that means we're going to be using the the FastLED library.
We need to make sure we use that every time we do anything with with FastLED of course.
And then we have a couple of defines so the first thing down here is the number of leds.
Now we've got 18 leds in our strip so we've defined NUM_LEDS being 18. I've then also done
another define for the led pin so this is the pin that connects via the resistor to the data pin
on the the led strip, and then we need to set up our led array so this is called crgb leds num leds.
So our array of leds is going to be called leds and the size of it is going to be the number of
leds that we have which is of course 18 in this case. So what this array does, is stores basically
the color data for each one of the leds so if you refer to leds 0 that will be the first led
in your strip leds 1 will be the second led etc etc. Then we get to our setup function.
Now the first thing we need to do is to tell FastLED where our led array actually is.
So we're going to use FastLED.addLeds and then here is the type of led that you're using. Now
I'll put a list up here of the various types that FastLED accepts. I'm using WS2812B type so i put
that in there. The next parameter here is the led pin which we've defined to be 2 so that's the
data pin for your leds and then the final parameter here is the color order. I've found
when using lots of these led strips that grb seems to be a pretty common color order but it could be
grb, rgb, bgr, whatever and it's a big a matter of messing about with this basically to find out
which order that should be. In in fact that's what we're going to do in a few minutes time
Then we need to set the brightness. We don't have to do this but it's good practice. I've set
the brightness here to be 50. Like most numbers in FastLED this number here is what's called a uint8_t,
that's an unsigned 8 bit integer. In english that basically means a number between
0 and 255. Lots and lots of numbers in FastLED are these these types of numbers between 0 and
255. It's actually incredibly helpful, it means FastLED can do maths on them much more
quickly than it could if they were floating point for example. So a brightness of 50 will
be plenty bright enough we don't want to set this too high because remember we are running
this off the usb port, so we don't pull too much current through our usb port on the pc here.
So that's our setup function the very basic setup function done
and then let's go on to our loop. So the way the FastLED works is that you set up the colors
of the led in your leds array and then at the end of it you write FastLED.show
and that writes out that array to the leds. So what i'm doing here is I'm setting led 0 to be red
led 1 to be green and led 2 to be blue and then we should be setting that in the array and
then we're showing it - we're sending all our data out to the strip. Now the reason why I'm
doing this is because I want to check that for this strip here, this is the correct color order.
If we do that now, if we upload that and if we have a look at our strip, we can see that
we do have the first led red, the second led is green and the third led is blue. That means that
GRB was the right color order for this. So now we know that our color order is correct for our strip.
I've changed the loop function here to make a something a little bit different. First of
all this line here is telling the first led in our array, so that's led 0, to be red and then we call
FastLED.show to write that out to the strip. Then we delay for 200 milliseconds.
After 200 milliseconds we then turn our first led in the strip black so turn it off basically
and then we write that information out to the strip with FastLED.show and then we delay for
another 200 milliseconds. What we're basically doing here is we're blinking our led on and off -
not particularly exciting but hopefully you can see how this works.
One little comment here is that using delay in FastLED patterns is very much frowned upon
and in fact in arduino programming in general it's not normally a very good idea. That's
because during delay your arduino can't do anything else. Over the next couple of videos,
I'll show you a much better way of doing this but hopefully this is understandable for the moment.
Now let's make the led look as though it's moving back and forth along the strip. I've changed the
loop function here again it looks a little bit more complicated but it's not that bad - if you
understand arduino programming it shouldn't be too tricky. We've got a for loop setup
and it's going to start off with i = 0 so it's going to start off with the first led
in the strip and we're going to set that to be red. Then we're going to show it so we're going to
write that data out to the strip and we're going to wait for 100 milliseconds or a 10th of a second.
After a tenth of a second we're going to turn that led off - we're going to switch it to black -
and then we're going to repeat the loop but for the next led in the strip. So at this point i = 1,
so that'll be the second led in the strip. We're going to make that red, we're
going to show it, then we're going to turn it off, and then we're going to make the third led
red etc etc. And that's going to make it look as if the led is moving along the strip.
When it gets to the other end we're basically going to do the opposite. We're going to move it
back. So we start off at the maximum number of leds of 17 (or 18 - 1) and we're
going to step back i--. We're going to subtract one from that each time, so we start off
on led 17 being red and then it'll be 16, 15, 14, 13, etc until we get back to the other end again.
I now want to have a look at some of the fill functions and the first one i want to look at here
is fill_solid. What this does is it will fill a certain number of leds with a solid block of color.
The first parameter in here is just the led array which is leds, we defined that
earlier on up here. The second parameter is how many of those leds from zero do we want to fill?
I've put NUM_LEDS so we can fill all of the leds in our strip. Then the final parameter here is
what color do we want to fill that with and I've put red in this case. I then told it to write
that data out to the led strip and then wait for half a second and then do the same thing for green
and the same thing for blue. So let's upload that and you can see that our
strip is behaving itself it's flashing red green and blue at half second intervals.
Let's now have a look at the fill_gradient function. This is fill_gradient_RGB because
we're using rgb colors here. We'll talk more about colors during the next video because it's
a really important thing to understand on FastLED. If you understand how colors work it makes
things a lot easier. There's a few different parameters here for this leds again like earlier
this is our array that contains the information about our leds, then we have NUM_LEDS, so how
many leds do we want to fill and then I've put two different colors here - magenta and yellow.
That will start with magenta at one end of the strip and yellow at
the other end of the strip with a nice gradient in between those two. You can actually have up
to four colors in one of these gradient fills so here I've got red, yellow, green, and blue and that
gives a nice little sort of rainbow effect so let's upload that and see what that looks like.
Finally then, one last function to have a look at and that's fill_rainbow just because it makes a
really pretty pattern. It starts off very similar to fill_solid and fill_gradient.
We've got leds that's just a reference to our led array, the number of leds that we want to fill
up so i'm just putting NUM_LEDS so we fill up all of them. This next parameter here 0,
this tells you what value of hue to start off. In FastLED, hue is what we think of as the color and
hue runs from 0 to 255. Again, we'll talk about this in more detail in the next
video. Then the final parameter here is the delta hue so how much do we change the hue by as
we go from one led to the next. So if I want to go all the way through the rainbow from one side
to the other I want to do 255, that's the whole hue from one end of the spectrum to the other divided by the number
of leds we've got. That should give us a lovely rainbow effect so let's have a little look at that.
Thank you very much for watching and i hope you found this useful as you start on your FastLED
journey. Next time we'll be looking at how FastLED handles color, a pretty important topic.
If you want to know about this as soon as the next video comes out please
do consider subscribing and helping me out. All right hope to see them bye bye for now!