Paul McWhorter arduino lesson 3
If you haven’t watched lesson number one, yet it played be good to go ahead and tune into that. We show you how to get the Arduino set up and running with your first. Very simple program: what we’re going to do in this lesson is we’re actually going to use the Arduino to control our first external circuit and the circuit that we’re going to be trying to build is shown here, it’s a very simple circuit, where we apply a five Volt signal the five volt signal is going to go across a resistor to an LED and is going to turn the LED on okay. This is a pretty simple thing, and why do we start with an LED? We start with an LED because you can buy an LED for about a dime and therefore we’re, not risking very much if something goes wrong. You’Ve just burned out an LED and nothing more expensive, and the second reason is is that people have very rarely ever gotten hurt with an LED and so it’s. A very low cost component is a very safe component, so it’s a good component to learn on the thing to understand, though, is the programming techniques that we will be showing, even though we’re just turning an LED on and off. The thing is, if you can turn an LED on, you could also turn a motor on, and if you could turn a motor on, you could open a door or open a window.
So the things that we’re doing to turn an LED on and off could be the same types of programming techniques that you would use to operate a motor or run other more sophisticated devices and so it’s a good place to start. What you can see here is this is the circuit that we are trying to achieve. One thing to understand, though, is that we will be getting this voltage from the Arduino itself if we were just to come in and hook this circuit up with a battery or a fixed voltage. It’S not going to be very interesting because the LED is going to come on and just sit there we’re not going to have any way to control it. So what we’re going to do is we’re going to use the Arduino to provide the five volt source and that way we can turn things on or off, or do things more interesting based on the program that we write so that’s? What we’re going to be doing today now, if you thought about this and if you’ve ever done anything with circuits, maybe you’ve done things with putting a stereo in your car. Putting speakers in what you would think is: okay. I got to hook these things together. So maybe you would have some wires like that. You say: okay, I’ve got a I’ve got a hook to the one leg of the LED, and then I got to come off the other side of that LED and I got to come and I got to hook that to the resistor, and so I come in Here I connect this the resistor and then I need more wire, so I can hook it up to the battery battery or the voltage supply and what you can see if you try to do this very quickly, you’re going to end up with nothing but a rat’s Nest and invariably it’s not going to work because you’re going to end up with two things touching that aren’t supposed to be touching and you’re, going to end up with a short in your circuit and it’s never going to work, and so you just simply can’t hook Up circuits, as they get more complicated by running wires around like what I’ve tried to show here, you need a better way to hook your components together and that’s.
Why, today, what we are really going to talk about is we’re going to talk about the breadboard, and the breadboard is something that allows you to hook a circuit up very neatly, so that you can keep it organized and so that you can keep keep track of Your wires and get something that you can get that makes something that’s actually going to work because you can keep it organized, and so, if we’re going to use these bread boards we’re going to kind of have to learn how the things are hooked together and learn. How they actually are kind of organized and so I’m going to take a second here and try to just talk you through the breadboard that I’ve made this nice graphic here to show you how things are hooked together on the board. The thing to understand is with all of these holes. The thing to understand is along a column, the holes along a column are connected together. So if I want to hook this leg of the led to this leg of the resistor, I would need to hook those two legs in a common column. So I could put one leg of the led in this hole and I could put one leg of the resistor in this hole and because these two holes are in the same column, those two pins are now connected together. So I connect components together by placing them in a common column. Okay. Similarly, this column is hooked together.
Okay, the thing to understand, though, is, is that things break at this trench. So basically, this hole is connected to all of these holes, but this hole is not connected to this hole because the connection does not go across the trench and most all breadboards are that way they have a trench in the middle and the connection stop across the Trench another thing to understand is is that holes along rows are not connected together, so this hole is not connected to this hole and it’s not connected to this hole. Holes along a row are not connected. Holes across a trench are not connected, but holes along a column up until the trench are connecting holes along a column up until the trench are not are connected. Okay, the thing to see also is: is that there’s nothing magic about this column? These holes are connected together. These holes are connected together, so along the column along any column, the holes are connected together, there’s one other special case and that’s for the top two rows and the bottom two rows. The top row is all connected to itself. So, along this top row, row number one all of those holes are connected together. Similarly, all along row number two all of row. Number 2 is connected together. Similarly, the very bottom row is all connected together and the second to the bottom row is all connected together. So the top two rows are special cases Row, one all connected together to itself Row, two all connected together to itself.
Second, to the bottom row same thing, bottom row same thing, and the reason for that is is that that allows you like a lot of times. As you get more and more components, let’s say you wanted 10 LEDs, you could come across and you could have a voltage across that whole row that establishes a 5 volts and every time you need 5 volts. You can just hook up to that first rope. The thing to understand is you see that that first row is labeled positive it’s, not positive, because it’s labeled positive it’s positive when and if you bring a positive voltage into it. Similarly, that second row is labeled, negative or ground that becomes ground when you bring a ground into it, so don’t get confused. Those labels are just convenient to remind you how to hook things up, but you have to bring in the positive voltage for it to be positive. You have to bring in the negative voltage to row two for it to be negative, and if you hook those up backwards, they will indeed be backwards. So remember it matters how you hook it up, not how its labeled okay. So with that, we could probably start thinking about hooking together a circuit, so let’s go back and look at this circuit that we are trying to connect – and I think I can get to it here remember this is the circuit that we are trying to build, and So what would this would probably be a good time is? This would be a good time to just pause the video and then look at this circuit and think how you would make those connections on this board and then also remember that you’ve got to bring your voltage on the phone.
i from one of the pins on the Arduino, the pin that you use is just basically which one you choose you could use any one of these digital pins here let’s see. If we can get that on, get that to focus. You could use any of these pins between 10 zero and pin 13 I’m going to use, pin 9, but you can use whichever one you want, but it’s. Just when you hook it up the code that you rod has to match the circuit that she’s on the circuit that you build so go ahead and pause for a second and then, after you think, you’ve figured out how to hook it up, come back and I’ll Show you how it up? Okay, alright! So if you’ve taken a look at it, hopefully you have figured out a way that you would hook it in that you would hook it up. Let me kind of show you how I hooked it up. I mentioned that I was going to use, pin nine, and so you can see that ten nine comes here and then, if you remember we needed to go to, it would sure be great if I can get both of these up at the same time. But maybe I can just kind of toggle between them a little bit here. So if you look at the circuit, you wanted to go from your positive voltage, which for me, is going to be ten nine coming off the Arduino to one leg of the resistor.
So you can see here that I’ve got pin nine coming over to one leg of the resistor. This leg of the resistor is connected to, and I do need to be in here closing this leg of the resistor is connected to this wire because they are, in the same column, there’s, nothing magic about this column, but it’s. Just that leg of that resistor has to be connected to this wire and then that wire goes and connects to pin nine. So that brings the voltage into the left side of the resistor. Then, on the right side of the resistor, we wanted to connect to the diode okay, this leg and this are in the same column. So this leg of this resistor is now connected to this diode. And now this leg of the diode is connected to this wire, which was the bottom side of our circuit because they are in the same column, and then I bring that wire back and connected to ground, so we’ve got sort of the complete circuit. Here we go from, pin nine, which is the positive voltage we come to the resistor. The other side of the resistor is connected to the led. The other leg of the LED is connected to this wire, and then that goes back to ground. Okay, there’s – something very important here that you need to understand – and that is – is that the resistor does not care which direction that it’s plugged in it can be plugged in this way or it can be plugged in that way.
The resistor doesn’t care, which direction it plug it’s plugged in. I should remind you that if you’re using the spark phone or if you just have a part supply, you need to make sure that you’re, using about a 330 ohm resistor as a current limiter and the way you can tell what the resistance value is. You can look at the color code and the color code for a 330 ohm resistor is orange orange brown, and so, if you’re, using the sparkfun and better kit, you want to make sure that you get one of the orange orange brown resistors and it should be Labeled in their 330 ohm, but never trust the label, because somebody might have used the kit before you and they might have put the 10k resistor back in that package. And so you really want to learn your your color codes and you never want to trust the label on the bag, because people sometimes put things back and the wrong in the wrong bag. Anyway, you got the 330 ohm resistor it doesn’t care which direction it plug. It’S plugged in you could plug it in this way or you could reverse it and plug it in the other way, either way it doesn’t matter the LED does matter. The LED only works if it’s plugged in a certain specific direction, and if you look at the LED, you can see that one leg is longer than the other leg. The long leg is called the anode and the anode needs to be connected to the positive side of the circuit.
So you’ve got the positive voltage coming in from the red wire going across the resistor it’s. That positive voltage needs to be connected to the anode, and the anode is the long wire okay. So basically you don’t really have to remember anode and cathode. So much. The easy thing to remember is the longer wire needs to be connected to the more positive part of the circuit, and so for us that is right here. Okay, now let’s go ahead and see if we can start trying to kind of build this thing, I think how to try to make this one, maybe bigger, make this window bigger and see. If you can see it a little better help, you see it okay, so now we’ve got this a little bit bigger and I’m going to come over here behind it, and basically I can probably make myself smaller as well and try to get these windows so that You can see what you need to see and we can just hook this circuit up and I hope you sort of work along side me here as I’m doing this, probably behind us down. So what you can see is let’s start at the Arduino and we’re going to start at pen 9 I’m going to use a red wire. Just like I did in the picture and I’m going to plug into pin 9, and hopefully you can kind of see that there we go and then pin 9 just comes over here.
You can kind of use any column that you want, but once you start using it, you need to kind of keep track, of which one you’re in and so I’m just going to put it there and then what we saw is the resistor here needs to be In that same column, so I’m going to come in and I’m going to plug one leg of the resistor into that column and then I’m, going to just let the resistor kind of go across to where sort of a natural stopping point is there. You can see. I want just a couple of rows over, so the left leg of the resistor is, in the same column as the red wire. Now, what we know is we know from here that the LED needs to be in the same column so that it connects with the other leg of the resistor and remember we want the anode. We want the long leg of the resistor in that column and so let’s see if I can’t bring this over a little bit, we’re going to put that long leg of the resistor. But the long leg of the LED in that same column as the resistor, and so you got to kind of plug it in and let’s see. If you can see that you can see that the long leg of the LED is plugged into the same column as that second leg of the resistor and then the other leg of the resistor.
I used a gray wire because it shows up a little bit that bit better, but I will use a black wire here. The black wire in that same column is the short leg of the LED, and so you can see that got it in the short column of the LED, and then that goes back in the picture here. So we’ve just made this connection that goes back to the ground on the Arduino, so let’s look here and see if we can see that ground pin. Okay, that is right there and I plug it in, and what you can see is is that this starts working, because I had actually left a program in it from earlier and so let’s see if we can get rid of that program. Okay, so at least I know I hooked it up right, okay, so you can see that this. This looks a lot like this on the screen and this on the screen. We did to match what our original circuit was, which is shown here. Okay, so we kind of got the circuit matching the schematic, the graphical diagram, and then we have that let’s actually close my window. There we go. We got that then matching what we have here, and so let me go back and get the diagram here. Okay, so this diagram is basically what we have built here and have hooked up to the Arduino and so that’s the kind of that’s, the sort of hardware part of it and let’s see.
If I can get this son in the field of view here for the camera – okay, that looks like that looks good, alright. Now what we can do now is, ah we can come in and we can start writing our code for our Arduino, and so at this point it would probably be better for me to make this window small so that we can focus on the codes we’ll go Back to a smaller window on the camera – okay let’s back over to me, okay, so this is probably a pretty good place for us to start working on our code. So we call up the arduino ide again the integrated development environment and, as we explained in the first video there’s, two parts that every Arduino program have a voice setup. The setup is inside of these curly brackets and it’s. The things that you would want to just do one time so typically that’s declaring our pin modes, telling the Arduino what’s going to be an input and what’s going to be an output, and then we have our loop and our void. Loop begins with this curly bracket and ends with this curly bracket. So whatever we put between those two curly brackets is going to execute over and over and over and over it’s going to loop through those and the things that we put in the set up are just going to just going to do run one time. The other thing that we talked about in the first video and I’ll – just really try to remind you up here, is the importance of declaring our variables that we never want to go in and set a pin mode.
Like you know, we said we use pin 9 down here. We would never want to come in and say 10 mode, a pin 9. We never want to put constants in our pin modes. We want to always use variables because that’s, a good programming practices and our variables that we define up at the top here are our global variables. Normally, you don’t want to use global variables. Normally, you want to use local variables like just define the variables only where you need them. If you need them in void, set up put an invoice set up if you need them in void, loop put them in void, loop and they’re. Only valid they’re. The one of the few types of variables you really want is a global variable would be the the variables that you’re going to use to define your pins, because those are the ones that everybody needs to know so we’re going to go ahead and do some age. Some global variables here and remember: we declare our variables by telling them that they’re int, if we’re, going to declare which variable we have or which pin we have we’re going to hook to the pins, come in discrete numbers. 1. 2. 3. 4. 0 through 13. So our pins are all integers, you would never have a pen, 9 and a half. You would have a pen 0 or a pen 12. You would never have a pen 9 and a half, so your pins need to be declared as integers and now.
Int is very specific. That tells is going to be an integer, but now in declaring your variable, you can make the variable whatever name you want, but you want to make it something that makes sense. And so, if you look our pin, our LED is red, and so why don’t we call this variable red, LED, okay, and that is going to be connected to which pin that’s connected to ten nine, okay and that’s, because that’s, where we hooked it up on the Circuit board, if you want to make it pin 12, you could hook it up to pin 12 and then you could make this pin 12 so it’s, whatever you want, but not as one that’s going on being that’s going to work. Okay, if you remember in the first video as well one of the things that we did was we were playing with, leaving the light on, leaving it off turning it on and off and remember. We had variable of life on time and that’s going to be how long we leave the light on and remember that these are measured in milliseconds, so there’s a thousand milliseconds in a second. So if we want to leave the LED on for half a second, that would be how many milliseconds half of a thousand would be 500, and then we also have an INT, which is off time and that’s, going to be 500 as well again, you could have Called the on time, whatever you want it or the off time, whatever you wanted, you could call this a and you can call this B, but the nice thing is to name your variables with something that sort of tell you what you’re doing with them.
So, as your programs get more complicated, your variable names help. You keep track of what’s going on so we’re going to say that we want the on time to be 500 milliseconds and we want the off time to be 500 milliseconds. What do we do in our void setup? These would be things that we’re just going to do one time and what what that would be is set our pin modes until we say pen mode and what is our pen, our pen is nine, and then that is an output, okay, and so we set ten Nine to output, no, no don’t do it that way. Never ever ever put a constant in your PIN mode. You you told it what you wanted to be up here: red LED is pen 9, and so, when you come down here, don’t put the constant nine in there put the variable red LED, which you declare it up here and that way, if I come in later And I’ll make this 10. It changes it here and it changes it everywhere else that you use it so use variables and as I’m teaching this is it not been grading. I count off if I ever see you put constants in here, because it’s good programming practices to declare a variable, a sign of value to a variable and then use that variable. So we only we only use variables in our in our pinmode declarations. So we make 10 mode which one red LED, which is 10 9.
Can we make that an output because we’re going to write to it? Now we come down to our void loop, and this is where we actually are going to turn the light on or off and to turn it on. We do a digital right. Did you toll right and what do we write? 2. We write 2. 10. 9. No, no, no do not write 2.9, you write 2 re D LED which is 9, and then you want to make it hot and that will turn it on doing. The digital right high is applying that 5 volts that we need. That was shown in our circuit schematic. The way we get those at 5 volts is we do a digital right, hi, okay, so that’s going to turn the light on so let’s. Just take a look at this now at this point and see if it’s going to work so let’s go ahead and run the program. When I run this program, we don’t want to see any orange down here, meaning that we don’t have any mistakes, and then we should see the LED turn on so all green down here. Everybody’S happy the program downloads and the LED turns on it. Sort of looks white because the camera it’s very bright, but if you look at it in person, it is in fact red, and so that turned it on now. How do we turn that LED off? Well, you would come in and we would digital raat LED red LED low, changing it here.
Doesn’T make it turn off here, because we haven’t downloaded the code into the Arduino. Yet when we click this, the code will come down into the Arduino and then it will turn it off and you can see what’s the code downloaded. It turned the LED off now that’s kind of simple, just turning an LED on and off, but this is a very powerful technique because, like I said, if you can turn an LED on and off, you can turn a motor on and off and you can turn A motor on and off you can open and close a door or open and close a window. So all of a sudden, some really interesting things start happening now. If we want to make it blink, we’ve got to turn it on, and then we got to tell it how long we want it to stay on so we’re going to do a delay after we turn it on we’re going to delay by 500. No! No! No! Do not put constants down here. We define the on time up here is 500, so we come down here and we say on time we delay. So what we do is we turn it on and then we wait. We wait how long on time, what it’s on time on time is 500 milliseconds after we have waited 500 milliseconds, we do a digital right and then we do to where our edie LED and we make boom. This should be high okay, so we digital right, red, LED high turns it on.
We wait for on time, which is 500 milliseconds, which is half a second, and then we are going to digital right red, LED low, okay, don’t forget your semicolons. At the end of all your lines of code now, usually when things don’t work, is you forget your semicolons okay? So now we would turn it on. We would wait 500 milliseconds and then we would turn it off, but it would come back on really quickly. So we got to have our second delay, which is our off time. Okay, so now let’s, look at the line of code. We’Re telling turn on for 500 milliseconds earn off and then wait. 500 milliseconds so turn on. Wait. 500 milliseconds turn off wait: 500. Milliseconds! Then this is the void loop, so it’s going to be looping in here turn on wait, half a second turn off. Wait: half a second! Do it again turn on wait? Half a second turn off. Wait! Half a second! So if we did this right, the light should be on for half a second and offer half a second everybody’s, happy it’s downloading on on on off now, the neat thing is is because we use variables we can kind of start affecting things by what we do Here so let’s say instead of half in half what, if we were on for only 10 milliseconds and we were off for or let’s say on for 100 milliseconds and off for 900, so that’s going to be on for 110 off for 910.
So, in that case it should just blink and be off most of the time. So look at what’s look and see what that does hit the wrong button, all right there. It goes everybody’s happy, and here you can see briefly on and off for most of the time. Well, what if we do it the other way? What if we come in and say let’s be on for 900 and let’s be off for 100. Now, what it’s going to do is it’s going to come on after this downloads and it’s going to be on most of the time and then off on for most of the time and then just very briefly off ok, so what your assignment is. I want you to start playing around with this and what your assignment is, do something different with the LED, maybe make it come on five times fast and then five times slow, just build in these code, this code down here, turning it on and off where it Does what you want it to do, but when I come around for a grade or if you’re doing this just on your own, what you should just do on your own is make the LED do something unique that you decide. This is what I want the LED to do and then come in and change this code or add code to it to make the LED do what you want. Also, if you’re part of my class, then what I also want you to do for a grade is add a second or a third LED.
Instead of just having a red LED and your spark fund and dinner tip, you have some yellow LEDs. Maybe you should add a yellow, LED in addition to the red, LED let’s, take a look here: okay, just picking red ones! Well, okay, there’s y’all! I had a yellow one, if you add a yellow one, if you connected the yellow one in the same place that you connect the red when they’re going to blink together like in fact, I could probably do that right now, with a little luck, I could hook Them together and you see that same signal is making them blink together, and you could do that pretty. You could do that pretty easily, but you see that’s not very interesting because they’re doing the same thing. What I want you to do is I want you to create a different pin so that you create a different circuit you’re, going to need another resistor and you’re going to need another pin off the Arduino like maybe besides pin 9. Maybe you should use pen 8 set that up this yellow LED and then you can independently control the red LED and the yellow, LED by having more lines of code in your program but that’s. What I want you to do, I want you, for your assignment is to make the red LED work independently from the yellow, LED and write some code that will make it do that. Okay, that’s video number two video Arduino lesson: number two hope you guys will come back soon and tune in for video lesson number three: we will keep getting gradually more and more complicated in our programming.
We will be working with LEDs for a while because again they’re cheap and they are safe and so we’re going to kind of learn on these LEDs and then, as we go on we’ll start incorporating more sophisticated components.
Paul McWhorter arduino lesson 3 Video
Paul McWhorter arduino lesson 3 news
Posted on Friday August 19, 2016Books You Should Read: Basic Electronics Hackaday … Continue Reading »
Paul McWhorter arduino lesson 3 Social