The BEST FastLED Tutorial | WS2812b LED Strip Arduino Nano

By | January 28, 2023

so you've got an led light strip an arduino and now you want to do all the cool things or you just watch my 58 second fastest fast led tutorial and your brain melted this tutorial is a little better paced we'll be setting up a ws2812b light strip with an arduino nano and talk about the basics of getting our code going then we'll play with the code a bit to show you some of the cool things you can do first let's get our hardware set up start with a breadboard and an arduino nano we're gonna hook up our data line first our ws2812b light strip has three wires and our data wire is the middle green one the light strip has an arrow on it to indicate the direction of our data in signal our data is flowing from the arduino nano into the light strip and down its length so the arrow should be pointing down the length of the strip away from the nano we'll be using pin 2 of the nano as our data pin between pin 2 and the data in of our light strip i'm using a 330 ohm resistor in series this isn't intrinsically necessary but is good practice to dampen reflections in our transmission line if you remember doing physics experiments in high school or grade school maybe you remember tying rope to something across the room and sending waves down the rope when those waves would reflect back towards you they would interlap and either cancel or add to the other waves coming down the line this is the same idea here those reflected waves could be additive and cause an over voltage condition all right next we'll hook up the power rail of our breadboard to the nano we're going to be powering the lights through our nano to start with well technically through our usb port since the 5 volt pin on the nano is a direct connection to our usb power normally you'd want to power these separately which i'll show you later but we're going to do it this way first for convenience i'll show you a function you can set in the code later that will limit the current your light strip will be allowed to drop so we don't damage anything these light strips can draw a lot of current if you let them up to 60 milliamps per light we have 60 lights on this light strip so that means it could draw up to 3.6 amps we need to be careful not to draw more current from our power supply than it can handle putting out some usb ports can put out more than 500 milliamps but we'll limit it to 500 in our code to be safe i made a video discussing usb port identification and specifications if you're curious i'll put that above in the description i'll put that above and in the description as well okay place a wire from the 5 volt pin of the nano to the positive rail of the breadboard then place a wire from the ground pin to the negative rail now place a 100 microfarad capacitor in parallel on the power rail of the breadboard as well close to where we place the power wires be sure the negative marked side of the capacitor goes to the negative rail since this is an electrolytic capacitor electrolytic capacitors are directional and have to be hooked up with the correct polarity this capacitor is another case of best practices and isn't entirely necessary it's meant to make up for lag in the power supply depending on what we're doing with these lights the amount of current they're drawing from the power supply can change rapidly which could create some lag in the power supply and cause some weird things to happen if you're curious i talk a bit more about this in my led lights for rc car part 3 video where i talk about some troubleshooting i had to do i'll put a link in the description and a card above the capacitor basically stores some extra charge it can inject whenever the power supply might struggle to keep up if you aren't planning to do anything fancy with the lights say for instance you just want to display one static color you might not need one each of the lights on the light strip does have a small decoupling capacitor built into them also which is shown on the datasheet these perform a similar function they're meant to keep any ac fluctuations and current away from the digital chips built into each of the lights they do this by creating an easier path to ground for alternating alternating signals due to the nature of how capacitors work in ac circuits the higher the frequency the more of a short they become so since dc essentially has no frequency it lets it through to power each digital chip but keeps out any ac that would cause undesirable results in the chip okay all that's left is hooking up power from the breadboard rail to the light strip but we're going to hold off on that for a moment remember we need to limit the current supply to our light strip so we're going to do that first before hooking up power to the light strip go ahead and connect your arduino nano to your computer with a usb cable then open the arduino ide you'll need to install the fast led library into your arduino ide so go to tools and manage libraries search for fast led and install the one by daniel garcia while we're at it let's make sure we have our board and comp set up properly i'll go to tools then board and make sure arduino nano is selected of course you can use whatever board you like so make sure you select yours accordingly there's an issue with some nanos i've come across where you can't get the drivers installed on windows 10 to be able to download to the board there's an easy solution on sparkfun.com i'll leave a link below for this as well you just go to the link on their page for the ch340 drivers install a little program that takes care of installing your drivers and suddenly you can download to your board yay another problem with the nanos especially if you like the cheap chinese ones is the bootloader version you'll notice under tools and processor you have two options for the appmega 328p the updated one and the old bootloader version i have several arduino nano boards and i have to swap between these two processors based on which board i'm using if you have the wrong one selected it will try to download but it'll just freeze up no good okay lastly we need to pick our com port arduino ide sometimes populates this list automatically when it senses the board you have plugged in so you just have to select it but if you're having issues just go to device manager in windows and check the ports it should list which com port your nano is assigned to now that that's done we're going to build a sketch we'll start with a preprocessor include of our fast led library so we have access to all its magic then we'll add the preprocessor defines these will be all the constants we might want to change easily at some point if we modify our hardware for instance if we use a different light strip or change the data pin we're using we'll define the number of leds in our light strip first we have 60 so we'll define a constant called num leds it's super generic i know i really wanted to call it num nuts and make my program squirrel themed but i'll refrain guys okay next we need to define our data pin we're using pin two so let's make a constant and call it data pin for pin 2.

how boring and obvious next we'll define our color order for the light strip sometimes these light strips don't come in the order of red green and blue in this case with the ws2812b i'm using the order is green red blue i'll talk a bit more about this in a bit but we'll define the constant as color order perfect next we need to define our chipset aka the light strip we have let's call it chipset and set it to ws2812 okay we need a constant for brightness i think we'll call this one brightness and set it to 60. yeah programming's boring guys many of the values we'll set in the fast led library are 8-bit values which means they'll range from 0-255 i'll save the binary numbers crash course for another day just know that's the range of values in decimal that is acceptable for most of our fast fast led settings fast lead fast lead settings next we'll define constants for our volts and amps we'll call volt something super obvious like volts hey programs are meant to be readable we're making it readable we'll set it to 5 since we're using a 5 volt light strip we'll call our amps max amps we'll set it to 500.

this is in milliamps and we'll use this value to limit the amount of current draw as i talked about earlier okay that's it for our preprocessor nonsense now we need to declare an instance of the crgb class included in our fast led library this will be an array and we'll call it leds this array will correspond to the number of lights we're using in our light strip we already defined this as num leds earlier alright let's get on to the setup portion of the code this is the stuff that runs once when the board is powered up here's where we get into our fast led library function initialization craziness we're going to call the add leds method of the fast led object defined in our library this is c plus plus object oriented programming object-oriented programming is great because we don't need to worry too much about what all is happening under the hood of what's included in the fast led library you can certainly go look at that stuff if you want to if you aren't aware i'll show you quickly just open up file explorer and go to your documents folder then open up arduino and then libraries open up fast led look at all the things there's a keywords file in there that shows you all the class keywords you can call it shows you all the light strips this library is compatible with as well as all the colors you can call good stuff you can also take a peek in the source file it has all the header files and cpp files i found a little bit of adult humor hidden in the code in one of these files i try to run a clean channel so i won't point it out but if you feel like an easter egg hunt feel free to go searching enough about all that nonsense though we don't care back to our setup code first we'll call the add leds this gives our library the unique information it needs to run properly we defined our ws2812 light strip as chipset so we'll add that then we'll add data pin then color order the main reason we define this stuff at the top instead of just typing in ws2812b 2 and grb is so that if we ever want to use a different light strip or change the data pin we're using or anything like that all we need to do is change a few defines in the top of the program that's it it makes our program easy to adapt to different setups last we tell it the name of our array and the number of leds we're using the second thing we'll set up is our max power by calling our set max power and volts and milliamps keyword we just plug in our volts and max amps that we defined above then we'll set our brightness with the set brightness keyboard it's important to note the max power setting takes precedence over the brightness setting so even if we set the brightness all the way up to 255 the max power setting will limit it based on the current the light strip draws next we'll call fast led clear and then show we do this because our light strip likes to remember like it's johnny 5 and it's alive this just clears whatever might be set in the light strip already best to start with a blank slate that's it for our setup now it's time to get to your actual loop program before we get into that though i figure now is a good time to mention color order we define a color order value at the top and set it to g or b different light strips have different color orders and if you don't set the order correctly you could end up with colors you aren't expecting each pixel on an led light strip is made up of three colors red green and blue to come up with all the different colors on our light strip we'll be using we just mixed the intensity of each color like we're mixing paint colors back in kindergarten now for instance we want to show just red and no other colors on our first pixel here's one of the ways you could set these colors on an individual pixel we're writing values to this array location it represents the first pixel on our light strip and it stores three values one for each of our colors so we set our values in the red green blue order we set red to a max value of 255.

the next two green and blue values were set to zero hey look at that we have a program let's upload it and see what happens what's this knob do um oh yeah i guess we should hook the light strip up now that we have everything set up properly that would probably help first unplug the power because that's smart it's a smart thing to do now we'll just hook up the red wire to our red plus 5 volts on the breadboard then the black zero volt wire to the blue negative dc line on the breadboard if your colors are all weird or something the zero volts and the plus 5 volt lines are usually marked on the light strip to help remove any doubt all else fails you can go check the data sheet gross who likes looking at those anyway let's plug back in and see what happens we have a red light amazing now the only reason we have a red light and not a green light is because we defined our color order appropriately at the top of the program let's see what happens if we change that to rgb like we're professionals we'll re-upload with the new order now our light is green amazing even though we thought we were setting a red value so whatever light strip you happen to be using you need to get the color order set correctly you can do this by addressing the first three lights in your light strip as red green and blue like this and then adjusting the color order define according to whatever is messed up what's this knob or you can cheat just go to filed in examples scroll all the way down to fast led then pick rgb calibrate this program has lots of comments to describe how to use it to calibrate your color order but it might be even easier in my case i just scroll down and find my ws2812b light strip in the dot add leds section and in the comments to the right it tells me what the typical color order of the ws2812b light strip is you can also find this information on the gross dash sheet by the way there's also a plethora of information on the fast led libraries github page which i'll link in the description below yeah i just said plethora the github page includes way more information than i could fit in one video all about the fast led library so you should check it out it even talks about calibrating your color order okay we have lots of lights what do you say we actually use all of them that would be cool now we could just keep doing the same thing we were doing with our calibration program and individually address each and every pixel down the whole length of our light strip that doesn't sound too fun though there's got to be a better way well i wouldn't make such a horribly melodramatic statement if there weren't a better way so let's investigate we're going to play with some for loops if you're not familiar with programming that's quite a bit beyond the scope of this video i might get into that in my learn electronics videos eventually but i'm running on the assumption you're at least a little bit familiar with programming if you're watching this video if you need to brush up on the syntax and how stuff works though you can certainly check out the language reference page on the official arduino website it shows how to make all the nonsense go i'll throw that link in the description as well so instead of writing out a color for every single light down the light strip we're going to set up a for loop iteration to do that automatically it'll look something like this we'll initialize our integer variable and call it i then we'll set it equal to zero this will cause us to address the lights from the beginning of the light strip to the end for the if part of our for loop we're going to say that if i is less than the number of lights in our light strip defined as num leds we will go ahead and execute whatever code is in our for loop brackets the increment line condition is i plus plus which just increments our value of i by 1 at the end of each for loop execution now let's examine what's going on in our loop there are lots of ways to set the colors of our leds beyond just setting values from 0 to 255 like we did earlier the fast led github page i mentioned shows a lot of different examples in the controlling led section i'm going to be using the keywords method here remember i showed you all those keywords earlier in the library files it lists all the different colors we can use i'm going to set leds i equal to green now each time the for loop executes remember the value for i will increment and it will set each location in our leds array equal to green all the way down the whole length of the strip isn't that way easier than going through and setting each individual pixel ourselves automation is a wonderful thing lastly we need to actually write this data out to the light strip we do this with the fast led dot show function we could include this either inside our for loop or outside what do you think the difference will be tell me down in the comments [Music] okay we'll include it inside the loop first i'm also going to implement a little delay just to emphasize the effect let's download you'll notice the dot's show statement is inside the loop it shows each pixel as it's written so it creates kind of a chain effect of lights moving up the light strip now let's put it outside the loop no delay needed this time it now writes out all the data for the lights and then shows them all at once cool how about we make a second for loop with a decrement operator instead of an increment we'll move the dot show statement back into the for loop with the delay then we'll just copy and paste to make a second loop we're going to change a few things in this loop first we're going to set i equal to num leds this time so it will start at 60.

second we'll say if i is greater than or equal to 0 we'll execute the code in our for loop then we'll make our increment line i minus minus to decrement the value of i instead of incrementing it each time through the for loop this effectively writes our pixels from the end of the light strip to the beginning instead of from the beginning to the end lastly let's change our color to red beam me up scotty very cool here's just a couple other examples of some different code you can implement all right you can also go explore some of the different examples that come included in the fast led library the code in most of those examples is pretty advanced but you should be able to at least get your hardware set up properly to be able to run those examples from the information i've provided in this video i also made some short videos showing those examples with some theme music if you want to check them out they show how to find and load those fast led examples i'll hurl a link to that in the description and the card above to you okay i've got one last important thing to talk about one cool thing about these light strips is that you can chain multiple strips together you can solder them or you can get wire pigtails or clips to hook them together however when we add lights to our light strip there are a couple things to consider first we've been powering our light straight from the 5v pin of the nano so far the 5v pin can handle putting out up to 800 milliamps but we might be limited by our usb port output as well we set our max power to 500 milliamps to keep the lights from drawing too much current well when we add more lights we're going to need a better power supply something we don't need to limit to 500 milliamps so i'm going to use my benchtop power supply to power the 5 volt rail instead of the usb power through the 5v pin i'll remove the jumper from the 5 volt pin as a precaution so the nano will be powered from usb and the lights will be powered from my external power supply i'm leaving the ground pin hooked up though so both will have the same reference now we can bump up that pesky 500 milliamp limit my power supply can handle up to 5 amps so i'll set my limit to 3600 milliamps that's 3.6 amps the most my single light strip should draw now we can set our brightness quite a bit higher for the single light strip how fun i don't know if the effects will be noticeable on camera but i can definitely tell the difference [Music] okay i don't have extra light strips to add on here but we should be able to power quite a few more lights with this setup just keep track of your current draw and set your power limit according to what your power supply can handle one last thing to consider is the longer your strip is the more voltage drop you will have along its length you may need to inject power along the length of your light strip so it doesn't dim or flicker the farther your lights get from your power source i don't have extra lights but i can show you how to do this just using the opposite end of my light strip so to inject power at the opposite end of my light strip i'm just going to make a loop out of it and plug the 5 volt and zero volt wires into the corresponding points on my breadboard rail piece of cake now i have all the power if you're curious about more practical applications than a breadboard can provide check out the led lights for rc car project i've been working on i'm using batteries and a regulator along with some extra hardware like buttons and a tilt switch it's super cool alright smash that like button extra lots if you got value out of this video and please consider being clingy and hitting that subscribe button we're all about electrical things and other nonsense on this channel i just said we like i have multiple personalities it's just me guys this is live me lighting bring the little lightning into your life okay now we can make homemade led tv let's crunch the numbers so a normal hd led tv is 1920 by 1080 pixels so that's 2 million seventy three thousand six hundred pixels i'm gonna need a bigger power supply

As found on YouTube