HOW TO USE WS2812B NEOPIXELS WITH FASTLED ON ARDUINO

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
working with ws2812 be neopixels and the fast LED library how to get it all up and running I have this strip of neopixels which are the ws2812 B RGB LEDs 5 millimeters by 5 millimeters it's on this flexible PCB tape where you can actually cut and make this whatever size you want ultimately you have a power supply 5 volts and ground and then some data going in and data passing between each LED whatever shape it's in I'm going to be using the Arduino Uno so at the beginning of this strip I will have data coming out of the Arduino it's going to go into the input of the first LED that LED will take the first set of data for red green and blue information use it for itself and if there's any further red green blue data it will just pass on all the remaining data in that packet so these LEDs don't have an address it's just pure data being sent and first-come first-serve whatever first data comes in the LED takes it for itself if there's any more data it'll just send it on and then whatever data first arrives on the other LEDs that's automatically assigned to that LED so it takes care of itself you can have as many or as few LEDs as you want just that the first data to arrive is the data that gets used by that LED the data sheet for the ws2812 B LED is really sparse so the chip has the RGB LEDs built in as well as the driver chip and that's assembled on that LED strip tape along with a decoupling capacitor so we have the power supply data in data out ground so all of these individual LED packages with the integrated driver chips are just connected with their data lines in series data out of one goes to the data in of the next etc the data is actively driven out so there's not some of a concern of signal integrity with having a long string of lights because it's freshly generated output data from each LED and here's how the data stream looks there's eight bits to control the level between fully off and fully bright for red green and blue so there's 24 bits of data per LED so data one in this case would be coming from Arduino in to the first 28 12 LED and it would receive in this case if we say that we've got three LEDs it would receive three sets of data each being 24 bits for red green and blue this first pixel is going to take the first received 24 bits and use it to configure the red green and blue levels in itself then it's going to pass on the other data to the next pixel on the data to section of this circuit so three sets of 24-bit data come in one gets used the two remaining sets of data gets sent out on d2 that gets automatically used by this pixel and whatever is left gets sent out as d3 so d3 there's only one more set of red green blue data that goes out onto this third pixel and it takes that as its own data and there's nothing else to send out on d4 so there's nothing transmitted then if you have at least 50 microseconds of no activity that means the stream is done and we're ready to get some new data and again the first set of 24 bits will be used by the first pixel and so on so here's the schematic diagram of the ws2812 B string of neopixels each individual module is going to have a decoupling capacitor on it's five volt supply the data is going to come in from Arduino go out along the string of lights to the next data in and out and so on until the last LED on the string with this particular LED module the 24 bit data is not in order of red green blue it happens to be green red blue so we're going to need to hell the fast LED library that this is the sequence of data for our particular ws2812 B LED module and this is how I have it hooked up I have the Arduino Uno it's getting its own power source from the USB connection to the computer I'm using digital pin 2 as the data output for the strip of neopixels and I happen to be using a 220 ohms series resistor in line with the data that along with this 1000 micro farad capacitor across the 5 volt supply to the neopixels those two items are commonly recommended best practice components to add since we are sending data out here at a certain data rate and we're not just using slow on-off signals to turn on and off an LED or something like that this becomes a transmission line so without any sort of termination there's gonna be signal reflections here and we could even see voltage spikes here that may exceed the safe recommended voltage tolerance of the data input on these modules if this line is not terminated you'll get a 5 volt signal sent along this line and when it gets to this data input there will be a reflection back and it will stack on top of the original 5 volts and you'll end up with a higher spike and it will just echo back and forth until it naturally dies out because this is not going to be an ideal wire there is gonna be some resistance in it it could cause data corruption it could cause possible damage if this chip can't tolerate the voltages so we can add a series terminating resistor and that will absorb reflections and keep the voltage levels here safe I did a little bit of characterizing of this I'll demonstrate that later on to give 5 volts to this neopixel strip since we're going to be potentially using a lot of current in order to power 60 lights I've just got a separate 5 volt power supply that can do 3 amps so I'm bringing it in through this little connector and giving 5 volts and ground directly to the neopixels so that I'm not trying to use the limited current capability of this uno normally I would just power something through this five volt header right here but I want to be able to give as much current as needed because we have so many lights here and we could be changing the current between low and high levels very quickly and very often we're gonna have all these transient power requirements so it's good idea to have a local bulk capacitance here to help provide current while the power supply tries to keep up and adjust to the varying load demands and we do need a signal return for this data output so I did tie the ground of the Arduino common with the ground of the neopixel power supply but we don't want to connect the five volts of the external supply to the five volts of the Arduino so we keep the 5 volt supplies all separate but we do need a common signal return in one of my experiments I do use a potentiometer so I'm just connecting the center of a potentiometer to analog zero and then one end to five volts on Arduino and one end to ground on Arduino but that's not part of this overall neopixel setup so I did not show that here I didn't want to confuse things and cost too much clutter using about a foot of data wire between the Arduino output and the connector of the data in on the LED strip without a series resistor here's a waveform I captured displaying over three volts of overshoot and almost two volts of undershoot I even tried using a very short ground wire right up to this point to go to the closest ground I could to this data pin and either way I had the overshoot and undershoot now I threw in a 220 ohm resistor and the overshoot and undershoot are cleaned right up the max is just around where it should be 5 volt level and the minimum is close to ground basically it just looks like a typical waveform but obviously there's no massive spikes for the 5 volt power supply going to the LEDs in order to make sure the power supply current is going to be enough drive the worst case we're all 60 lights are on full I could go with the standard worst-case assumption that each led is going to require maximum worst-case 60 milliamps when fully on 20 milliamps each for red green and blue that would come out to 3.6 amps and I happen to have a 5 volt fixed bench supply that can do 3 amps so I thought that would be more convenient if I could confirm that I can get away with that so how much current is this really using in order to figure out how much current I really need to run this strip of 60 lights worst case max brightness all lights on white I want it to take 3 measurements so I wrote a sketch that will turn on just one light at full brightness and every other light turned off then I turn off all lights and then I turn on all lights full brightness so obviously turning on all the lights is a brute-force way to see how much current it's using but in order to do the math and double check what's going on when I turn off 59 lights and keep just one on measuring that current it's actually going to be higher than what it takes to turn on just the one light for real because it's one light plus the standby current which is gonna be less than 1 milliamp 459 other lights so that can add up so I'm going to reset the sketch and take some measurements so 87 million swith one light on 54 milliamps for all lights to be off and that's the idle current of the chips and then 2.0 3 3 amps to run the whole thing full power we have 87 milliamps to turn on one light plus the idle current of 59 other lights so I'll subtract out the idle current where all the lights were off 54 milliamps and that means it took 33 milliamps to turn on just the red green blue LEDs in that first light so the idle current of all 60 lights being off is 54 milliamps divide that by 60 lights it is 900 micro amps or 0.9 milliamps of idle current just to have each chip individually on so I add that to the actual current to turn on red green and blue at full brightness and obviously it's going to be actually thirty three point nine milliamps total current to turn on just one pixel red green and blue at full to give us brightest possible white so that's only slightly over half of the theoretical worst case of 60 milliamps per light where it's estimated 20 milliamps each of red green blue so then if this is the actual current for one light multiply that actual current by the sixty lights and based on my measurement of one light I would predict it's two point zero three four amps to run the whole thing so now I'm going to reset the sketch again and wait until it gets to the point where it powers up the entire strip 2.0 3 4 3 3 amps 2.0 3 4 calculated based on the measurement of 1 light so the math works out so it really is thirty three point nine milliamps per light to be on full brightness for my specific LED strip I'm using the fast LED library which I installed from the library manager and it's also available along with documentation on github and the fast LED library once installed comes with a lot of examples but to get started I created a couple of examples myself so we include the fast LED library it's also convenient to define some things so that it's easier to read and see what's happening later in the sketch and also if you need to make changes you can just make it at the top of the sketch instead of having to dig through the code so our LED type because the fast LED library can support different kinds of LEDs we're using the ws2812 beast and as we saw in the datasheet the data coming in to each LED is ordered green red blue not RGB it's actually GRB so we defined that here and the library needs to know so that it has accurate color data going out in the correct order the number of LEDs on my strip that I'm using our 60 I'm using Arduino as data pin 2 for the output data to the LEDs and if we want to we can change the brightness so I made a brightness definition of 200 and it can be 0 to 255 one reason that I might want to use a brightness level obviously the brighter the LEDs the more current they're gonna use so I just toned down the brightness because I'm just working on the bench here I don't need this to be as bright as possible so in the fast LED library there's a structure called C RGB and that contains individual red green blue brightness levels between 0 and 255 for each of the colors for each pixel that you have so what we're doing here is creating an array called LEDs and the size of it is the number of LEDs that we have so I'm creating an array 60 elements long so it's going to go from 0 to 59 and each element in that array contains individual 8-bit red green and blue data so we will update this red green blue data for each LED using this and then send it out to the actual LED strip when we're ready in the setup we finally tell the library we are using LED type ws2812 B on Arduino pin to our lights are expecting data to come in green then red then blue and we have 60 LEDs in the array called LEDs this library can do some fancy things but we're gonna look at the basics here so there's several ways that we can address each of these 60 LEDs what we do is we set up the color information on whatever lights we want and when we're done we have to send that out finally to the strip using fast LED dot show so the very first LED on the strip the one that's connected directly to the early data out is going to be number zero in our array of 60 lights so I can say I want LED array number zero to have red green blue data 255 for red so full brightness red and then green and blue are off so my very first LED on the strip is going to be fully red and that's what we have down here this is the input right here another way so the next LED we can just send hex data so zero eight is red green is going to be FF which means full brightness that's 255 and a7 is blue whatever way we want to represent it we can control the red green and blue there's a built in routine called fill solid which means we can choose a single solid color so I want to start at LED six and I want to do five LEDs so six seven eight nine and ten are all gonna be filled with this color and that's up here it's kind of a purplish-pinkish color there's another routine called fill rainbow so here I'm starting at LED eleven and I'm filling 10 LEDs with a rainbow pattern and my starting hue is one which is basically toward the red of the rainbow so that's up here this is a red LED and then it fades orange down toward yellow like a rainbow and speaking of using a hue instead of setting the RGB levels we can also use this hue saturation and brightness or value value also means brightness so some of these other LEDs further along I'm setting a purple hue with full saturation so it's as purple as it can get it's not faded purple and full brightness and then I set another purple with not as much saturation but still full brightness and then I did hue green and hue orange so those are up here these are purple green and orange where one is going to be really saturated and the other one's going to be a little bit weaker but the color so here's this hue saturation and value or brightness it's sort of like when you're using a paint program and you want to click on one of those color boxes to choose your color so down here toward zero is the reddish and then it goes orange yellow green aqua blue purple pink and then whatever number you're picking for hue you're gonna basically be in one of these ranges sometimes this is easier to work with than just setting red green and blue levels as they say here you can mix red green and blue and get a whole bunch of colors but just working with red green blue values can be awkward like if you want to do things with different tints or shades of an a color you've already got you have to try and figure out the individual red green and blue values to adjust to just kind of fade your color as is but if you use like saturation and stuff like that you can do color washing and you can easily navigate a rainbow of colors just by the way this is laid out so it just makes things a little bit easier depending what you're doing so also for convenience instead of trying to manually figure out red green and blue combinations you can use web color names so up near the end of my LED strip I'm setting colors by name white hot pink dark orange purple yellow green firebrick so those are up here it may not be coming out very clear but you can set the colors by name for example here from Rapid Tables calm here's a web color code chart so you have a sample of the color swatch and the common name that's been given to it you can have the red green blue hex data or the actual number 0 to 255 of red green blue but all those color names may not be supported so I found it helpful to come to the fast LED library over at github so in the pixel types dot H file here for example if we're going to use those hue names just like over here I chose hue purple green and orange these are the hue names that are available and these predefined RGB colors these are the ones that we can use so looking at this first color called Alice blue the hex RGB data is F 0 F 8 FF and Alice blue is f 0 f 8 FF on this web color chart the only other thing I did in this example is change the brightness and just keep fading it down after a one-second delay the next thing I did is I made an LED chaser with two separate strips I have three blue LEDs and three green LEDs running from one end of the strip to the other at slightly different rates so they're constantly chasing each other catching up and overtaking and crossing over and I also have a potentiometer between five volts and ground and analog zero input when the potentiometer is all the way toward ground the sketch is going to interpret that as automatic chase mode when I start to move the potentiometer and it goes a certain amount above ground to work five volts the sketch will detect that and it will give me control like I have right now of this green pattern the blue is still running at the same rate and to end but by changing the potentiometer I'm moving the green on my own right now based on the setting of the pot so again I still have 60 LEDs I'm using Arduino pin two I'm using the ws2812 B and the color order is green red blue and I have a potentiometer on analog zero and I have two color bars chasing each other I just numbered them one and two so the position of one and the position of two I just started out on LED number two I have to know what direction they're going because they go up and then they go down so zero means forward and one means reverse first I read in the pot and if it's greater than ten where the reading is going to be between zero and ten twenty three I want to be in control of the green so the one position is going to be where my potentiometer is and I'm mapping it over between the pot reading of 0 to 1023 and the actual number of LEDs 0 to number which is 60 LEDs minus 1 because it actually goes 0 to 59 but 1 position so the position of the green light when we first run the program if we're not using the pot LED number 2 is going to be green and because the green strip is three lights long I also set the one directly below and directly above the center one as green likewise number two is the blue one so I set the color of whatever three lights green and blue bars then I'm ready to display it so fast LED dot show and the animation I tweaked the timing so I delay 150 milliseconds before I do another update and of course the next thing I want to do is make those lights go off because we're gonna animate we're gonna turn them on then we turn them off by saying them all black and then later when we come back we're gonna turn them on again in a new position or we're just gonna turn them on in the same position and it doesn't look like anything moved so if they are free running from end to end we start it out the direction for each bar is zero which means we want to go forward so if the direction of the green bar is forward I want to increase it by four LEDs because we're going faster than the blue bar so we're actually jumping ahead a bit so I'm counting up along the strip I want to make sure I don't go off the end of the strip so if I increase the position in the direction I want but I go too far just keep it at the maximum and since we're at the maximum change it to reverse direction because next time through the loop it's time to send this strip the other way and so on and it looks like it's animating up and down so using a neopixel can be as simple or complicated as we want but at least it is easy to get started and from there we can do whatever we want you
Info
Channel: Gadget Reboot
Views: 133,537
Rating: undefined out of 5
Keywords: ws2812b, neopixel, rgb led, addressable led, arduino, series termination, transmission line, bulk capacitance, led current, fastLED, light show, led chaser, ws2812b arduino, neopixel arduino, arduino neopixel, arduino led strip, fastled tutorial
Id: YgII4UYW5hU
Channel Id: undefined
Length: 24min 32sec (1472 seconds)
Published: Mon Jan 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.