How to Blink an LED with Arduino (Lesson #2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you will learn how to write a new Arduino program and build a circuit to Blink an external LED in addition to your Arduino you will need a breadboard two jumper wires an LED and a resistor 150 or 220 ohm resistors work well a breadboard consists of a grid of holes where you can insert wires in other parts like LEDs and resistors to build a circuit you can easily remove and rearrange the parts if you make a mistake or want to build a different circuit each set of five holes that are adjacent to each other in a row are electrically connected to each other so for example if I plug one jumper wire into the left end of this Row in column A and plug another jumper wire into the right end of this Row in column e these two wires are now electrically connected adjacent rows are not connected to each other so for example if I move the red wire down to row eight it is no longer connected to the black wire similarly holes are not connected across the Gap in the middle of the breadboard so if I move the red wire over here to column F even though it is in the same row row 7 as the black wire the two wires are not connected if you flip your breadboard over you will see that normally they have an adhesive backing and you would leave this on however in this video I am going to remove the backing from this breadboard so I can show you how it works so if we remove the backing you can see these metal rows inside the breadboard and each of these is a set of metal Clips these are what grab onto the parts when you press them into the breadboard but again you can see that each set of five Clips is all connected to each other by a single piece of metal so if I look at the back of the breadboard here you can see what I described on the front the pieces of metal are not connected between adjacent rows there is insulating plastic in between them and they're also not connected across this Gap in the middle of the breadboard but each set of five holes in half of a row is all connected to each other by a single one of these metal clips the long vertical strips on the sides of the breadboard are called buses or rails and they're connected a little differently but we're not going to worry about them just yet we are going to build a circuit that connects the resistor and the led to the Arduino then we are going to write code that uses one of the arduino's pins to turn the LED on and off note that when you're building your circuit it's a good idea to unplug your Arduino that way you're not working with a powered up circuit and this reduces the chance of short circuits if you make a mistake let's start with the LED look at it closely and notice that it has a long leg and a short leg the long leg is the positive side that will be connecting to one of the arduino's pins pick two rows in the breadboard to insert the LED legs it doesn't matter exactly which rows but make sure you keep track of which end is the long leg or positive side I'm going to put the LED legs in rows 10 and 12. next I'm going to connect the resistor so it is in series with the LED this means electrical current can flow through the resistor and then through the LED now your first instinct to do that might be to put the resistor so it is physically lined up or physically in series with the LED in the breadboard like this however remember how the holes are connected inside the breadboard adjacent rows are not electrically connected to each other so there is no path for electrical current to flow from the resistor into the LED here because row 13 is not connected to row 12. in order for the parts to be electrically connected or in series I need to move the resistor over so one end of it is in row 12 next to the LED once I've done that I now have a path for electrical current to flow through the resistor then through the LED the purpose of the resistor here is to protect the led by preventing too much current from flowing through it if you forget the resistor completely or the resistance value is too small then too much current will flow through the LED causing it to burn out however if the resistance value is too large then not enough current will flow through the LED and you won't be able to see it light up that's why you can't just pick any resistor and it's important to choose the correct value again 150 or 220 ohms both work well for this project next I'm going to use my jumper wires to connect my circuit to my Arduino I'm going to use my red wire to connect the positive end of my circuit which is the free end of the resistor to Arduino pin 12. in order for electrical current to flow I need a complete path or a closed circuit so I'm going to use my second wire to connect the negative end of my circuit or the short leg of the led to the arduino's ground pin which is labeled gnd this gives me a closed circuit or a complete path for electrical current to flow out of pin 12 through the red wire through the resistor through the LED and then through the black wire back to the Arduino now that we've built the circuit we're ready to write the code to Blink the LED this is a breadboard diagram made with a computer program it makes it a little easier to see exactly which hole each part goes in on the breadboard you can use it as a reference when you build your circuit if you need to finish building your circuit pause the video here before you continue double check to make sure all of your wiring is correct before you continue then open the Arduino IDE and get ready to write your first new program to create a new program in the Arduino IDE select file new this will open a new window with a skeleton that includes the setup and loop functions but they're both blank so you can add your own code you can select file save to save your program on your computer make sure you give it a name that makes sense to you and save it in a place where you'll easily be able to find it again this time instead of loading an example program you'll write your own code follow along as I type and pause the video if you need time to catch up on the typing remember that we connected our led to Arduino pin 12. the first thing we need to do in our setup function is tell the Arduino that we're going to use that pin as an output we're going to do that with the pin mode command so type pin mode make sure you capitalize the m open parentheses 12 that tells the Arduino which pin we're going to use comma output in all caps that tells the Arduino we're going to use the pin as an output then make sure you end the line in a semicolon all Arduino command lines must end in a semicolon or you'll get an error when you try to upload the code now that only needs to happen once and then we want to Blink the LED on and off forever so we're going to go down to our Loop function to turn the LED on we use the digital write command type digital write make sure you capitalize the w open parentheses 12 comma high that tells the Arduino to turn that pin high or on then again remember to end the line in a semicolon next we want that PIN to stay high for one second so we're going to use the delay command type delay open parentheses but remember don't just type 1 because the delay command works with milliseconds not seconds so we need to tell it to wait for 1 000 milliseconds and again and the line in a semicolon after that we want the PIN to go low so we're going to type digital write open parentheses 12 low note that the Arduino rde automatically fills in the closed parentheses for me when I type the open parenthesis and again we're going to end that line in a semicolon finally we're going to do another delay 1000. notice that I forgot to hit the space bar twice there you don't strictly need to but to keep your code neat code inside functions is usually indented by two spaces that lets me look at this and easily tell that all of this code here is part of my Loop function now make sure you reconnect your Arduino if you had disconnected it when you were building your circuit see that I have a notification here that my Arduino is not connected so I've just plugged it back in but then I need to reselect it from this drop down menu after that you can upload the code once you've connected your Arduino and uploaded the code if you built your circuit correctly the LED should blink on and off now here's a programming and circuit building challenge for you add another LED that is connected to our Arduino pin 8. edit your code so that led blinks on and off at the same time as your first led pause the video here and try it out you might have built your circuit like this I added a resistor and led to the breadboard and then connected them with jumper wires to Arduino pin 8 and to one of the other ground pins available on the other side of the Arduino that's okay because it will work however the Arduino only has three ground pins available so if you wanted to build a circuit with four or more LEDs you would run out of ground pins a better approach is to use the breadboard's buses the long strips that are connected up and down the entire length of the breadboard normally you would use the black bus marked with a minus sign for the ground connection so what I can do is take one of my ground wires that's connected to the ground pin on the Arduino and plug it in to that bus instead that is going to make my ground connection available up and down the entire length of the breadboard so unlike the rows in the middle of the breadboard these buses are connected across the Gap so this hole is connected to that hole and so on down the entire length of the breadboard but they are not connected all the way across the breadboard so this bus over here is not connected to this bus over there so if I wanted my ground connection available on both sides of the breadboard if I was building a bigger circuit I would use another jumper wire to connect the left side to the right side but since this circuit isn't that big I'm not going to bother with that for now what I will do is take my two jumper wires and use them to connect the LEDs directly to the ground bus so now I will still have both of my LEDs connected to ground but instead of connecting them directly to the Arduino ground pins I have connected them to this ground bus which is then connected to a single Arduino ground pin with this jumper wire one other thing to note here is that these long wires tend to get kind of messy as you build more complex circuits you can wind up with a tangled nest of wires that is hard to trace and debug so while these wires are good for making the connections to the Arduino they're not always the best choice for the breadboard to breadboard connections that is where shorter jumper wires like this come in handy you can either purchase these in a kit or buy a spool of wire and wire strippers and cut them yourself so for example I am going to replace this long wire with a short wire that makes the same electrical connection it's just going from the ground bus to the LED but it's using a physically much shorter wire I'm going to do the same thing with the other one replace that with a short wire and you see that this will make my circuit much neater so it is easier to trace the wires and debug the circuit to find mistakes finally you will notice that I have not connected the positive or power bus here that is useful if you need to provide your circuit with a fixed voltage like 5 volts or 3.3 volts from the Arduino but you don't need to do that in this project here is another breadboard diagram that shows one way you can add a second led to the Circuit while making use of the ground bus if needed pause the video here and follow the diagram to finish building your circuit but remember there's more than one correct way to build the circuit on the breadboard so it's okay if your circuit doesn't match this diagram exactly as long as it still works if you want to modify the program to also blink an LED connected to pin 8 you can do that mostly by copying and pasting existing lines of code for example I can copy and paste my pin mode command in the setup function but I'm going to change the 12 to an 8 to tell the Arduino to use pin 8 as an output next I'm going to copy and paste my digital write command in the loop function and again change the 12 to an 8. to tell the Arduino to turn pin 8 High I don't need to copy and paste the delay command this tells the entire program to wait for one second so pins 12 and 8 will already be high I only need one delay command there then I'm going to copy and paste my digital write command again change the 12 to an 8. so now the program tells the Arduino to set both pins 12 and 8 as outputs turn both pins 12 and 8 High wait for a second turn both pins 12 and 8 low and wait for a second I can upload this new program to my Arduino now when I reconnect my Arduino and upload the new code I see both LEDs blink on and off at the same time here's one more challenge if you're up for it change your code so the LEDs blink such that one is on while the other is off and vice versa pause the video here and give it a try if I want to change the program so the LEDs blink out of sync I don't need to change much I still need to set both of them as outputs however I don't want to set them both High initially I want to set one of them high and set the other one low then when I set the first led low I want to set the other one high so all I had to do was change two of the highs and lows in the program again if I upload this now the LEDs should blink out of sync after uploading the new code I can now see that the LEDs blink out of sync sort of like the warning light at a railroad crossing now if at any point when following along with this video your circuit didn't work as expected that's okay even experts make mistakes and it's easy to make simple mistakes when you're first getting started with circus next up we'll talk about some troubleshooting steps you can take to identify and fix problems in your circuit and your code for more Arduino tutorials and lots of cool projects you can do with an Arduino check out the links in the video description for thousands of other fun Hands-On science and engineering projects visit us online at www.sciencebuddies.org
Info
Channel: Science Buddies
Views: 84,165
Rating: undefined out of 5
Keywords: science, STEM
Id: FKekzzj5844
Channel Id: undefined
Length: 15min 57sec (957 seconds)
Published: Thu Oct 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.