Using PWM on the RPi PICO microcontroller

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome making stuff with chris day-hut today we're going to take a look at the pwm signal pulse width modulation on the raspberry pi pico the pwm signal is the digital computer's way of simulating or mimicking an analog output now most microcontrollers and computers cannot generate an analog output meaning a variable voltage output our outputs are strictly on or off and at the system's voltage 3.3 volts in the raspberry pi pico world now before we get into the programming of it with micro python i'd like you to see what a pwm signal looks like on the oscilloscope as well as on an led over here on the oscilloscope you can see i've got a thousand hertz cycle going and this is what a pwm signal looks like if you're not familiar with oscilloscopes down here is zero volts up here is 3.3 volts according to the settings that i've set on my dials now what we're looking at is time from left to right voltage bottom to top so 0 volts 3.3 volts and then this is time going to the right and what we're seeing this would be one cycle from here from here to here would be one cycle and you'll see that half the time it's on and the other half is off and we refer to that as 50 percent duty cycle a hundred percent duty cycle would mean that it's always on and a zero percent duty cycle would mean that the signal is always off now for a comparison what we're going to do is take a look at an analog signal and we'll see that uh specifically on this the oscilloscope but i want you to understand uh how i've got this set up uh both on the raspberry pi uh as well as on the oscilloscope now the pico is playing no part in this other than providing voltage over to a trimmer potentiometer and this is set up in or configured as a voltage divider so as i rotate this uh we'll be changing the voltage from 0 volts to 3.3 volts so i'll be tuning that we'll see the result over on the oscilloscope now i've also got a schematic that i've created in fritzing so that you can clearly see what is actually shown here and here is my probe for the oscilloscope this is the ground clip for the oscilloscope on our ground wire and then this is 3.3 volts coming out of the pico so here we're going to measure the voltage and that's what you'll see on the oscilloscope okay this is analog voltage output and i'm rotating the potentiometer to see that signal change so there's zero volts i'll raise it up and that is now three volts so back down zero back up three volts that gives us infinite control between 0 and 3 volts that is the difference between what an analog signal looks like versus a digital signal in our case for today's discussion a pwm signal now for reference i always refer to uh the book published by raspberry pi foundation this one in particular is getting started with micro python on the raspberry pi pico if you don't have this i highly recommend you download it have it available it's a tremendous resource the other resources that i use when working with the raspberry pi pico is the raspberry pi pico data sheet the raspberry pi pico python software development kit and the actual chip the rp 2040 data sheet for the raspberry pi pico these are all excellent references they're free of charge and published by the raspberry pi foundation now the one that we're looking at specifically today we're talking about the pwm signal now on page 101 what we're looking at specifically for pwm is to determine which pins we can use and more importantly which pins we can use at the same time we cannot use pwm a number zero and pwa number zero over here at the same time we can use other combinations but we can't use those at the same time because they share the same resources if we did use them at the same time they would have to run at the same frequency and duty cycle so just be sure if you're using multiple pwm outputs to select from different groupings and not match pairs such as shown here before we get into the coding let's take a look at the circuit for our example program which is using an led it is also providing us with an area to hook up the oscilloscope now over here we're using gp number two which is pin number four the physical pin number four that connects to the long leg of the led long legs are always positive in many regards uh the flat side of the led connects to a resistor which comes back to the ground rail and then connects back to the raspberry pi pico at a ground uh pin now for the oscilloscope i've got the probe connected to the long leg of the led and that's the signal we'll actually be seeing and then the ground for the probe is connected to the same ground rail now up here on the actual uh breadboard in the real world we've got the same thing going on you see two wires here that were for our analog signal this is the wire off a gp number two coming over connects to the long leg of the led flat side of led is always goes towards negative so that's connected to ground via this 220 ohm resistor on the ground rail and then back into the raspberry pi pico on a ground pin and here is the ground connector for the oscilloscope and the probe for the oscilloscope now we can take a look at the program itself now we're going to load a couple of libraries import machine is the machine specific library for how micro python deals with the unique differences between each type of microcontroller such as a raspberry pi pico or an arduino etc utime is the library for your time based stuff and in this particular part of the program we're really not utilizing it but we will in further iterations now the u time is identical to time except for it's the micro python version of it now the most important line that's coming up is this one right here and of course the next one but pwm underscore sig equals machine dot pwm uh is extent is essentially creating an object within our program from the constructor of the pwm signal in the machine library uh and in this case we're setting it up on machine dot pin number two gp number two as i referenced in that previous bit of information out of the manual so that creates the object pw sig that we're going to use uh for our pulse width modulated signal on that output pin the next item is our frequency now we're going to set that frequency in this example as at a thousand and that's a good starting point if you set it too low say you set it to 10 or 100 and you've got a device such as a lamp or an led you might see flicker so you want to set it high enough so that you don't see that flicker if you're controlling an electrical motor such as a hobby servo or a conventional motor you don't want it too low because if it's flick as the led is flickering imagine a motor being started and stopping that rapidly it could be very punishing to the motor in all the circuitry in between furthermore you cannot go too high on that frequency because the mosfets the devices that are actually controlling the high powered voltage and amperage controlling the motor probably can't switch on and off all that fast so they really are dictating what your max frequency is for a motor controller so you will have to check the data on your motor controller to see what the max frequency allowable is if you don't know start at a thousand go up or down from there and monitor the behavior for uh results and and see make adjustments to make it better if you can the next element that is very critical is the duty cycle now the duty cycle is that on time of that cycle within our frequency setting so as we look at that on the oscilloscope our duty cycle is this upper time right here now the downtime here is the off this is the on so i in this particular example i'm running at a 50 percent duty cycle now one of the things getting into these numbers and conversions to duty cycle frequency hertz and all this fun stuff it can be a bit confusing so what i've done in this particular example is i've broken it out so that we can work in a term that we're all familiar with a percentage and in this case uh the maximum duty cycle we can have is 655 36 individual settings now because it's a computer it starts at zero and counts up to six fifty five thirty five now that would be a hundred percent on or a hundred percent duty cycle and therefore zero would be zero percent duty cycle now let's say i wanted to have five percent duty cycle just barely on well i would take that 655 35 and multiply it times .05 and that would give me 32.76 so i would set that as my duty cycle and likewise if i wanted to add 25 50 75 percent i would just do the multiplication this would show me the results and this is the percentage that we would get so in this case in our example i've set duty cycle at 655 35 times 0.5 or 50 percent and we've established that as the duty cycle and then we've set the duty cycle on the pwm signal object to that duty cycle value so we just take this value and put it in there and that controls how how much output we're going to get from this duty cycle or from this pwm signal so if everything was linear in an ideal world at 50 duty cycle the led in our example here would be at half brightness and if we had a motor hooked up we'd be running at half speed uh it's going to be pretty close to that if all the circuitry is good but don't expect absolute perfection in that regard now let's take a moment to look at how the duty cycle affects both the signal on the oscilloscope and the brightness of our led so the first thing we'll do let's reduce our signal down to five percent so we'll go ahead run that all right now we look at it on our oscilloscope my time machine and we'll see that we've got a very small amount of on time versus a lot of the off time if we look at our led we'll see that it's glowing rather dimly it's not very bright now it's kind of hard to say because you don't have anything to compare it to but we'll see here in successive uh values that we're going to set to so that was at five percent let's go to 25 percent if we look back over at our oscilloscope we'll see that our on time got wider and our leds getting brighter now let's go to 50 percent we're back to our original example that we started with 50 on 50 off and our leds gotten a little bit brighter again we'll go up to 75 percent now you can see in our signal on is almost all the way across almost 100 percent and our led got even brighter yet now we'll go for full on actually i'm going to stop a little bit short but i'll explain that in a minute i'm gonna go about 98 uh for my on time uh what i want to do is for you to be able to see on the oscilloscope how we've got our on time all the way across and we just drop down now at 100 the line would just be solid across the top over here at the led you can see again it's even more bright for comparison we'll stop this one set it back to five percent or even two percent run that and you can barely see the led glowing at all now i've changed my duty cycle back to 50 percent we can see that on the oscilloscope and we can see that on our led and you'll notice that the led is solidly lit um it looks perfectly fine no flicker or anything like that so now let's change our frequency and see how the behavior changes so we'll go from a thousand i'll stop it now we'll go to cut that in half to 500. and you'll notice over on the oscilloscope that the signals got wider in that same time frame so they're longer on and longer off because it's going at half the frequency but we still don't see any flicker on the led so we'll go stop this we'll stop this we'll change it down to let's go to 50 and see if we can see any flicker at all still no flicker on the led ah camera might be picking up a bit more and as you can see here on the oscilloscope our signal got so long that it's actually exceeding uh what i can show on the screen at this time base so i'm going to adjust it adjust it down so we can see the signal and i'm going to make it even smaller yet again all i'm doing is changing how much time is represented by each one of these lines on our graph finally we'll go down to 10 uh hurt so 10 times per second and we'll see if that creates the flicker now that's an obvious flicker and you would definitely not want to look at that for any period of time it'd just irritate your eyes now imagine a motor being turned on and off at that frequency it would be very damaging to all the power circuitry as well as the motor itself probably because it's constantly starting and stopping so that's the effects of changing our duty cycle too low going too high would be harder to demonstrate or show such that you couldn't see a visual representation of it so it's hard to demonstrate the ill effects of a high frequency but again if you don't know start at a thousand work your way up or down from there another example just so that you understand how fast this can actually go earlier we were running a base frequency of a thousand right now i've changed it to ten thousand led you wouldn't see any difference on that here we can see that it's running at 10 kilohertz or 10 000 times per second well let's see how fast the pwm signal can go that's at 10 thousand so i'm going to change it to a hundred thousand and we look at that on the scope we can see that it is just short of a hundred thousand here of course the led doesn't look any different um we'll try to go faster and see if we can get more frequency out of it that's pretty darn fast all right now at that point we are at nearly a half a million hertz or 5 498 000 kilohertz so that's really really fast and you're now starting to see some decay on the actual signal we get some ringing on the off and ringing on the high so the signal's starting to degrade but nonetheless it is outputting a pretty decent 500 000 hertz uh pwm signal and again with the led you will not see any difference there now the last thing i want to show you with pwm which adds a little pizzazz to it and it gives you that true variable speed control what we're going to do with this example program is ramp up the brightness of the led and then dim it back down slowly so that we can accelerate a motor and decelerate a motor if you see what i'm getting at uh and of course these ramp up and ramp down effects can be pretty neat for various lighting situations as well for as far as the programming goes it's quite straightforward uh we've got everything is pretty much the same at the top we're going to start with our duty cycle at zero then we're going to enter a loop to ramp up the brightness of the led so starting at zero means it's off so we'll get into a while loop where we're gonna increment the duty cycle up by one time one one uh bit every time through the loop we'll set the pwm signals duty cycle every time through the loop and then we'll just slow it down a little bit uh so that we can actually see it happening so once we get to full brightness then we'll turn around and we're gonna start again at 6 55 31 and then count down to zero so here we're minus equals one counting down setting our duty cycle and again sleeping so we'll see this the effect of this on uh first let's look at it on the led so we'll run it here you can see it ramping up and now dimming down very smooth uh no flicker no jitter so it's a nice signal up and down now let's look at it on the oscilloscope here you'll see it going full on and then now full down again full up 100 percent back down zero percent so that is how you would do fading and variable speed control uh with an electric motor of course some motors you don't need to accelerate up and decelerate down but bigger motors certainly that's a good good thing to do otherwise you can cause a number of problems with your drive circuitry i've used pwm signals on various microcontrollers for many years now and they've been very very beneficial to many of the projects i've worked on so i would recommend if you're a good tanker good maker you should learn about the pwm signal and experiment with it so that you can utilize it in your projects the other thing i'd like to mention if you like this video please subscribe to the channel that will help secure its existence in the future and if you like the video give me a thumbs up i'm chris day hut for making stuff with chris day-hut thanks for watching
Info
Channel: Making Stuff with Chris DeHut
Views: 4,054
Rating: undefined out of 5
Keywords: Raspberry Pi Pico, RP Pico, Arduino, Python, Micro Python, Toggle switch, Rocker Switch, Tactile switch, Button, Slide Switch, Matrix Keypad, Membrane Key pad, Keypad, Keyboard, LED, Fade LED, PWM, machine, Analog Matrix, Relay, Sonar, HC-SR04, Interface, Real world computing, Maker, Making, Chris DeHut
Id: wT9R1aUUttw
Channel Id: undefined
Length: 22min 57sec (1377 seconds)
Published: Fri Apr 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.