How to Use a Button with an Arduino (Lesson #5)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you will learn to use a button as an input to your Arduino to control an LED you can see here that as long as I hold the button down the LED stays on and when I release the button the LED turns off this is a good opportunity to introduce the concept of an algorithm or the procedure and rules that your program will follow in addition to your Arduino breadboard and assorted jumper wires you will need an LED a push button a 10 kilo Ohm resistor and a 220 Ohm resistor let's say that our goal is to write a program that turns the LED on when the button is pressed and off when the button is not pressed one way to represent the program is to write pseudo code sudo means false in pseudo code you write out the algorithm using everyday language without worrying about the exact syntax or commands you would use with an Arduino for example the pseudo code could be to start the program set the LED pin as an output set the button pin as an input then enter an infinite Loop where we check if the button is pressed if the button is pressed we turn on the LED if the button is not pressed we turn off the LED then we repeat this Loop continually checking if the button is pressed a flowchart is a graphical way to represent an algorithm using different symbols for different actions and arrows to connect them for example we would tell the program to start we represent that with an oval then we would take actions to set the LED pin as an output and set the button pane as an input we represent these actions with a rectangle then we need to check something or make a decision we represent that with a diamond we need to check if the button is pressed depending on whether or not it is pressed we will take different actions if the button is pressed we will turn the LED on we will then loop back to check again if the button is pressed if the button is not pressed we will turn the LED off and again loop back to check again if the button is pressed now that we know how to represent the algorithm let's take a look at how to build the circuit and write the actual code to implement the algorithm to show you how to build the circuit I will use a computer program called tinkercad circuits this is a circuit simulator that runs in a web browser you can create an account and follow along at tinkercad.com but you can also follow all of these steps using your physical Arduino and breadboard we will need to connect to both the arduino's ground and 5 volt pins in this circuit however remember that it is more convenient to use the breadboard's buses to give easy access to these pins rather than wiring directly to them so I am going to run a jumper wire from the ground pin to the ground or negative bus this is a good place to mention color coding we typically use black wires for ground and red wires for 5 volts so I'm going to run another jumper wire from the 5 volt pin over to the positive or power bus on the breadboard and I'm going to make that red now let's connect the LED and a resistor I am going to use a 220 Ohm resistor if you're using a physical kit you will need to pick the correct value in tinkercad you can just type in the value I'm going to put my LED in the breadboard remember that you want to make sure the legs of the LED are in separate breadboard rows if you put them in the same row they will be short-circuited together so I'm going to put the legs of my LED in rows 10 and 12 and the longer leg or the positive side of the LED is in row 12 the shorter or negative side is in row 10. I'm going to put my resistor in series with the LED so they are electrically connected remember that the end of the resistor and the end of the LED need to be in the same row on the breadboard so they are connected that is highlighted here in tinkercad remember that adjacent rows are not connected to each other so don't do this and then I'm going to connect the free end of the resistor to Arduino pin 12 which I will be using to control the LED I'm going to pick a color for that wire since I'm using red for 5 volts I'm going to pick a different color here for my LED maybe I will make that one purple and then finally I'm going to connect the negative end of the led to the ground bus since I am using the bus I do not need to run this all the way over to a ground pin on the Arduino I'm just going to use a short black wire to connect from this row on the breadboard to the ground bus thus completing the circuit for my LED next we are going to add the button and these can be a little tricky the first time you try to put them in the breadboard what you will want to do is rotate the button and make sure that the pins fit straddling the Gap in the middle of the breadboard so I will switch over to a video clip of the physical breadboard and button here where you can see that again this button is designed to fit across the Gap in the middle of the breadboard you don't want to have it rotated 90 degrees or try to put it entirely on one side of the breadboard because then it won't fit and work properly and we can demonstrate the same thing in tinkercad here I want this button across the Gap in the middle of the breadboard I don't want it entirely on one side and I don't want it rotated 90 degrees like this because then it won't properly fit across the Gap so start out by making sure you have your button rotated the right way it has four pins all four of them should be inserted firmly into the breadboard on both sides of the gap now that we have our button in the breadboard we're going to add some additional parts to connect it to the Arduino our goal is to connect it to one of the arduino's pins that we will have set as an input meaning it can detect an external change between high or low voltage we will set it up so when the button is not pressed the voltage is low or zero volts and when the button is pressed the voltage is high or 5 volts we will do that by connecting one side of the button to one of the arduino's input pins we are going to use pin 2. and again there isn't a strict color coding convention for this but I'm going to pick a different color for my input wire since I use purple for my output wire I'm going to decide that I'll use green for my input now when I press the button it connects the pins at the top to the pins at the bottom so remember that normally breadboard rows are not electrically connected to each other so row 22 is not connected to row 24. however when I press the button it's going to close metal contacts on the inside that do connect row 22 to row 24 because that is where I have placed the button so I am going to run another jumper wire from row 22 over to my power bus Which remember is connected to 5 volts on the Arduino that means that when I press this button row 24 will be connected to row 22 which is connected to 5 volts so I will have a path from 5 volts through row 22 through row 24 through this jumper wire and a pin two so when I press the button pin 2 will read 5 volts so next you might ask well what about when the button is not pressed don't we want the input pin to read zero volts so shouldn't we add a jumper wire from row 24 over to the ground bus or zero volts that way when the button is not pressed I have no connection up to row 22 but in row 24 I have a connection to zero volts so I'll have a path from zero volts to the input pin the problem with that is that if I wire the circuit like this when I press the button I will have a short circuit directly between 5 volts and 0 volts because I will have a path directly from the power bus through row 22 through the button through row 24 down to ground or zero volts that is dangerous because it can cause a very large amount of current to flow and that can damage components in your circuit so we don't want to add a jumper wire directly from row 24 down to zero volts what we do instead is add a large resistor typically about 10 kilo ohms or 10 000 ohms or more we are going to connect this resistor between row 24 and ground that way when we do push the button we have a very large resistance between 5 volts and zero volts so very little current will flow and it won't damage your circuit however when the button is not pushed there is still a connection between row 24 and ground so this is called a pull down resistor because it will pull the voltage on the input pin down or low to zero volts not only does this help us avoid short circuits it ensures that the voltage is only ever in one of two states high or low if the resistor wasn't here at all and there was no connection to ground then the Arduino doesn't really know what to do when the button is not pressed because this voltage could be floating or some value in between 0 and 5. the digital input pins on the Arduino always expect the value to either be high or low and nothing in between don't worry if you didn't completely understand that explanation about wiring the button and what this resistor is for just make sure you follow this diagram and connect your button the same way it's shown here then we'll switch over to writing the code to use this button to control the LED in tinkercad I'm going to open the code window and make sure I have text coding selected and not blocks if you are using a physical Arduino you would open the Arduino IDE now follow along as I write the program to control the LED using the button first in my setup function I'm going to use the pin mode command to set the LED pin as an output I have the LED connected to pin 12 so I'm going to type pin mode open parentheses 12 comma output close parentheses semicolon next I'm going to set the button pin as an input so I'm going to type pin mode open parentheses 2 because I have the button connected to pin 2 comma input close parentheses semicolon we only need that code to happen once so it goes in our setup function then in our Loop function we are going to write code to continually check if the button is pressed and turn the LED on or off we're going to do that using something called an if else statement we are going to set up the structure of that statement first and then fill in the different parts do that by typing if open close parentheses curly bracket press enter close curly bracket press enter again type else open curly bracket press enter and close the curly bracket what this structure will do is say if whatever is in the parentheses here is true then do whatever is in between these curly brackets else if whatever is in these parentheses is not true do whatever code is in between these curly brackets this is the same structure we had in the pseudo code and flowchart earlier in the video we just didn't worry about the syntax like the parentheses and curly brackets there this is where it helps to use good programming practice and type comments to explain what your code does comments are useful for other people who read your code and for you if you come back to your code in the future and don't remember what it was supposed to do you can start a comment using a double slash the Arduino will then ignore any text after the double slash when running the code so for example here I'm going to type if the button is pressed turn the LED on else if the button is not pressed turn the LED off now we can go fill in the code to actually make these things happen you may remember that you can turn the LED on and off using the digital write command so to turn the LED on I'm going to use digital write 12 comma High because the LED is connected to pin 12. to turn the LED off I'm just going to copy and paste that line down here inside my else else statement and change that to digital write 12 low note that the code inside the if and else statements is indented another two spaces to help keep it organized we're still missing the part inside the parentheses here this is the condition our if statement will evaluate to see if it's true remember that we want to figure out if the voltage on pin 2 is high or low we do that using the digital read command which reads the value of a pin instead of writing to it we're going to do parentheses 2 because we want to check pin 2. then we are going to check if that voltage is high we do that by typing a double equal sign and then High that way this condition will be true if the voltage red on pin 2 is high and it will be false if the voltage read on pin 2 is low now in tinkercad I can hit start simulation or if you are using a physical Arduino you can upload the code to your Arduino once you do that and the code has uploaded you should be able to hold down the button and the LED will light up as long as you hold the button down if you release the button the LED should turn off now here's your first programming challenge the code so the LED is on when the button is not pressed and turns off when the button is pressed there's more than one way to do this pause the video here and see if you can figure out one or both methods one way to do this is to switch the high and low in the digital write commands note that if you're using tinkercad you need to stop the simulation before you can edit the code so for example if I change this High to a low I'm also going to change my comments to keep them consistent with the code that will turn the LED off and if I change this low to a high that will turn the LED on now if I hit start simulation we'll see that the LED is on by default and it turns off when I press the button however there's another way to accomplish the same task first I'm going to revert back to my original code or if the button is pressed I turn the LED on and if the button is not pressed I turn the LED off let me see that if I run the simulation again I'm back to my original hit behavior of the LED being off by default and turning on when I press the button now I can simply change the condition in my if statement from checking if the digital read value is high to checking if the digital read value is low that will check if the button is not pressed and now the else statement will happen if the button is pressed so I've left the digital write commands the same but I have switched around the logic in the condition that a if statement is checking so now if I start the simulation again we will see that the LED is on by default and turns off when I press the button so there were two different ways to accomplish the same task in the program now we'll use this opportunity to introduce a new programming concept imagine what would happen if I wanted to connect my led to a different pin on my Arduino I would have to change the number 12 in three different places in my code this creates a risk that I could forget to change it somewhere and then get an error when I try to upload my program instead I can use something called a variable a variable lets you store a number and then use the name that you give the variable throughout the program if you want to change the value of the variable you only need to change it once at the beginning let's demonstrate this with an example I want to create a variable to store the number 12 that way if I want to change my led to a different pin all I have to do is change the value of that variable I do that by declaring the variable at the top of the program this tells the Arduino that the variable exists I'm going to type int which tells the Arduino that the variable is an integer there are different types of variables but we're not going to worry about those for now then I get to make up a name for the variable for example you could call it LED pin you can also have underscores in the name but you can't have spaces then I am going to write equals and with the value I want the variable to be set to which in this case is 12 and then end the line with a semicolon as always it is good practice to add comments explaining what my code does so here I'm declaring a variable for the LED pin Now everywhere in my code that I previously had the number 12 I can replace with the name of my variable LED pin so I'm going to copy and paste it there there in there if I run my code I'll see that it still works exactly the same as it did before since I had changed the logic around previously now the LED is on when the button is not pressed and when I press the button the LED turns off however say I want to rewire my LED so it is connected to pin 11. now all I need to do in my code is change the value of my LED pin variable to 11. I don't have to change it anywhere else in the program and again I can hit start simulation or run the code on your physical Arduino and we see that the code works just the same because I swapped this pin both on the physical Arduino and in my code here is your next programming challenge add a variable to the code for the button pin and then replace the number two as needed in the code while you're at it make sure you add comments to any new lines of code and any existing lines that are missing comments pause the video here and try it out here's how you could do that just like I did for the LED pin I'm going to declare a variable for the button pin and set that equal to two and write a comment explaining what that does then I'm going to copy and paste the name of this variable and replace the number two in both my pin mode command and the setup function and in the digital read command in my Loop function now I should be able to run the simulation and again it will work as expected if I want to change my button to a different pin for example pin 3 I can move this wire over to pin three change the value of the variable to 3 in my code and then everything will still work finally there were a couple lines where I did not add comments earlier it is always a good idea to comment every line in your code remember to stop the simulation or it won't let you edit the text so here I am setting the LED pin as an output and I am setting the button pin as an input now you know the basics of using inputs and outputs with your Arduino you have the foundational skills that you will need to move on to using more advanced Hardware like sensors and Motors that will enable you to do something like build a robot 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: 67,155
Rating: undefined out of 5
Keywords: STEM
Id: yBgMJssXqHY
Channel Id: undefined
Length: 20min 57sec (1257 seconds)
Published: Thu Oct 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.