Arduino Tutorial 8: Understanding Pulse Width Modulation (PWM) and 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 calm and we are here today with lesson number eight in our new or improved our most excellent Arduino tutorial series and what we are going to talk today about is pulse width modulation now in lesson seven we taught you a new command on the Arduino and that command was analog right right we started with digital rights where you could turn a pin on digital right high turns it on 5 volts digit right low turns it off zero volts so you could turn something on or off by using digital right high and digital right low but then you said what about the in-between values what if we want 1 volt what if we want 4 volts and what we learned in lesson 7 is is that you have a command in the arduino which can sort of do that and that is the analog right command now what do I mean by sort of well we're going to talk a little bit more about how the analog right command works today and it works through something called PWM which is pulse width modulation and that is what we are going to talk about and that's what we are gonna learn about and you're gonna start again this series is about pulling up the hood looking under the hood digging deeper pulling back the onion and understanding a lot more of the real magic that is happening with the Arduino so I need you to pull yourself a big mug of iced coffee no sugar black coffee over ice and I need you to get out the most excellent super starter kit by Eloi Eloi go that you know our three project kit if you haven't gotten one of these yet check out the link down in the description hook a brother up click on the link and order one all of this new series is going to be based on this kit and so we will be working with this kit for a long long time we are already on lesson number eight in there are many many more lessons to come but I digress ok what I need you to do is go ahead and setup again that circuit that we had done in Lesson numbers and so we can go back and look at this again a very simple circuit I have a wire coming off of pen nine it is going to one leg of a resistor current limiting resistor going to the long leg of the LED short leg of the LED comes back to the ground on the Arduino and I'll give you more details of how to hook this up in lesson seven so if you're not sure what I'm talking about you can go back and look at level essence seven or you can look at where did it go this circuit and it should sort of make sense right we're using an Arduino pin for five volts we go through a 330 ohm resistor we go through the LED and then we come back through ground so this is all explained in in lesson number seven okay let me let me talk to you a little bit about what we did last time okay we came in and we started doing these this is the simple code that we are on pin nine so we called red pin nine and then here we created a variable called brightness and you can set the brightness and then you can do your pin modes and the void setup and then in the void loop we told you about this new this new command which was analog right and so let's look a little bit more about this analog right command and exactly how it works and so let's see what I'm going to do is I'm going to come in and set the brightness to zero okay so we will set the brightness to zero and if I download this analog right red pin to a brightness of zero this is a lot like doing a digital right of red pin low right and so let me go ahead and you can be watching the LED here and when I download this what happens them exactly what you expect it turned the LED off because you're setting the brightness to zero now we talked in the lesson seven that the values you can use are zero to 255 that's basically two to the eighth and so that's like an 8-bit number between zero and 255 is two to the eighth different positions or increment increments that you can do that's why it's that kind of odd number 255 and so a brightness of zero if we set that that would be equivalent to zero and a brightness of 255 would be equivalent to five volts or the same thing as a digital right hi I'm kind of rambling a little bit so I will try to get a move on here to the actual point and what we are going to do is we are going to come in wow look at this incredible shot I have we are going to be able to look at the signal that is coming off of the Arduino and look at it on an oscilloscope and basically an oscilloscope just shows you a picture of the voltage that you are getting out of something like the Arduino and so you see here we have a cable that comes out with a red and a black lead and where we hook these on the circuit will allow us to take a picture of the voltage at that point so I will show you how I hook it up by coming over here and so we're gonna get the red to that top leg of the resistor because that will look exactly precisely at the voltage coming off of pin nine and then we can also hook it over here to the bottom leg of the LED right there which is connected to ground and so by hooking it up in such a manner we are looking at precisely the voltage that is coming off of pin none so now that I have that hooked up I am just really really very particular about having things sit square I hate them to sit tilt it so I'm constantly adjusting so that things are Square that's pretty good okay I don't know is anybody else like that leave a comment below does it bother you if things are kind of crooked okay let me know leave a comment below but we are going to go to our most fantastic shot where we are looking here at the voltage a picture of the voltage that is coming off of the Arduino well what did we set the Arduino to we wrote a zero and so we would expect zero volts out if we look at this it is coming straight across right in the middle which is zero divisions okay and so we told it to output a brightness of zero or zero volts alright we're getting zero volts what would be the next thing that we could do we could write 255 and 255 is the maximum value and so what we expect out we would expect the full 5 volts out and then we would also expect when we download the the 255 we would expect the LED to come on and so let's come over here let's download it got the happy little green bar and boom the red LED is on full brightness now ladies and gentlemen let's come over here and let's look at the oscilloscope in fact I'm going to zoom in on the oscilloscope remember it was down here but now it is up like this is one division two divisions about 2.4 divisions ok and the knob is set on 2 volts per division and so if I have two point four divisions times 2 volts per division that is telling me that that's an output voltage of about four point eight volts now understand the thing about an oscilloscope is it's really intended to show you a picture of the waveform it doesn't show you an exact value if you wanted an exact value you would go get a digital voltmeter and read the voltage the the nice thing about the oscilloscope is that shows you a picture of the voltage but it's not exactly accurate and so we're we're getting four point eight volts that is really pretty good and and about as close as you would expect working with a working with a oscilloscope let me see let me make sure that I had that rounded out and good you know that was good and so that is that's probably about as accurate as we're going to get so what's gonna happen if I come over here and I need to show you this view again so what happens if I come over and I write a zero brightness okay keep your eyes on this right here where my pin is pointing and now I'm gonna download boom it goes to zero volts alright I'm gonna go back to 255 boom it pops up to five volts it is behaving exactly the way that we would expect it all right well let's do a half right what would if we wanted instead of five if we wanted two and a half volts that would be like that would be like 255 divided by two because that would be half a five we would put in like 127 and so we're gonna come over here and we're gonna put in 127 for the brightness and then that should make the LED have brightness and then this should come down halfway to like a little over one division right so let's download it uh-oh what happened well the LED went to half brightness but we have something strange going on on the oscilloscope something very strange so let's zoom in so when we set a brightness of 127 we were expecting instead of being up here at 5 volts we were expecting to come down to two and a half volts but do you see what's happening it's simulating two and a half volts by switching back and forth between five and zero five and zero and it's at five half the time and it's at zero half the time so it's averaging it's averaging two and a half volts does that make sense it is averaging two and a half volts so when we say like to an analog write of 127 for two and a half volts it doesn't give us two and a half volts it gives us five zero five zero five zero also let's look and see what kind of period this has so we start here and then we go one two three four it looks like about four point two divisions so we have four point two divisions and it is set on point five milliseconds per division so I'll multiply by 0.5 e to the negative three point five milliseconds so that has a period of about let me get this here it has a period I'm getting my calculator set up it has a period of about four milliseconds okay it's got a period of about four milliseconds and what this is doing is it's staying at five volts for two milliseconds and then it is staying at zero for two milliseconds with that averaging out to about two and a half volts so that is called pulse width modulation so you have four millisecond period and then how much of that period do you stay on and how much of that period do you stay off and so let's get a really dim number like let's set it to ten and we would expect 10 to be 10 divided by 255 we would expect that to be over let's see 10 divided by 255 times 5 we would expect that to be about 0.2 volts okay and so let's download that and I oh my goodness so look at that it is and it is where you can barely even see how long it is on I'm trying to get it to trigger a little bit differently it's kind of hard to trigger when you're like this let's see if I can okay that's pretty good there you go okay so you can see that it is on for just a very very small amount of time it's at five volts and then off most of the time and so you can see that this would be equivalent to an average of about 0.2 volts because I'm on four five volts for a very short period of time and then off most of the time what if we went instead of ten let's see if we can even see it if we go to one it might get so small that we can't even see it okay oh you can sort of see it just that barely tick there so it's like here here here do you notice as we're doing this the period isn't changing the period is still the same with four milliseconds but we're just leaving it on for a short period of time or let's put it at ten a longer period of time okay do you see how that's on longer what if we go to a hundred it's on four even longer what if we go for that's not liking the trigger there very much okay what if we go to 200 that's going to be on for most of the time and then just off for a little bit and then what if we go to like 250 okay and that is on almost the whole time which is that one little click that one little tick down so what you can see is pulse width modulation has a constant period and it's just changing at what fraction of that period the signal is on versus the signal is off thereby creating not two and a half volts but if you average over the period an average of two and a half volts and so that is how the analog right function on Arduino really works and what we're really getting is pulse width modulation which we have kind of explained today now the other thing that I will show you is what if you really wanted an analog voltage if you really wanted an analog voltage you could put a capacitor across there and the capacitor would smooth out that up and down and give you the average value for something like this I am using about a thousand micro farad capacitor and so that is a pretty darn big capacitor I'm gonna come over here back to this shot is this this shot we want yeah that's a good shot so what I'm going to do is I'm going to take this oscilloscope off and then these capacitors a lot of times they're they have a polarity to them so you got to make sure that you're putting the negative to the negative and the positive to the positive and so I'm going to hook this across the voltage that's coming out of the Arduino look it across there like that so I go the positive lead to the positive voltage in the negative leave of the capacitor to the ground and now I'm going to hook the oscilloscope back up okay and so now let's put in some values let's put in in okay so you can kind of see this I'll just go to the oscilloscope full screen again okay so let's know let's go back okay so you can see what I'm coding so let's put in 2:55 downloaded okay in boom we are up to the full 5 volts what if I put 127 which would be about half of that boom you see I come down to about half now what you can see is there's a little waviness in that signal and what that waviness is coming from is is that the capacitor doesn't completely smooth it out but he gets it pretty darn smooth so let's go down by half again let's go down to about sixty okay and look at that we're down to about half of half of a division there so what you can see is is that the pulse width modulation for a lot of things like an LED it really works because what's really happening is it's blinking the LED on and off really fast but it's blinking it so fast that you can't see it and it does look like dimmer or brighter same times same thing a lot of times with a motor this it'll sort of average it out as well that's the way pulse width modulation works if you want a true analog signal you're gonna have to look more into capacitors and use a capacitor to smooth out the signal does this make sense I feel like I rambled a little bit too much in this I feel like I rambled a little bit too much in this video would really love to hear your comments down below do you understand pulse width modulation do you understand what the Arduino is really doing when you give an analog write command kind of say it back to me let me see that you actually understand what's going on really enjoy it when you guys comment like it even more when you give me a thumbs up or share the video and I hope you'll stay tuned we're gonna have a lot more great videos coming don't forget this series iced coffee no sugar Palmer quarter from top tech boy comm I will I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 174,290
Rating: 4.9742351 out of 5
Keywords: PWM, Pulse Width Modulation, STEM, Arduino, Tutorial, AnalogWrite
Id: YfV-vYT3yfQ
Channel Id: undefined
Length: 19min 36sec (1176 seconds)
Published: Tue Jun 25 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.