Arduino Uno R4 WiFi LESSON 11: Pulse Width Modulation (PWM) Simulation of Analog Voltages

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcarter with toptechboy do.com and we're here today with episode number 11 in our incredible new tutorial Series where you are learning how to think like an engineer using the Arduino Uno R4 WiFi what I will need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee as always I want to give a shout out to our friends over at sunfounder sunfounder is actually sponsoring this most excellent series of video lessons and in this class we will be using the sunfounder elite Explorer kit now hopefully most of you guys already have your gear but if you don't take a look down in the description there is a link over to Amazon you can hop on over there and pick a kit up and believe me your life and my life are going to be a whole lot easier if we are working on identical Hardware but enough of this Shameless self-promotion let's jump in and talk about what I am going to teach you today and what I'm going to do is I'm going to do a little bit of a deep dive into the topic that we looked at last week in lesson number 10 now what we learned in lesson number 10 is we could simulate analog output voltages using the Arduino if we used one of those little squiggly pins one of those digital pins with the squigglies we can get something out of that that acts like an analog voltage so what did we do we showed that we could control the brightness of an LED with an analog right command so if we did an analog right at a value of 255 it was full brightness of 127 it was half brightness half again quarter brightness and we could control the brightness of the LED and we did that by simulating analog voltages now last week I might have just said analog voltages and referred you to this week so what we're going to learn this week is it's a little bit more complicated than that why well what did we learn on those lessons those earlier lessons about binary numbers all digital computers only know what two things on or off those correspond if it's off we call that a zero if the switch is on we call that a one so all digital computers know are zeros and ones now on the Arduino a zero is Zer volts and a one is 5 volts so in reality the only thing that we can output to a digital pin with the Arduino is a what a zero or a one that corresponds to a voltage of0 volts or 5 volts so that then begs the question if we can only output 0 volts or 5 volts how are we getting all of those magn magnificent in between values in the last lesson where we could we could get all the different brightnesses between full bright and full off very smoothly how did we do that well we do it through the magic of pulse width modulation which is often times described as pwm so that's what we're going to learn today I'm going to take a sip of my coffee I am going to get out of your way we're going to come over to our sketchpad view and we're going to look at this okay so let's draw some pictures and see if we can understand pulse width modulation so I think what I am going to do is I'm going to draw a coordinate axis here so I will start and I will draw my horizontal axis like that and now I will draw my vertical axis like this okay that looks pretty good we'll come in and just for completeness put some nice little arrows on this like this and this and what is our vertical axis our vertical axis is voltage so we'll call this volts and then our horizontal axis is time okay so now let's do the easy ones let's say that we wanted to apply to the LED a full 5 Vols how would we do that well the simplest thing would just be to do a digital right to that pin comma high and high would be five volts high would be on right it would be five volts that would be the easiest way or we could do an analog right to that pin of the full value which is what 255 255 is the number two raised to the e8th power minus one because we start counting at zero and so 255 would be an 8bit binary number if that makes sense okay so what do I want I want I want uh I want 5 volts so let's label this we will call this one Volt 2 volts 3 volts and then this is four volts and then this would be 5 Vols okay that looks pretty good so what would I do if I wanted five Vols well I could either do a digital right high or I could do an analog right with a value of 255 and what that would give me is that would give me let me try that again okay that would give me let me get my straight line here and I think that'll look better that would give me here at this 5 volts I would come like this boom that would be five volts well what if I wanted zero volts I could do a digital right low to the pin or I could do an analog right with the right value of zero and then that would come down here and that would give me zero okay so if I'm doing a five or a zero that is really straightforward but if I can only output a five or output a zero what if I wanted two and 1 12 volts what if I wanted half weight I can only apply a five or a zero but I want two and a half how could I make something look like two and a half well I could make a signal that averages to two and a half how would I do that I would turn the pin on to 5 volts for a certain period of time and then I would turn it off to zero so I would go 5 0 5 0 5 0 and the time at five would be the same at as the time at zero and that would average it would average to what it would average to two and a half now that is what the Arduino does when you do the analog right command now I should be a little bit more specific here and what I should say is one period that total on time plus the total of time is a fixed amount and that is 2 milliseconds okay now what we have to remember is one one mil one let me let me put it this way we just got to remember our units 1 second is equal to 1,000 milliseconds okay now I could also tell you that one millisecond is equal to 1,000 micr seconds okay and so what I can see is is that 1 millisecond is equal to 1 1000th of a second okay and then I could say 1 micro second is equal to 1 1000 of a millisecond and then what I could also say is one microsc would therefore be what one 1 millionth of a second okay does that make sense one one millionth of a second so you just got to remember there's a th000 milliseconds in a second there's a th microseconds in a milliseconds and there's a million microseconds in a second so we got to do that because we're going to kind of start working with some of these units here well what is that period I said that the total on time plus the total of time is the period and that period is always going to be the same when we're doing pulse with Mo modulation and that period is equal to 2 milliseconds okay so I need to create a horizontal axis down here and so what this is going to be we're going to call this 0.5 can you see that yeah 0.5 milliseconds and then this is 1 millisecond and then this is 1.5 milliseconds and then this is what 2 milliseconds okay so the whole period would come out here to 2 milliseconds so if I want to do 5 volts it stays on for 2 milliseconds and then it stays on for 2 milliseconds and then it stays on for 2 milliseconds okay if I want a zero volts what does it do it stays off for 2 milliseconds and then off for 2 milliseconds and then off for 2 milliseconds what if I want 2 and A2 volts okay what if I want 2 and A2 volts that's 1 2 3 4 5 that looks a a little bit not perfect but what I want is I want 2 and 1 12 volts and so that would be halfway between two and three and so what I want here is 2.5 volts now what I cannot do is I can't stay on Two and a Half Volts for two milliseconds why I can only be a 5 Volt or a zero well how could I get this halfway value the way that I could get this halfway value would be to stay on for on at 5 volts for one millisecond and then turn off and then I could stay off for one millisecond does that make sense so I'm on half the time I'm off half the time so that starts looking like what 2 and2 volts well what if I wanted one volt if I wanted one volt I would stay on 20% of the time okay this would be 1 2 3 4 I would stay on for one volt I would stay on for 20% of the time and so it would look something like this I would come over and then I would come down and then I would come over and I am staying on for 20% of the time and so that would start looking a lot like that would start looking a lot like what one volt okay so what you're doing is you're not creating a a onv signal you're creating a signal that averages to 1 volt in time or you're creating a signal that averages to 2 and2 volts over time now what are we writing we're writing values of okay we're writing values that are between 0 and 255 so what two let me try this here we're writing values between 0 and 255 well these are all going to be between 0 and 5 volts but what if I did let's say 126 right 126 would be 1/2 on and 1/2 off okay what if I did 62 62 would be about 1/4 of the time on and it would be 34s of the time off okay what if I did 25 that's 10% of 255 that would be 10% of 5 volts would be like 0.5 volts that would be a half of a volt that would be one10 of the time on and that would be 9/10 of the time off okay so what we're doing is we're just turning it on and off at a rate that allows us to average out to the value that we want but in the end we are only applying a z volts or a 5 volts okay hopefully that makes sense and I've kind of tried to draw it out here for you but let's go in and let's see if we can play around with this a little bit and what I have set up for you to kind of understand it more practically in the real world here what you can see is is that here is our trusty Arduino Uno R4 WiFi and then I have that pin 11 yeah it's that pin 11 that we were using last week I have it hooked up to the oscilloscope probe here to the oscilloscope probe you see that and then I have the ground wire hooked to the ground of the oscilloscope and then I have that going into channel one of the oscilloscope and what does the oscilloscope do it shows you a picture of the voltage form just like over here I kind of Drew you a picture what the oscilloscope does is it actually takes a snapshot of that voltage that you're applying and it makes a graph of it and then we can kind of look at it and figure out what is going on okay well if we're going to do that we need to set up a program so let's just come over here and I think I might just go to this view this time and let's write that program real quick that would apply an analog s signal a a simulated analog signal a pulse width modulation signal over to that most excellent pin 12 was it pin 12 no it wasn't pin 12 it was pin 11 okay so what do I do I'll say uh pwm pin what pin was that it was going to be pin 11 okay and then I need a bright value and I'm going to set that bright value I'm going to set it halfway to begin with so half of 255 would be a about 126 let's say and then I'm going to come down here if I'm going to use that pin 11 what do I need to do a pin mode of pin 11 no not pin 11 what pwm pin like that and then what is it going to be it is going to be an output like that okay and remember you've got to use a squiggly if you want the pwm you've got to use a Squig squiggly okay okay now when I come down here what do I want to do I want to do an analog right analog right and what do I want to write I want to write to pwm pen and then what do I want to write I want to write that brightness value okay and so we're trying to simulate 2 and 1 half volts we only have a 5vt signal so what are we going to want to do we are going to want to turn on and on off that 5V signal when it where it's on half the time and off half the time okay so now I think I'm going to come over here to this View and now what you're seeing hopefully if this thing can focus in that s really lost its focus let's see if we can get that to focus okay yeah that looks like it's focused in a little bit better now so you can see right now there is just zero volts nothing's being applied but now what we're going to do over here is we're going to run this program and then we're going to see if we can see we're going to see if we can see why does that not want to focus okay hopefully that'll hold the focus now what we're going to do is we're going to see if we can see the voltage when we're trying to simulate 2 and 1 12 volts which would be a bright value of 126 so we're going to run this thing and it is compiling ah did pwm pin uh what did I do pwm pin that looks good pwm pin that looks good why did it not like that ah I forgot ah did you guys catch that I got to give it I got to declare it it's an INT and it's an INT did that make sense okay let's try running it again and this time hopefully we won't see any of those ugly little orange messages okay it is compiled it is downloaded and boom look at that okay look at that let me adjust this a little bit okay let me adjust this a little bit give me just a second here okay look at that okay so what I want you to do is I want you to look right here we're going to call this time zero okay we're going to call this time zero and then we pop up to this voltage and we stay for two we stay for two divisions and we go down and we stay for two divisions so up two divisions down two divisions we're on for how long half the time so that 5V signal averages out to what it averages out to 2 and 1 12 volts now a couple of interesting things here is this down here says each division for this signal is 500 microseconds so we are on for 500 microseconds another 500 microseconds would be 1,000 micros 1,000 microseconds is 1 millisecond so we are on for 1 millisecond then we turn off and we stay off for 500 microseconds 500 microseconds makes a th000 microseconds 1,00 microseconds is 1 millisecond so we go on for a millisecond we go off for a millisecond then what do we do we repeat so we're going on and off like a th times a second okay we're going on and off a thousand times a second and that's so fast you're not even going to see it but it does create this simulated analog value now let's try something else let's say that we wanted a tenth of a volt well if we wanted a tenth of a volt we would need if we wanted a tenth of a volt that well no no I don't want to do a tenth of a volt let's do 0.5 volts 0.5 volts would be 10% of five that would be 10% of 255 that would be what about 26 so we're going to make that 26 and now what do we expect we expect it to be up for 10% of the time and down for 90% of the time while it's downloading it's shut off and boom look at that so now let's see for one cycle I've got 1 2 3 4 and it looks like I am a little less than I'm a little less than a half of a fourth and a halftimes a 4th would be an eighth I'm a little less than that so you can see indeed about 10% of that cycle I'm on and 90% of the cycle I'm off that is going to simulate a 0.5 volt signal what if we went really really low what if we went for one out of 255 on the brightness and let's do that and see what happens and boom you see it's just barely on it's barely on because it's one part out of 255 so that's less than 1% and so that would be like something like 0.05 volts there that would be a very very small voltage because it's off most of the time well what if we said uh let's see what would 90% .9 I'll use a calculator here I'll say 0.9 time 255 would be like 230 and so 230 would be 0.9 * 5 this would be like 4 and A5 volts if I put that at 230 and we're going to run it and then what are we going to see boom you see it is on for most of the time and then off and back on now what I want you to see is no matter how I'm changing that right voltage notice that it is always 1 2 3 4 4 * 500 microseconds is 2,000 microsc which is 2 milliseconds so from one cycle no matter what pwm value you write no matter what analog write value you write you always have one period of two milliseconds it's just how much of that period are you on and how much of that period are you off so we have learned something pretty cool today guys if you want to play around with one of these oscilloscopes you don't have to have it for this class you don't need it for the class but I did put a link down uh Below in Amazon in case you guys want to play around with an oscilloscope and be able to look at your signals I guess the other thing that I could say here is man this thing is really not wanting to focus do you see how it said 500 microseconds per division so we go 500 1,00 1,500 2,000 microseconds each division this way is worth each division this way is worth 500 micros seconds well how about in the vertical in the vertical each division is worth two volts I have two volts per Division and so I go one two no I'm sorry it says one volt per division one volt per division so I start at zero volts one volt two volt 3vt 4vt and this line would be 5 volt so you can see out of my Arduino I'm not actually getting a full 5 volts I am getting like 4.8 volts well a lot of times that's because your USB cable is probably coming in right at 5 volts but that USB cable might be a little bit low on the voltage at supplying and also a lot of times you'll have some protection diodes in there and you'll get like a few tents of a volt drop across those diodes and so you see I am not really getting exactly 5 volts looks like I am getting more like 4.8 or maybe 4.75 volts out of it okay guys so what did we learn today we learned that digital devices computers only know what zeros and ones for the Arduino that corresponds to 0 volts and 5 volts and how do we get the in between values we trick it by switching on and off but it doesn't know 2 and A2 volts it knows zero volts or it knows 5 volts and that is how it works so when we give an analog right command right the command down here is called analog right we are not really analog writing we are what what is that doing that that is giving me trouble we are not really get out of there okay this is just really getting kind of annoying we are not really analog writing what are we doing we're pwm writing and so you've got to understand you're not really putting an analog value out you're just simulating one okay guys I hope you are having as much fun taking these classes as I am making them I guess I really don't have a homework assignment for you I guess what your homework assignment would be leave a comment down below and sort of summarize what you learned in this class say it in your own words so that I can see that you really understand what I'm saying because this is a really important concept so you can kind of summarize in a comment down below what you've learned in this lesson and that will be your homework for this time next week what we're going to do is we've learned so far how to do digital rights we've learned how to do analog rights which are simulated pwm rights and now next week what we're going to do is we're going to learn how to start reading from the pens instead of just talking to them we're going to start reading from them we're going to start reading voltage values back from those pin okay so that'll be something a little bit new and that is what we're going to do next week okay guys want to thank you all especially always want to give a shout out to you guys who are helping me out over at patreon you guys that are supporting me on patreon you're keeping this great content coming you're the ones that are keeping me in the game good job thank you you guys can also help me out with the thumbs up it also helps with the old YouTube juice if you'll leave a comment down below if you haven't already subscribe to the channel when you do make sure you ring that Bell so that you get notifications When Future lessons drop and most importantly share this with other people because the world needs more people thinking like an engineer and fewer people sitting around watching silly cat videos Paul mcarter with toptechboy do.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 4,268
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: 4QUH5D_6XVc
Channel Id: undefined
Length: 28min 21sec (1701 seconds)
Published: Thu Apr 04 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.