Arduino NeoPixel LED Tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey i am brian at so much science and today we're going to be doing a tutorial on how to control neopixel leds from an arduino this will be for total beginners so if you have no knowledge but you have the right equipment that's great we will take you right from the very beginning all the way through being able to control the LED is to do almost anything you want what we're going to cover is how to get the right software they arduino ide to send the code to the Arduino board and do what you want we'll also talk about how to download the neopixel library within the arduino app within the arduino ide then we will talk about which arduino to get which cable it's going to need and how to hook up the leds for you to start doing your thing there will be a lot of things we skip over if you know that would contribute to a greater understanding of arduino and LED lighting and all of that but we want to get you started if you haven't done it before and you want to get going this is a great place to start open up any browser and then in the address bar enter arduino cc and when you get to the website you'll see across the top there as a menu you can go to software and then downloads once it loads scroll down a little bit down to where it says download the arduino ide I'm running Mac right now so I will click Mac OS and this is open source software so you do not need to pay for it but if you are able to make a contribution that's great I'm gonna click just download and through the movie magic of the Internet of YouTube I will cancel that and it's already in my downloads folder from earlier here's a zip file with the software that I'm going to need gonna double click that it will unzip in the Downloads folder alright so now we have the arduino application in the Downloads folder and then all you have to do is click and drag it over to the Applications folder and then you would let go there I'm not going to do that because I already have our Dino installed on my computer and I don't want to lose any of the settings that I have put there so I won't do it but it's a pretty straightforward process you should be able to handle it great and then once it's installed there are many ways you can open it I just like to open up spotlight hitting command space and then start typing in arduino right arrow open up arduino once arduino opens up this looks pretty busy it's showing the software I was working on earlier don't worry about that right now there is a lot to cover there but we're not going to do it at this moment the next thing to do once you have the arduino ide up is go to tools click manage libraries and this is where we're going to download the neopixel library the way I like to describe it is you know before the Internet if you wanted to know something well even now you can still do this you can go to the library you may know a lot of things you already know how to eat you already know how to breathe but what if you want to do a science experiment or speak another language you go to the library and you can learn it there well in computer programming we have libraries also and when we use them or when we import them then we can use all the abilities of that library basically and so we're going to use the Adafruit neopixel library get a little bit slow then I'm gonna get these search results it's not actually the first or second result on my screen but here on the third one you can see a two fruit neopixel by Adafruit it's already installed for me so the install button is grayed out but you'll just get the most recent version click install and if you do want to confirm it you can go to appear to type installed well what's going on here type that's going on type installed and we can see the Adafruit neopixel there that's great okay that's step one you got the arduino ide installed you have a neopixel library installed we're good but the hardware is another big piece to getting this project to work so first thing we'll cover is the Arduino board I strongly recommend getting the Arduino Uno r3 it is the standard board it works for almost any project you can imagine at this point especially if you're a beginner and so I would really recommend going for that one you'll also need a cable to go with it they typically don't come with it unless you buy some kit and so you can buy a USB a 2 USB B cable and the USB a side is the standard kind of rectangular USB that you're familiar with and the USB B is that more like a square shape and you may recognize it from being like a printer cable it's often used to control printers so you may have a spare one already at home or maybe there's one you can borrow if not otherwise you can just buy one what I'm going to be using today is the sparkfun red board because Arduino is open-source there were a lot of other versions out there even if they operate exactly the same or almost exactly the same but are made by somebody else there are many other options available so this is the sparkfun red board that's what I'm going to be using today because I already had it on hand and I'm used to using it I find that sometimes uploading code to it is a little bit trickier doesn't bother me too much but if you really are a beginner I would recommend just getting the Arduino Uno r3 either way if you do end up with a sparkline red board or a me Arduino compatible board just make sure that your USB cable fits the one you've got the Sparkfun red board uses a mini B connection on the board and this is what the usb a 2 mini B connection looks like this is also a cable you may have around at home already so maybe if you have this as a spare and not the other one as a spare maybe by the red board and then it will already fit what you have the other thing you need obviously are the LEDs and so this is what it'll look like and an Arduino without an external power supply or I should say LEDs being operated by an Arduino without an external power supply you can only get up to about eight LEDs if it was eight LEDs doing white like emitting white which means all red all green all blue like full-on they draw as much current as the Arduino was able to supply so if you go any bigger than this you need to get an external power supply but we'll cover that another time so I have eight here so that I can power it with the Arduino and I've connected a power a ground and a signal wire the power is red the signal is yellow and then ground is black and you'll connect it to the corresponding pins on the Arduino well we won't clever soldering today there are other videos online for that or we can do another one in the future so we'll leave that to the viewer when you when you look for this you'll want to search for LEDs at neopixel when you order it you'll want to make sure that your your LED strip is addressable and the easiest way to do that is just look for the code at ws2812 l-- that's the common ship that's used on these LEDs on the individual LEDs to be controlled by the neopixel library on the arduino we have all the preparation done now and at this point it's time to start looking at our code so we can start making our creation this is some code that I wrote before starting this video and I'm just gonna go through it starting at the top explaining some of the main points that you need to keep in mind as you start experimenting with it but I won't be explaining all of it because part of its just to get you started and then you can think about what you want to do next and then learn how to do that so right here at the top you can see these two slashes and that text is grayed out those are common so those are places for you to write notes to yourself or to someone else that might be using the code or for other people to send notes to you it is saved in the file but the computer doesn't try to interpret it it just knows to leave that alone because it's for people to read and then go down a few lines you can see the hash or the pound include and then there's a de fruit neopixel text and that's to include the library the neopixel library to give us all those expanded capabilities that we talked about earlier then a few more lines down that we're using some variables here so variables and computer programming are a bit different than the variables we use in like algebra and geometry because in programming a variable it really is just a place to store information it's we give it a name and then we can refer to it in the future by that name and it will hold some kind of information in there what we're doing right here is we have a variable called led pin and we've assigned the value 6 to it and when we do programming in Arduino we also have to tell the computer what kind of information is going to be in there so in this case we have int that's an integer and we really want to use integers when we're doing anything where where it's counted or when there's discrete steps so for example on our Arduino board we have pins and there's pin 0 1 2 3 4 5 through 13 and some others there is no pin 3 point 5 or 7 point 4 there are only integer values that are valid for for addressing those pins so we like to use integers there or if we're counting something sometimes it makes sense to count in our program if it's going 0 1 2 3 4 like that we'll use integers there so we have a variable called LED pin it has the number 6 there because we're going to be using the number 6 pin on our board to send data to the strip of LEDs and then we have another variable below that numpy so we need to tell the computer how many pixels we're going to have because it'll make that much space for information for color information for us to send to the LED and these can be named anything we just picked something that would be descriptive and helpful later so what we call it led pin and numb pixels there we kind of know what those mean and then we use them later down here numb pixels and LED here because this function within the neopixel library it needs to know how many pixels there are and how many LEDs there are so we can do what it needs to do this stuff down here that it's very important but it's also more complicated than we need to get into right now so you can just use the default values that I have here and this whole line is basically saying declare a new variable called pixels it's this object it's this class that is Adafruit neopixels and there's a lot going behind the scenes here but you can easily just copy that line for now before you understand it and you can still use it it's no problem one more variable down here we have delay eval because later we're going to put in a line in our code that makes the program slow down because it can go so fast that when we're sending information to LEDs it's too fast for our eyes to pick up so we put a delay in there and then we go down to void setup now again there could be a lot to unpack here but the most important thing for you to know is that every time that you run this code so the first time that you upload it to the Arduino or if you turn the power off on the Arduino and turn the power back on it it runs the void set up a loop once and then never again and sometimes you may not need to put anything in there and that's fine but sometimes you do need to run things once and so you put it up there and the format is you write void and then setup with parentheses and then there's a curly bracket to start and then it knows everything between that first curly bracket the open curly bracket and the closed curly bracket the closing curly bracket is going to be run once and that void setup loop or void setup function maybe I should say and then we have void down here and this is kind of the opposite it runs every single time well it just is that that is what's running every single time the ARB you know the Arduino will turn on run the void set up and then run void loop forever until there's either no power or an error or you tell it to stop so everything in here is going to happen very quickly unless you slow it down now here's where we're going to start setting the colors for our LEDs one very important thing to keep in mind here now is that in programming again there's there's always special things in programming the numbering system or the numbering will say usually starts at zero we call it a zero index and we're going to be doing the same thing here so we know we have eight LEDs our LED strip but you can see these are numbered up through seven but we've started counting at zero so it's going to work out there's eight lines here there are eight LEDs the first LED is at position zero and the second LED is a position one and so on so you can think about it as sort of like a 10-1 if you had a a hundred eighty you are going to number it zero through 99 if n is 100 99 is n minus one there you go and then the magic the colors all show up in here so this is another part that you can kind of just take for now and then you can dig in and understand more later we do pixels dot set pixel color and then the position and then this comma separates it and then pixels dot color with a capital C and then we have three numbers here and the first one is red the second one is green and the last one is blue and they can be any number from 0 to 255 there are 256 levels here but again it starts at 0 so at 0 through 255 if you set 0 0 0 none of the LED elements turn on and and that's it if you have 255 255 255 they'll all turn on and you'll get a white really is pretty close to light there's a little bit of an imbalance and exactly how the the colors show up but you'll recognize it as white and then everything in between you can mix and match and do whatever you want to do and you can change those in every loop through your 3-year code one thing that I didn't cover earlier is that in the void setup there's this pixel stop again that initializes the library basically again there there are a lot of details behind that you don't need to worry about just put pixels dot begin open parenthesis open parentheses close parentheses or open and close parentheses however you want to call it and that will get it started and then what's happening is that the program will sort of create eight spots to save color information and you can change those as many times as you want in this code in here and then once you call pixels dot show that will then send the current set of color information out and so in this case there are eight positions and we'll send eight color values out and they're sent out in order so then the first LED it actually has some circuitry inside of it where it takes the first set of color information and removes it from the packet but it uses that information to change color then it passes the rest on the next LED takes the first that it gets and passes on the rest of the rest of the information and so on and so if you send out eight packets of information and there's eight LEDs the last one will take the last packet and then that's it and great job and then if we go back to the code we have delay and delay eval so delay is a function that Arduino understands and it will take a number inside of it for the number of milliseconds to stop or to wait to do the next thing to move on to the next line and we set as variable delay Val earlier to be 500 so it's going to run very quickly through these lines and then it's going to stop for half a second and then it will get to the end and once it hits that curly bracket it'll go back to the top of the loop and run again few other things to point out is that all these lines need to be ended or terminated with a semicolon and it may seem like a lot of extra work but what that allows us to do one of the things that lets us do is if you had a really long line of code but you wanted to keep it a nicely formatted or have it go across multiple lines for a person to read it easily you can do that you can just you can just separate it up like this now this is this is a great example I wouldn't recommend doing that here but you can because the computer is basically going to put it all together until it sees a semicolon yeah see our formatting is all kind of weird here so not a great example but that that's what it's for and you've got to mess it up you're gonna mess it up a thousand times here and do it a million times I've been coding for at least a thousand hours and I still mess that up half the times I compile my code so I don't feel bad about it just be ready for it I really made a mess here alright don't do what I did this spacing is unimportant here but it makes it a little bit easier to read what else to tell you that could be it there are some extra things here I put at the bottom that I will cover another day not in this video I'm gonna be a little bit too much to process I think that's it you you have the basics for what you're going to need to get the LED used to change color I'll make another video or you can look it up of course yourself on how to make the colors change while the code is running you might want to look into if statements or for loops that would be my that would be my recommendation on where to look next you'll see some cool stuff one thing that I want to be very clear on is that there are some things in the code and in the hardware that we need to make sure match up really well so for example the LED pin in this example we've used six as the pin number well we have to use pin number six when we hook it up to the Arduino as well if you're looking at the Arduino there are a lot of different pins here and on the right side when you have the the data input here and the power on the top on the right side we have pins 0 through 13 and then a few more labeled ones above that when we are doing this program we have a lot of options on what we do I'd recommend using pins 2 through 13 those are all going to work as we described just like you'd expect there's a way to use these analog pins as well but you don't want to use pin 0 or 1 and then the pins up here can get kind of complicated read up on it if you're really interested that's gonna be a more intermediate topic there are 4 parts to this LED there's basically the high voltage side the 5 volts there's the low voltage side the ground then there's a data in where all that information about the colors comes in there's also a data out where the LED sends the remaining information on to the next one down the line and we are able to provide that power from the Arduino as long as we're only powering about 8 LEDs and so there's a few different ways you can hook it up but you'll take the wire connected to 5 volts and you can connect it to the 5 volt pin on your Arduino alright and then the black one or whatever is connected to ground because the color is really just the insulation we put on the wire on the inside is all copper it all conducts electricity it doesn't matter you put this on ground and there are at least three different grounds and they're all the same so plug it into a knee pin label GND on the Arduino and then in this case the yellow one that I have connected to the data but it's whatever color you pick connect that to the pin that you select in your code and in our case it's going to be pin six once we have the program completed our sketch completed see even I did it once we have our sketch completed then we're going to go to the top of our menu and you'll see a few options this ones verify what that does is it just checks your code to make sure there's no obvious errors in there that's probably going to be pretty helpful for a while you may want to do that like if I click verify it compiles it which computer speaks for kind of like putting it all together and no errors at least not yet and then right next to it is upload so I can click that and it's now going to send the sketch that I created through the USB to the Arduino now my setups a little bit funny so I need to kind of trick it if I can click it fast enough it'll go there we go now it's uploading and the LEDs are still lit up because it's the same program that I was running before there's also keyboard shortcuts you can do command you if you're on Mac or look it up if you're on Windows and here's the other thing so if we go if we go into our as finder for Mac or Windows Explorer on Windows we'll go in here then you'll find an Arduino folder inside of your Documents folder and when you go in there you'll see all these folders but no files so what I was working on here was create X simple LED and you'll see here's the file here this is a plain text file but it's saved with a dot Ino ending and that's just to signify that it is a we know sketch but it's just text in there but one trick here is if you do create a new file in the future you want to make sure that you put the sketch inside of a folder that has the same name so you can see this folder is create X simple led and the sketch has the exact same format it's just that it's a text file with a dot Ino ending you will get an error if you don't do that and it's just not gonna run so plan ahead maybe on that one this was a pretty quick run-through of the code but it will get you started and hopefully get you a feel for what can be done and what you want to do next so I'm good luck send me any questions I can make more videos I will make more videos and I want to make videos about what you're interested in so let me know that was really long and I don't know what we're gonna do them alright okay let's call it
Info
Channel: So Much Science
Views: 38,836
Rating: undefined out of 5
Keywords: arduino, neopixel, led
Id: mliaJrkme2U
Channel Id: undefined
Length: 25min 32sec (1532 seconds)
Published: Tue May 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.