Arduino Tutorial 7: Understanding the Arduino Analog Write Command

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and we are here today with lesson number seven in our most excellent series of Arduino tutorials the new the improved Arduino tutorials so I hope you guys will pull your suppor yourself a nice big mug of iced coffee no sugar black iced coffee okay that's what you need to get through this lesson that is very very good also need you to get out your a lego super starter kit if you haven't picked one up yet click on the link in the description below and you can pick one up for about thirty five dollars and it is a most excellent value and what I will be using for all of the remaining tutorials in this series ok so what is this lesson going to be about we are going to learn how to do analog writes with the Arduino what have we learned so far on the Arduino we have learned how to do digital rights where we can take one of the pins and we can set it high or load digital right high or digital right low we have 13 pins we can use pins 2 through 13 to do a digital right we can turn those pins on or high or we can turn them off but you ask what happens if I want one of the in-between voltages I want something between 0 volts and 5 volts well we can sort of do that with a command called analog right I say sort of do it because there are a few caveats which I will explain in the next video but for right now let's just consider it a an analog voltage and we'll talk more about the nuts and bolts of it in the next video so what do we need to get out to do this project we need to have let's see I will go here I will get out of your way is that far enough I think that's far enough so what you need is you need to get your Arduino you need 2 wires you need your breadboard you need a red LED a red LED and you need your 330 ohm resistor and so we are going to hook this circuit up again and we've done this before I'm going to go a little faster because we did it I believe in lesson number three where we talked about how a breadboard works and then we did it again in Lesson number six where we built a binary counter so you've done this a couple of times I'm gonna go faster but just real quick we need to connect to a pin to provide the voltage will go through the resistor to the long leg of the LED short short leg of the LED then back to ground okay so we've done that if you can't I will go a little bit faster this time because you should pretty much understand that but I do have to tell you something very important and let's see if I can get this to focus I wish I had a better document camera I really oh that's pretty good okay now in the earlier digital rights you could use anything from pin two to pin 13 you could turn it on or off in the analog right only some of the pins will work and it is the pins that have the squiggly okay DC pin 3 has the squiggly bite let's see if I can point it the squiggly it's kind of hard the squiggly there it is pin three point five pin 6 10 9 10 and 11 so you have to use one of those if you want to do an analogue right 3 5 6 9 10 or 11 I think I'll use pin 9 just because I can and I am gonna go a little bit faster here because you guys ought to know how to hook this stuff up so I go to pin 9 and then I bring that voltage from pin 9 over I think I'm gonna put it in column 15 it doesn't matter you can put it in whatever column okay then I need my resistor one leg of the resistor is going to go into column 15 I'll put the other one in column 20 but again it doesn't matter but what does matter is one leg of the resistor has to be in the same column sorry the same column as this red wire now I bring in my LED and I put the long leg in the column with the other leg of the resistor and then the other leg of the LED I jump across I jump across this trench and because the column here is not connected to the column below the trench in that way it looks like I'm still in the same column but the two legs of the LED are not connected okay so that is all connected and then I am ready to go right no what do I need to hook up I need to hook up the bottom of leg of the LED the short leg of the LED to the ground okay and so I have a convenient black wire here so I will go to that same column as the short leg of the LED and then I will come back to GND or ground that is by pin 13 now notice here I only need one ground and so I don't need to set up a ground rail down here and if I don't need a ground rail I don't set up a ground rail and finally need one ground I do not need a ground reel okay so now we have got our circuit built what do we do now we open up the Arduino IDE and you can see that here and we start writing our code so how many pins do I have I have one pin that I'm using it's pin nine and remember I do not want to see you using constants anywhere in your code so we are going to use variables so instead of putting nine and you know all throughout the program we're going to set it up here what kind of variable is it going to be it is going to be an integer so we declare it as int and then I will call it red pin okay or yeah red pin is good and then that's going to be equal to pin nine so now anytime I want to go and connect to the red LED all I got to do is just put in red pin okay now we are using pin nine and therefore what do I need to do in the void set up pinmode right gotta do a pinmode so I tied pin mode and then what pin well the red pin and then what do I want to set it at output all capitals all almost all commands end in a semicolon so there I am ready to go now down here I am going to turn the LED on in the void loop but instead of doing a digital right I will do an analog right so analog right okay do you see if I use the capitalization right it turns that happy little orange color analog right with capital W there are two parameters the first is what pin you want to use well I want to use red pin okay red pin and then what value okay now this is where things are a little different okay this is where things are a little different before you just said hi or you said whoa but now you can put in the in-between values and this is where it is a little funny so let me come back over here and show this word you can see it let me get this out of the way okay so the parameter the second parameter is not high or low it is a number between 0 and 255 okay that sounds strange but we'll talk about that a little bit more in a minute 0 corresponds to 0 volts and 255 does not correspond to 255 volts it corresponds to 5 volts ok now where does a crazy number like 255 come well let's think about binary you remember binary we start with 1 2 4 8 16 32 64 128 and then 256 ok so this would be like 1 2 3 4 5 6 7 this would be like a bits okay it's like if I have two raised to the 8th I'm gonna check that just to make sure with my trusty calculator to raise to the 8th is 256 okay so what this is saying is is that 256 this would be considered like you have 8 bits of resolution in the voltage that you can create because 2 to the 8th is 256 but what you got to remember is it corresponds to 5 volts now you ask why does it not go between 0 and 256 why does it go between 0 and 255 because you have 256 bits and 0 takes one of those and so if you start counting at 0 and you've got 256 slots you have to start it - you have to stop at 255 does that make sense if we started it at 1 we could go to 256 but since we're starting at 0 we stop at 255 to use up our 256 slots ok so let's just review how this works if you write a value of 0 you will get 0 volts if you write a value of 255 you will get 5 volts ok and it scales in between there and so like if you wrote let's say 125 ok that would be about two and a half volts now I know I know it doesn't work exactly that way but we'll talk more about the finer points in the next video ok so you've got to put in a number between 0 and 255 ok I'll just say like what if we wanted to let's come back over here ok so we got to put our parameter in here so let's say that I wanted to do the same thing as a digital right high with a analog right well I would say analog right red pen and then 255 and that would be the same thing as an analog right hi right okay why are you screaming at me no no no no what we do not use numbers down here right we create a new variable up here what are we gonna call it let's call it it's it's an int okay we're gonna call it bright like how bright do you want it and we'll set that to 255 now what do we write down here well we write the brightness which is the variable bright all right always use variables don't use constants down here it's good programming practice okay so this should do the same thing as a an analog right red pen bright at 255 should do the same thing as a digital right high so let's see if we can download this and see what happens and let me go back over here I got to get a better shot okay here we go boom look at that it came on it came on what if I wanted to do the same thing as a digital right red pen low well I would set brightness - what - zero okay and now let's download that and then let's watch over here boom it went out it went off okay what do you want me to do now well let's do an in-between let's do like 125 okay and so we'll download that boom man this looks like half brightness to me but it's kind of saturating the camera so you can't really see it I'll probably have to turn this down like let's turn it down to a five and you can probably see it dim if I turn it to a five okay yeah you can see that's kind of really dim there wonder if I lay it over if you can see it better I can kind of bend it over like that maybe you can see it yeah you see it's just sort of barely on there so let's go ten and then when I go ten let's see if we can see it get a little brighter okay yeah a little brighter let's go a hundred boom okay let's go to 55 okay full brightness so we have done it analog rights and this was a pretty short video okay but I want you guys to follow along play along with this some of the things that we learned is we learned that we use variables up here and besides digital right we can do an analog right and we can get the in between values okay would love to hear your comments down below would love you to give me a thumbs up like to interact with you guys down in the comments and so I hope I will hear from you think about sharing this video think about subscribing to the channel okay palma quarter top tech boy calm next lesson up we're gonna go in a little more detail and show you how this analog right really works like what's really going on it's it's like we're really I'll give you a hint we're simulating we are simulating analog voltages we're not really doing in a low voltage is but we'll talk about that next time okay what kind of assignment can I give you okay I want you to take this now I want you to turn it very dim and then leave it for 5 seconds and then have it go intermediate for 5 seconds and then have it go brighter for 5 seconds where you sort of do analog right wait or analog right delay analog right delay analog right delay and then play with the brightness value so you would have like brightness 1 brightness 2 brightness 3 brightness forward brightness 5 so you sort of apply in your program those different brightnesses and then you can watch it sort of step up in value so I want you to play around with this and do more than just this one analog writing I do want to give you homeworks and I do want you to do these things because the problem is it makes sense when you watch me do it and you can copy what I'm doing but you got to get out there a little bit on your own you got to develop your confidence where you go out and you do some programming on your own and then when it doesn't work you have to go in and you have to figure out why it doesn't work and if you write programs on your own we'll make mistake then when you make mistakes you learn to troubleshoot okay guys this is palma quarter from top tech boy comm enjoying my mug of coffee and i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 360,605
Rating: undefined out of 5
Keywords: stem, Arduino, Tutorials, Lessons, AnalogWrite
Id: 6CRhpUV92ww
Channel Id: undefined
Length: 15min 52sec (952 seconds)
Published: Thu Jun 20 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.