Raspberry Pi LESSON 9: Using GPIO Pins for PWM to Simulate Analog Output (Pulse Width Modulation)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter with top tech boy.com and we're here today with episode number nine in our incredible new tutorial series where you are teaching your raspberry pi who's boss what i'm going to need you to do is pour yourself a nice tall glass of iced 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 sun founder sun founder is actually sponsoring this video series of lessons and we will be using the most excellent sun founder ultimate raspberry pi kit now believe me your life in my life are going to be a whole lot easier if we are working on identical hardware hopefully you already have your gear put together if you don't have your gear yet look down in the description there is a link over to amazon where you can pick up this most excellent kit but enough this shameless advertising let's jump in and let's talk about what i am going to teach you today and today i think it's the day that things really get interesting now they have been interesting i think the last few lessons we're getting in there and we're really starting to bang on those old gpio pins we've learned how to send signals to the gpio pins we've learned how to read signals back from the gpio pins we've had some pretty cool demonstrations where we could like turn an led on and off based on using a push button switch that's been some pretty interesting stuff but today we're going to take it to the next level and what we're going to learn to do is we're going to learn how to put pwm signals to the gpio pins now what is gpio gpio is general purpose input output what is pwm it is pulse width modulation and what pulse width modulation does is it allows you to take an inherently digital system and it allows you to simulate analog voltages now the truth is digital systems only have two choices they are on or off they are high or low they are one or zero they are true or false there's no in between e there's no in between e and digital okay so when we were working with the led in the earlier lessons what could we do we could turn the led on or we could turn the led off now that is pretty exciting but i will admit sometimes you might want to like let's say have a light dimmer where you dim the led in increments between all the way off smoothly up to all the way on sometimes you want the in between values and the problem is you cannot get in between values just using something like the raspberry pi and just using something that is a digital signal so this is where pwm or pulse width modulation comes to the rescue because it is true still on or off but we're very clever in the way that we turn things on and off so that we can simulate analog signals now let's see here let me i'll have to do a little windows management here i always like to switch over and show you my studio view just for a second so you can kind of see how things are looking in the studio i'll probably go ahead and get out of your way here for a second and then what you can see is i've got something a little bit i've got something new to show you today i have got a digital oscilloscope and what that oscilloscope is going to be able to do it's going to show us the signals that we're actually getting off of our gp gpio pins when we set them up for pwm now if you have an oscilloscope by all means hook it up and use it okay if you don't have one first of all i know a lot of you would not be able to afford it's probably a couple hundred bucks but let me say that if you guys are really serious about electronics you need to start building your lab and building your lab is more than just buying the latest and greatest single board computer building your lab is starting to get equipment that you need like a good oscilloscope or like a good soldering iron and things like that you need to start putting together your equipment for the lab so if you have the coin you can go ahead and order one of these this is i've looked at a lot of oscilloscopes this one was about 200 and it's a very full featured oscilloscope in a good hobby model a real oscilloscope would cost anywhere from two thousand to twenty thousand dollars but for the home bench this is a really good value at 200 bucks got a link down below to the one that i'm using okay a lot of you guys don't have that kind of coin and you're just going to follow along with the raspberry pi that's okay because you can completely do everything i'm doing you're not going to miss out by not having an oscilloscope it's just because you can look on my oscilloscope and you can see what's happening but you're going to actually hook up your raspberry pi you're going to do everything that i'm going to do you're just going to do it without without seeing the results in front of you as far as the oscilloscope oscilloscope goes but enough of this introductory banter just way too much talking and not enough coding and so let's jump in and let's see how pwm works what i have here is i have set up a little graph and what we're going to do is we're going to look at voltage voltage over time all right now what we can do is we can come in and let's say that today i am working on the most excellent it looks like gpio pin 37 i could come in and i could turn gpio pin 37 on and what would it do it would come up here to 3.3 volts that's rather odd okay come up here to 3.3 volts and come across and we could turn it on okay we could turn on that gpio pin and get 3.3 volts or we could come over here and then we could turn it off and the voltage would go to zero so we can sit and we can turn the gpio pin on or we could turn the gpio pin off does that make sense okay but now what i really want let's say i wanted the led half brightness well instead of 3.3 volts what i would really want would be about 1.5 volts you see this is what i want here i want about 1.5 volts for half brightness the problem is my only choice is my only choice is on or off and so i can't turn it half on okay i can't turn it half on but i can turn it full on for half the time and believe it or not full on for half the time is almost the same as half on so what would that look like that would look like i would turn it full on and then i would turn it i would turn it full on and then i would turn it full off and then i would turn it full on and then i would turn it full off and then i would turn it full on and so forth you're going to get tired of me repeating myself but i hopefully hopefully that will help you understand it and then if you look at it what is the average of the signal that i just put on there the average is half and that average voltage it kind of acts the same way as if you had just set it there all right and so that's what we're going to do this works for a lot of different things that'll work for led brightness it'll work for motor speed and motor control there's a lot of things that this pwm will actually work for and so that is what our task today is our task is to figure out how to do this pwm okay also i guess i should say you don't just have to go you know like at half what you could do is you could come on for say 10 of the time and then off for 90 and then on for 10 and off you see what you're doing is you are modulating the pulse width and you could have it on for like one percent of the time 50 of the time 99 of the time if you turn it on at 100 of the time that would just then just be a dc or off at zero but you see you could go anywhere in between those and then you can kind of get something to start acting like analog out and so let's see if we can get that going we're gonna have to come over here i'm gonna have to figure out which window would be the best window for you to see let me uh do a little switching here one of the problems i have is i have more uh high resolution cameras in my system i have more high resolution cameras in my system then i have input ports on my computer so i'm actually have to switch cameras between ports and that makes things even a little bit more tricky to try to try to keep the studio running well for you okay so let's come over and let's see if i can start with an overhead view let me see if i just got a good overhead view so you can kind of see what we are dealing with here okay so i've got the raspberry pi and i have a simple led circuit hooked up and then i have an oscilloscope up here well what you need to do is you need to go ahead and hook that led circuit up and let me see if i can find it yeah this looks like this looks like the circuit that i have here in the schematic up here today we will be using the most excellent pin 37 that is on the left column and that is the second from the bottom gpio pin that's pin 37 and then we're using pin 39 as the ground and so we could come over here real quick and you can look at the pin out and you can see 39 is the lower left pin that is the ground 37 is one up from that and that is the gpio pin that we will be using okay and so what you're going to do is you are going to then come over and hook up an led so we go from pin 37 to the long leg of the led and then the short leg of the led goes through a 330 ohm resistor and then the right leg of that 330 ohm resistor brings it on home to pin 39 the ground okay you guys probably already have something like this hooked up or if you don't it will just take you a second to hook it up so that is our that is our setup then what i'm going to do is i'm going to start sending pwm signals to the led through pin 37 on the raspberry pi that's what you guys are going to do but then what you're going to do is you're also going to see what what you're going to see is you're going to see the effect of the pwm on the led but what i'm going to do is i'm going to get the probe from the oscilloscope and i'm going to grab that pin 37 by hooking to the long leg of the led and then i am going to grab the ground by hooking right here and now i have that and now i need to do a little bit of management here i think let me look that looks like 200 millivolts one volt per division actually that looks pretty good as far as trying to get it to do get it to do what i want okay so i've got the oscilloscope and the led hooked up hopefully hopefully you guys now have your circuit set up your led set up now what we're going to do is we're going to come over here to a new view where we are looking at lots of different things at the same time and let's see if that looks good yeah i think that looks good so you're going to be able to see my code and you're going to be able to see the oscilloscope and you're going to be able to see the led pretty slick pretty slick so let me come over here and then i think what we'll do is we'll go ahead and move this to the top by doing a quick clear okay and now we're going to go ahead and instead of writing a program what we are going to be doing is we are going to be working in the command line okay the the python shell and so to get there i type python right no no that puts us into python 2.7 and i think i did that by accident in lesson number five so i'm sorry about that don't use python 2.7 we're going to quit out of that we're going to clear again and it's what that we're working on we are working on the most excellent python 3 which is python 3.7 and we are ready to start banging on those gpio pins so what's the first thing we're going to need to do i'm going to need to import who import our old friend rp little i dot gpgpio and i'm going to import that as what gpio you can import it as kitty litter box if you want to i think it would be more meaningful and easier to write every time if we imported it as gpio that is a great coffee we got there okay boom we got the import that always makes us happy now we need to do the set mode and that tells the raspberry pi what numbering system we are going to do it will be gpio because that's what we imported it as and we are going to use the board configuration what's nice about the board configuration if you want to use pin 37 you call it pin 37 kind of makes life easy doesn't it okay we got that one now what we need to do is gpio dot set up what are we going to set up pin 37 the most excellent pin 37 and then that's going to be a gpio dot out so so far everything is kind of very familiar isn't it everything is very familiar all right so let's go boom that looks good all right so now let's just kind of check things out make sure that everything is in proper order atoms in the universe are in the proper place and everything so what i'm going to do is i'm going to go ahead and just do a write a true or write a 1 and we're going to watch the led come on and then what we should see up here we should see this line come up 1 2 3.3 volts i've got one volt per division i don't know you might not be able to see the divisions real well but it'll be one two three and then a little bit more than three would be 3.3 volts this is not pwm this is just good old-fashioned dc but we should see it on the oscilloscope now also this oscilloscope sometimes like auto modes and things like that so i might have to go in and tweak the oscilloscope to get the signal on there but uh we should certainly see the led come on so how would i do that you guys should know this already we are going to do a uh we are going to do a gpio dot output we're going to output to what the most excellent pin 39 and it is going to be a what let's do a true we're going to put a true like that okay everybody hold the breath boom we got the led to come on but we did not get we did not get it to show on the oscilloscope so let's see here it's normal channel one one volt per division okay uh let's see it's not triggering there it's not seeing it okay there it is i need to do an auto and now i do a single and now i do a normal okay so why did it not see it the trigger level was not set correctly and so it wasn't actually seeing it okay so let's try it again now now what would you want me to do okay you would want me to say false like that and uh man that oscilloscope didn't go down did it okay let's say okay let's try the auto mode there the triggering the problem is an oscilloscope wants to see an ac signal so sometimes it doesn't see it real good when we are just doing uh single transitions like that so let's try this again this time we'll go true and boom you see it go up and then we see the light come on the led come on and now we're going to do false and then boom we see it we saw that transition we saw it come down okay so the oscilloscope is working now the led is working universe is in proper order now i have to warn you while we're fooling around with getting things set up the oscilloscope might decide to kind of recalibrate itself or something so the oscilloscope might be something we're going to have to keep tweaking but at least you can see that voltage signal on the oscilloscope now how do we start doing pwm we have to create a pwm object for us to interact with and so i am going to call my pwm object my pwm you can call it my lamborghini if you want to but my pwm is a little bit more descriptive and that is going to be equal to what that is going to be equal to gpio that's what we named the library when we brought it in dot what pwm okay and now what pin do we want to be the pwm pin we want pin 37 to be our pwn pin and now what we have to tell it is what frequency okay we have to tell it what frequency for one cycle of our pwm for our pwm signal one cycle needs to happen at what frequency that's saying how many times per second okay now that's totally different than what percentage of that cycle is on and what percentage is off that is a different parameter that is duty cycle duty cycle is what fraction of the period it's on and what fraction it is off that is duty cycle we're not talking about that for whatever duty cycle we're using how many of those periods how many of those chunks do we want a second well a pretty good number is a hundred so we're gonna have a hundred of these blocks a hundred of these cycles per second and then and then inside of each cycle we'll define what the duty cycle is i hope that makes sense so we're going to use a frequency of a hundred all right and so now when we set that look at that it took it and so now it is ready to go and so now i'm going to say my pwm and then i'm going to say start the pwm and now i tell it what duty cycle to use i tell it what duty cycle to use well if we wanted something that was half brightness we would set it at what at a 50 duty cycle so that should be on half the time and that should be off half the time and then it is 100 hertz so let's try that and shazam look at that boom so what do we see here we see a half bright led and then over here on the oscilloscope we see that the the uh pulse is on for half the time and it is off for half the time now that is duty cycle we see the duty cycle we see the impact on the led now what we need to do is we need to kind of check and see if the frequency is right and so what i'm going to do is i'm going to look at this and you probably can't see it but right here is where we start measuring and right here there's a faint background grid that is one division and so our cycle is how long our cycle one cycle is one division well what do we need to know we need to know how long is the division and the division if you look down here if you could see it it says 10 milliseconds per division so we get one cycle in 10 milliseconds well if we get one cycle in 10 milliseconds how many cycles could we get in one second well it would be a hundred which would be a hundred hertz okay so what are we verified here we have verified that we have the right frequency we have verified that we have the right duty cycle and we have verified that we have an led at half brightness how cool is that man i love doing this and i love operating here in the shell i love operating in the shell so it it's like i'm right there down in those gpio pins and they are singing and dancing the tune that we are sending them so that is pretty exciting okay i think maybe you guys might see this better if i kind of turn the led to the side sometimes it's really hard to see the led what it's doing very well well that did not work very well it's just these sony cameras these these cameras do not like these cameras do not like the led very much also this screen on on the oscilloscope is actually very bright it's actually very bright and it's very bright colors but for some reason the camera doesn't want to pick up those colors and so i'm not sure i'm not sure exactly what's going on there but nonetheless i digress so okay let's go down here now we started it okay we started it but you know what we can do we can also stop it so let's see if that will stop yeah didn't stop yeah it did okay there it is just took a second to stop okay so it stopped now we could start it again and let's just say that we did 10 like that okay 10 percent i did start again stop [Music] okay start at 10 let's see okay boom there it is now what i need to do is for that i need to make you see it a little better so i'm going to spread that out and then what you can clearly see there is you can clearly see let me make sure i got that right okay now you can clearly see there that that signal is on for about 10 percent of the time and then when you look at the led here you can see that the led is very very dim okay so now you don't want to be starting and stopping so i'm going to give you a new command and what that new command is you can simply come in here and change and change with an uppercase c change uppercase d duty cycle okay so this is that kind of bumpy font change duty cycle like that and let's see if we can change it to let's say let's change it to 75 so we're going to be watching the led and we're going to be watching the scope and boom look at that very bright signal on the led and you can see now that it's on 75 of the time and so that is pretty cool and i'm still wanting to get that oscilloscope working a little bit better okay that looks uh that looks pretty good i think what i'm going to do is i'm going to expand that out a little bit like that okay so now you can see one duty cycle really clearly and you can see very clearly you can see very clearly that it is i need to this thing is behaving a little bit like i got a little bit of glitchiness in it okay that looks a little that looks a little better but uh what you can clearly see is in one cycle which is a hundred hertz which is the cycle itself lasts 10 milliseconds okay 10 milliseconds 1 divided by 100 is 10 milliseconds so if you want the period that is the amount of time from the start to the end the period of your cycle is 1 divided by the frequency so if i have a frequency of 100 the period is 1 divided by 100 which is 0.001 which is which is 10 milliseconds and so from here to here is 10 milliseconds and it is up for seven and a half milliseconds and it is down for two and a half milliseconds and i know i'm just kind of sitting here saying the same thing over and over but i really really want you guys to understand this so let's go in and let's just take it on up to let's say 95 okay 95 like that and clearly you can see that it's on for most of the time and i don't think it'll actually work for a hundred i think that it'll go to like 99 but i don't think it wants to do a hundred yeah it doesn't it you know if you want a hundred just do a dc right on there don't do a pwm so we could go like 99 and there that that would be the that would be the 99 or we could come up and we could do a one change duty cycle to one okay and there you can see kind of it doing the doing the other thing what do you also see there you see that the led is very very you can see the led is very very dim also i don't know if you can really do a duty cycle of zero probably not yeah it doesn't like the zero and it doesn't like the the hundred okay so let's go back to let's make it a do the cycle of five and see yeah there it goes so that looks uh that looks happy okay now the other thing that you can do is let me make it let me make it 50 okay let's make it 50 like that okay that looks very clearly 50. now you can also you can also in the middle of this you can change frequency okay and what frequency you can change frequency if you can spell frequency you can change it but you've got to spell it correctly if you want it to work all right you can change frequency now right now we're at 100 hertz if i change it to 200 hertz should that make the pulses spread out or should that make them compress think about it if i go from 100 to 200 should the pulses be twice as small or should they be twice as big okay they should be smaller okay or what now that that was an unexpected result so a higher frequency this thing auto scaled on me is what it did that was not a good demonstration okay so if i go twice as fast 200 hertz then each cycle gets smaller but then the darn oscilloscope re rescaled on me so let's go back now to a hundred and hopefully at a hundred hertz things are happening slower so those pulses should spread out if it doesn't auto scale on me on the oscilloscope yeah there it is now let's try going to 200 and see if we can do it without it auto scaling okay now this is should compress them together okay yeah that's uh that's kind of working now that is looking pretty good okay but now now that we see that we can do that i want you to watch the led okay and i'm gonna go back to a hundred okay in there did you see any change in the led no you didn't what if i go to a thousand do you see any difference in the led no you don't why don't you because you're not changing the relative time that it is on and off it's on half the time it's off half the time it's just you're doing that a lot quicker at a thousand than you are at a hundred okay you're doing it a lot faster at a thousand than you are at a hundred but your i can't perceive that at all your i can't perceive that at all and so let's go back and let's go back to a hundred let's go back to a hundred on the frequency and remember on all this on all this our duty cycle is fifty percent okay so a hundred now let's just see what if i went to 10 what might happen if i'm going 10 times a second what is going to happen at 10 times a second is you're going slow enough that your eye can actually perceive that on off on off on off okay at 10 hertz you can see the on off on off okay how about if we went to 20 hertz so you wouldn't want to do 10 hertz because then you're actually seeing it 100 hertz is a pretty good number to use but what i'm trying to show you is the frequency that you use of your pwm really doesn't matter very much so at 20 i'm still seeing it blink at 30 still seeing it blink yeah i think you guys can see it blink too let's try 40 yeah i'm still seeing a distinct blink okay 50. [Music] i'm barely seeing it blink at 50 and then at 60 [Music] no blink at all it's 60 hertz there's no blink at all so it's still blinking but somewhere between 50 and 60 hertz the little neurons in your eye can't fire that fast and it just says okay now i'm not seeing blinking i'm seeing it half on now what's the interesting thing about 60 hertz you got somebody tell me what is the interesting thing about 60 hertz your wall electricity your ac wall electricity is what 60 hertz why is it 60 hertz so that when you are sitting there reading back in the old days you know with the old-school lights they don't flicker that if they made the ac 60 hertz you don't perceive a flicker in the lights if you had the frequency lower than that you would start perceiving a flicker in your light bulbs anyway so i think part of the reason that uh you have 60 hertz electricity is so that you don't perceive flicker and i think also like the old refresh rates at t on televisions was about this as well so it looked like you had a still image and you couldn't and you didn't see the the the screen refreshing itself so just a little bit of trivia there okay so now that is some pretty uh that is some pretty slick stuff i'm trying to think of what else i could uh i could show here uh i guess what we we could do is we could come in and it's always good when we're done to say my pwm dot stop like that okay and that should turn it off and i think it turned it off but then the oscilloscope grabbed that last uh the oscilloscope grabbed that last signal yeah now it's just reading not just reading nothing and then always we need to gpio dot clean up like that so that we leave the pins clean for the next guy that might that might be coming in okay guys this is just really some pretty cool stuff if you ask me i think this is just some really cool stuff so this is like a really really huge step forward for us so what can we do now we can send signals to the gpio pins we can send standard on off signals high low signals to the gpio pins we can read from a gpio pin whether it's on or off and now we can send a simulated analog signal via pwm or pulse width modulation to the led okay or to the gpio pin and we could control an led or we could control a motor or we could control a thermonuclear reaction the point is once you can talk to the gpio pins it's like that is your pathway to the world you can read from anything and you can write to anything once you know how to work with those gpio pins okay time for homework time for homework what your homework is what your homework is is to come back to this circuit okay and what i want you to do is from your most excellent sun founder sun pounder ultimate raspberry pi kit i want you to get two of those little push buttons okay i want you to get two of those little push buttons and with those two little push buttons i want you to be able to control the brightness to control the brightness of the led in let's say about eight steps and so you wanna go from as dim as possible you want to go from about as dim as possible to as bright as possible and that might be like one percent to 99 and i want it to increment like in eight steps so when you press the button uh when you press the button the first time it comes on if you press that button again it goes up in brightness up in brightness up in brightness so you've got one button that is making things brighter and brighter and brighter and then you've got a second button and that button is your dim button so if you press one button every time you press it the led gets brighter up to full brightness and then the second button when you press it it begins to get dimmer but i don't want you to just hold it and while you're holding it dim and while you're holding it it brighten i want it each press of the button increments the brightness by one notch and then each press of the other button dims it but i don't want you like if you think from one to a hundred like if you think of one percent to 99 i don't want you to have to sit and poke the button 99 times to get it to go up i want you know seven eight nine increments to go from as dim as possible to as full as possible now you might think about uh you might think about actually how to have it turn off at the lower one and how to have it turn to a hundred percent at the higher one but you're going to have to think about that a little bit because i'm not sure that you can do a pwm of zero and that would that would actually work but you can play around with it that would be extra credit if you actually go from off to 100 if you can figure out a way to do that but two buttons to have a brighter button and a dimmer button how does that sound guys does that sound like a pretty good uh does that sound like a pretty good homework assignment i think it is and this to me is just really getting exciting because while an led is not that exciting what you got to see is is that the point is from our python shell we are controlling those gpio pins and what that means is if we can control the gpio pins we can control anything that we hook to those gpio pins and so this is actually very very powerful what we are doing here and where we are going with this yes as the lessons progress we are going to start using more and more exciting components from the kit but you got to learn the fundamentals with simple things like leds that way if you mess up if you hook something up wrong you burn out an led you don't burn out an expensive component so we're going to learn the basics on simple things like the led and then we're going to start reading from some really exciting sensors and we're going to start writing to some really exciting output devices and those that's where we're going hey i want to give a shout out to you guys who are helping me out over at patreon it is your support and your encouragement that keeps this great content coming you guys that are not helping out yet look down in the description link over to my patreon account think about hopping on over there and hooking a brother up also when you do this homework if you are successful leave a comment down below i am legend and if you're not able to do it leave a comment down below i fold it up like a cheap walmart lawn chair if you did get it working do a quick video of your homework solution post it to youtube in your description link back to this video and then on my video down in the comments leave a link to your youtube homework solution i go and i look at every single homework solution that you guys submit i want to see if you're learning i want to see if you're doing it right i want to see if maybe you're doing something better than what what i did so make sure that you post your homework solutions also in the comments look hop over and look at what other people are doing for their homework solutions you guys start interacting and getting a little bit of a community going here if you like the video give it a thumbs up uh if you haven't already subscribe to the channel when you do make sure you ring that bell so you will get notified when future lessons are released and then as always share this with your friends on social media because the world needs more coders and engineers and fewer people sitting around watching silly cat videos paul mcquarter from toptechboy.com i will see you guys later [Music]
Info
Channel: Paul McWhorter
Views: 19,273
Rating: undefined out of 5
Keywords:
Id: yJdN6LmGqvc
Channel Id: undefined
Length: 39min 43sec (2383 seconds)
Published: Thu May 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.