Raspberry Pi Pico W LESSON 36: Control a Servo With MicroPython

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul McCarter with toptechboy.com and we're here today with episode number 36. in our incredible new tutorial Series where you are unleashing the power of your Raspberry Pi Pico W what I will need you to do is pour yourself a nice tall glass of ice cold coffee [Music] that would be straight up black coffee pour it 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 most excellent series of video lessons and in this class we will be using the sun founder Kepler kit for Raspberry Pi Pico W now most of you guys probably already have your gear but if you don't look down in the description there is a link over to Amazon you can hop on over there and pick this kid 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 the Shameless self-promotion let's jump in and talk about what I am going to teach you today we have a very exciting lesson and I'm going to show you how to control a Servo using your Raspberry Pi Pico W sound cool I hope it does had a lot of fun putting the prep work together for this class and hope you guys enjoy it so in order to do in order to do this what I will need you to do is I will get out of your way and I will need you to get out your kit and I need you to find the servo in the kit the servo is in a little bag and it has a little blue box in it and that box is labeled sg90 so this has the sg90 servo so I need you guys to get out your sg90 Servo and then also we need to get three wires out these are the male to male wires get those out of the kit and I suggest a red a black and an orange because the colors actually mean a little bit in this lesson okay let me come over and let's switch over to the switch over to the overhead view so you can see live here we have the Raspberry Pi Pico W all fired up and ready to go so in this little bag you're going to get your Servo to begin with okay you're going to get your little Servo here and then also you can see that there's two of these little horns okay I like the one that has the four arms on it and the of the two long arms one arm is a little longer than the other and that's a little makes it a little easier to see where you are pointing set this aside do not put it on the servo yet do not put it on the servo yet okay now what we need to do is hook up this Servo to the Raspberry Pi Pico W you can see that you have a brown a brown a red and an orange lead okay brown red orange and most servos have this same color scheme the thing to remember is brown is ground brown ground brown ground okay the red is the supply voltage that makes sense so now we've got the power and we've got the ground and then the orange wire is the control wire the orange wire is the control wire so I'm going to sit here and I am going to to put the orange wire into the socket that has the orange and then next is the red so I'll go red to red and then I did not have a brown wire so I will use black for ground because usually black is ground now we are going to come over here and what you can see that I've done is I've created a power rail I've created a power rail from pin one why pin one because pin one brings in 5 volts from the USB plug so you're passing through five volts and the range the operational range on these little servos is something like 4.8 to 6 and so we want to make sure that we're grabbing this one that should give us around 5 volts then the one two three the third over is the ground and I create a ground rail so I have a power Rail and I have a ground rail all right what we're going to use for control so the red the red wire provides the power to the little motor in here but how do we control it we control it through the orange wire and so I'm going to connect the orange wire to gpio 15 which is this upper left one when I can I like to use these simple Corner uh gpio pin so I don't have to do a lot of counting and so I'm going to come in with the control the orange and I'm going to come over here to the corner all right now I'm going to come over and the red is going to go to the power rail let me put it over here the red is going to go to the power Rail and the black which is ground is going to go to the ground Rail and now we should be all hooked up at this point we should be hooked up and we should be ready to start programming this thing now I should say something here that's very important I put the red wire into the power Rail and we're getting our power from the Raspberry Pi Pico W okay what you have to understand is this is a very a very small servo it draws very little current so it's completely safe to power it from the Raspberry Pi Pico W now if you decide you want to use a bigger Servo you should not power a bigger Servo off of the Raspberry Pi Pico w why because that Servo motor might draw enough power might draw enough current to either burn out your gpio PIN or burn out your Power Pin or burn out your entire Raspberry Pi Pico W so what I like to tell people is it's bad practice to use your single board computer as a power supply you can have a separate power supply so if you had a big Servo what would you do you would take the red wire and you would connect it to an external power supply that can provide the current needs of the larger Servo okay and then still you control it over here so even if you're powering it from a separate power supply you still control it from the Raspberry Pi Pico W now very important when you are doing any type of circuit like this all of your grounds need to be connected together so the servo ground is connected to the Raspberry Pi Pico W ground the ground of your external power supply should come in and connect to that ground rail too all your grounds need to be in common in a circuit all right so that's how you would do it externally I'm not going to demonstrate that because there's too many servos out there and if you guys want to use a big Servo just use an external power supply okay very good very good now we need to talk about how do we control it how do we control the position how do we control the position of the little motor well the motor will go from zero to 180 degrees from zero to 180 degrees but how do we actually control it well let's see if we can switch over here to our sketch pad and I'll see if I can kind of come in here and show how this thing works okay let me see if I can get a coordinate axis for us give me just a second and so we're going to come down like this okay and then we're going to come 5 10 15 20 something like this okay and now we will turn that off and so we have a coordinate axis now on the horizontal axis is going to be time and the vertical axis is going to be voltage now this is how these little servos work the position the angular position depends on the width of a pulse that I send to that control pin okay it's looking for the width in time of a pulse and how does this work well the way it works is the way it works is if you have and let me let me uh sorry let me label this time scale and so this is going to be this is going to be five milliseconds 10 milliseconds 15 milliseconds 20 milliseconds so this is time in milliseconds and let's just say this is 5 volts all right now if you come in and you give it a pulse of a half a millisecond 0.5 milliseconds it is going to go to the angular position zero okay if so what I know is that if I have a pulse width of 0.5 milliseconds it is going to go let me make sure I did that right yeah 0.5 milliseconds it's going to go to an angle of zero if I give it a pulse width of 2.5 milliseconds it is going to go to 180 degrees so it's going to go from here over to here and then it is going to scale linear linearly from 0 to 180 degrees as you give it a pulse width linearly from 0.5 milliseconds to 2.5 milliseconds does that make sense I hope it does now it is going to look for a pulse every it is going to look for a pulse every 20 milliseconds so whatever I do in this first 20 milliseconds I need to repeat that in the next 20 milliseconds so if I gave it a half millisecond pulse there after 20 milliseconds I need to give it another pull so I've got to keep giving it that same pulse to keep it in that position so 0.5 milliseconds will give you an angle of zero if we came out to two and a half milliseconds let me get it up I didn't do very good on that if I come out to two and a half milliseconds I'm going to get an angle of a hundred and eighty so now let's think how would we do that how would we create these pulses do you remember that earlier lesson we did I don't remember in this class what the lesson number was but it was using pulse width modulation using pwm the way we're going to do this is we're going to set up a pwm and we're going to be pulsing a constant width pulse every 20 milliseconds and then that is going to control the position of the servo now I didn't go out and search the internet to see if there was a library to do this and sometimes you guys come back and say oh I found a library I don't have to do the math well the whole purpose of this is to try to show you how to think through a problem like this because even though you might find a library for this problem you will not always be able to find a library and if you don't think through things right now on simple problems you won't be able to think through things when harder problems come along okay so this is our basic points of how this thing is going to work now this is what you have to see you have to see like how does how does the uh how does the Raspberry Pi Pico W set a pulse width I know I want to pulse with a 0.5 milliseconds okay but what value would I write in order to get a 0.5 millisecond pulse well the first thing we have to remember is we need to set up we need to set up our pwm to be at 20 milliseconds so we want the period we want the period here the period to be equal to 20 milliseconds okay now what frequency right we don't set a millisecond value when we set up pwm we set a frequency well what is frequency frequency is 1 over the period so the frequency that we're going to set is going to be 1 over 20 and then it it actually wants it in seconds so 20 milliseconds would be 20 times 1 e to the minus 3 or you could say that it's 1 over .02 like that 1 divided by 0.02 would be equal to a frequency of 50 hertz so when we set up the pwm we don't set it at 20 milliseconds because it doesn't want milliseconds we set it at 50 hertz 50 hertz gives you a period of 20 MS second so we got to remember that number there okay now how do we actually write or how do we actually set that pulse width well for the pwm statement we are going to need to know what is the right value okay the right value is equal to the percentage you know how long is this signal up what percentage of the period is this signal up and then we multiply that by the total number of bits which is 65 535. the the total number of numbers for a 16-bit number so you take the percentage that you want the pulse up the percentage of the period you want the pulse up and you multiply it by the number 16 65 535 because that's 2 to the 16 minus 1. that's the kind of range of a 16-bit number and so how would we figure out what right value we use for our pwm pin to get zero angles well the right value for a zero angle would be equal to the percentage which is going to be 0.5 milliseconds over the period which is 20 milliseconds so we're on for 0.5 milliseconds out of 20 milliseconds and then we multiply that by 65 [Music] 535 and that is equal to the most wonderful number 1638 okay rounding it to an integer now the right value for 180 would be equal to what well for a hundred and eighty we want 2.5 milliseconds so that would be 2.5 milliseconds divided by 20 times 65 535 535 and that is going to be equal to 81.91 so if I set up a pwm signal with the frequency of 50 hertz and I apply a duty value of 1638 I will be at the zero position similarly if I set up a duty cycle of 81.91 then I'll be at 180. okay so let's write these two numbers down let's write these two numbers down and then let's go over and see if we can start playing around with things on the ardu on the Raspberry Pi Pico w okay we've got it hooked up here let's come over here and let's go to our code view I have Bonnie up and running and now I'm just gonna I don't want to write this as a program because really you don't want to do it just writing these values we're going to have to get a better algorithm but just let's just see if the way we are thinking about this works so just down here in the command line I'm going to say import machine like that oops uh let's see maybe ah I need to connect here all right now I'm connected to the Raspberry Pi Pico w foreign to connect let me try again there we go okay it is up now what I want to do down here is I'm just going to write a few commands on the command line and see if this thing is going to work so we're going to say import machine okay and then I'm going to set up my servos so my Servo is going to be machine I'm going to make make this a pwm object machine Dot pwm and then where do I want to set up my pwm on machine dot pin and what pen are we on we are on gpio pin 15. so I'm creating a Servo object that is something I can write pwm to okay now I've got to tell it what frequency I want so Servo Dot free frequency and we said for a 20 millisecond pulse what we want is we want a frequency of 50 hertz like that okay that looks good now what I want to do is I want to do the servo dot Doty underscore unsigned 16. so this is going to set that right vowel that we just calculated and to be at zero degrees we wanted to write 16 38 like that now pay attention to your Servo let's see can I see this over here okay I want to pay attention to my Servo let me get out of your way a little further here I want to pay attention to my Servo and see if it moves or Jiggles a little when I enter this command so let's see sorry [Music] that was a big ugly bunch of mistakes wasn't it do T underscore unsigned 16 like that and let's see if I can bring this up a little okay there I think you can see it better so now what we're going to do is hopefully I didn't lose all my okay so now we're going to come here and we're going to get that command again and go and yeah it it uh it went that time now zero degrees traditionally is starting at the origin and then you point to the right horizontally and that is zero degrees so now that this is set to zero degrees I want to put this little horn on there where the this little horn on there where the long end is pointing along the x-axis which is going to be zero degrees if we follow standard uh mathematical convention and so you see the long one I am pointing to the right I'm going to point it exactly to the right and now that is zero degrees that is zero degrees now if we wanted to take it to 180 degrees what did we say we needed to go to we needed to go to 81.91 so now I'm going to go here and I'm going to go 81 91 and then I'm going to hold this where you can see it I'm going to hold it like this and let's see what happens if we write 81 91 like that and boom 180 degrees okay 180 degrees well you might think oh we'll do half of that half of 8 000 would take you to 90 but that's not going to work right because 0 right value was not zero angle it was 61.31 so you can't just do that but you know if we did it you'll see it's not going to be exactly 90 but let's just say somewhere around 4 000 somewhere around 4 000 will be somewhere around 90. [Music] all right so that is working so we're controlling the servo we understand pwm and this thing is kind of working but that's not the way we want to do it the way we want to do it is I don't want to think in terms of pwm I don't want to think in terms of duty cycle I don't want to think in terms of milliseconds pulse widths I want to think in terms of angle and how would we do this so that we can set up an equation that does all the thinking for us and then all we have to do is think in terms of angle so let's come back over here and let's think about what we just said what we just said was what we just said was is that if I want an angle of zero I need to apply what number I need to apply the number 1638. okay if I want an angle of 180 degrees what value should I write to the pwm pin I should write 81 91 and the angle is going to vary linearly between 0 and 180 as you linearly vary the right value from 1638 to 8191 what do I have here two points what do I want to draw between the two points a line in order to get that equation the equation of that line what is it that I need to First calculate the slope of the line well the slope how do we find slope slope we call it m and that's equal to Y2 minus y1 over X2 minus X1 okay what is Y2 Y2 is 81.91 so I have 81 91 minus y1 which is 1638 over X2 which is 180 minus X1 which is 0 and then I get a slope of what it comes out to 65 53 65 53 divided by 180. now why do I not change this into a decimal number because this is more readable to me if I go back and look at my code I see oh yeah that's the slope that was that minus that divided by the 180 I can say yeah that looks right that's right so it's a little easier for me to debug now what is the equation of the line well the point slope formula is y minus y1 is equal to M onto x minus X1 so what do we have y minus 16 38 is equal to 65 53 65 53 oh over 180 times x minus X1 X1 is 0 and so this just becomes X and now what do we have we move the 1638 over by adding 1638 to both sides of the equation so Y is equal to 65 53 divided by 180 okay times X Plus 1638 now that's in terms of Y and X but let's write it in terms of the variables that we're using our y value was the right value this is what we want to write to the pwm the right value which was our y the right value is going to be 65 53 over 180 times the angle because our x value was angle and then plus 16 38. so with this equation what are we able to do I can pick the angle that I want and then I calculate the right value that I'm going to send to pwm do you guys see that this is uh do you you couldn't see the 1638 but I said it pretty good so anyway that that wasn't too bad too badly covered okay so that is our equation write that down okay so now with the little math we should be able to make quick work on the coding side we should be able to make quick work on the coding site so let's come back over here and now let's actually write a program so what do I want to do I want to not be quite so tiny okay and this I can make my normal size and all right here what I'm going to do is I need to import machine like that and then my Servo pin was what my Servo pin was gpio pin 15. now I need to set up my Servo object as a pwm object so Servo is equal to machine Dot bwm of machine dot pin and which pin were we on Servo pin okay so I am making Servo pin a pwm pin and then I'm calling that object Servo does that make sense I hope it does now I've got to set the servo frequency and that was we said that that was going to be we just did that that was going to be 50 hertz right we want 20 milliseconds so we want a frequency of 50 hertz now what we're going to do is we're going to set up a file Loop pile true when it's true true is always true we are creating an infinite Loop and now I want to get from the user what angle do they want so I'm going to say angle and that is going to be equal to the input of [Music] what angle do you desire I think that sounds a lot better than what angle do you want just me and then this would enter it as a string and we really want it as an integer so we make that input an INT like that now I've got to calculate my right value and my right value is going to be equal to 65 53 divided by 180 from that equation times the angle that we just input plus 1638 like that now I need to send that angle I need to send that right value to the servo so I do that by setting the servo dot Duty underscore 16 and then I want to send it the right Val but now understand with all this math this might not come out to be an integer and this Duty underscore 16 wants an integer so I have to convert that to integer just to make sure that it is an integer like that now I'm going to also come up here and import time and then here I'm going to see say time dot sleep is equal to 0.5 for half a second now actually I don't have to do this because waiting for user input would be a delay but I just do this to remind you that you can't just sit there and Hammer that Servo like in a while loop where you keep writing it because then it'll Jitter this is a mechanical object you have to give it time to move you don't want to move it to a new position before it got to the old position so I like to put about 20 milliseconds or so delay in there and so 20 milliseconds would be like point 0.02 not necessary in this point because we're going to get a long delay in here when we're waiting for the user to put the new number in okay this is very exciting let's see what happens let's come back over here and let's see if I had a view let's see hmm was this the one that showed them both was this the one that showed them both hmm I thought I did not just show you both the let's see yeah what happened let's see I do not know how I lost that shot but let's just watch it and I'll tell you what value that I'm gonna have okay and so what I'm going to do is I'm going to come back over here and then I'm going to run the program just to see if it will run ah what is that line two oh import import all right now let's run it Servo pin is not defined Servo pin Servo Servo pin what on Earth did I do wrong Servo pin what line is that in line four Servo is equal to machine.pwm machine dot pin uppercase s lowercase s that looks so right when I was trying to look at that all right come in what angle do you desire well I'm going to put a 0 and then I'll come back over here where you can see it now what do we expect when I put in a zero we expect this to point exactly to the right and so I'm going to go like this [Music] sorry come back over here hopefully you guys saw this it should be a u unsigned 16 bit like that so now we're going to run it we're going to tell it we want a zero and we're going to come over here holding this thing I'm having to work with my left hand so this is no fun all right so we're going to go like that now what do we expect we expect it to go over to the right when I click enter type in zero hold on hold on okay what angle do you desire I'm going to put in a zero boom okay it went straight to the right this time I'm going to say I want 90. boom straight up this time I'm going to say 180 boom straight to the left 135 which is 45 to the left of 90. I can put in 45 I can put in 20 and this thing goes wherever I want it to go so how neat is that so with just a little bit of math with just a little bit of math we have been able to write a program that allows us to select the angle that we want the servo to go and then it will go to that Angle now there's a couple other things I should have said about the servo and that is something that always bugged me when I was teaching students and what students do is they like to fidget so they like to get this and spin it from the horn do not spin a Servo from the horn or from the output drive gear why the Servo is not a fidget toy there are many good fidget toys out there don't try to use your Servo as a fidget toy because it's very easy to strip those little gears that are in there and if you are playing with trying to drive this from the output you'll strip those gears and your Servo will be ruined and students used to sit in my class and go like this with their Servo and it would really it would really drive me crazy so what did you learn today your Servo is not a fidget toy leave a comment down below my Servo is not my fidget toy okay so that's one thing that you need to learn alright so this has been a pretty slick lesson if you ask me now what's your homework assignment is for next week what your homework assignment is for next week I want you to add to this circuit I want you to add a potentiometer and if your potentiometer is always all the way to the left I want your Servo to be at 180 degrees why because 180 is over there to the left and so if your Servo is turned if your potentiometer is turned all the way to the left the servo is at 180. if your potentiometer is all the way to the right then the servo is like this at the zero degree position and then I want you to be able to to control the position of the servo using the using the potentiometer sound pretty neat I hope it does guys I hope you're having as much fun taking these lessons as I am making them I want to give a shout out to you guys who are helping me out over at patreon YouTube isn't showing me a lot of love these days and you guys that are helping out on patreon or the guys that keep or keeping this channel going and keeping these videos coming so I appreciate what you guys are doing if you enjoyed the video give us a thumbs up if you haven't already subscribed to the channel when you do make sure that you ring that Bell and as always share this video with others because the world needs more people doing engineering and fewer people sitting around watching silly cat videos Paul McWhorter with toptechboy.com I will talk to you guys later laughs
Info
Channel: Paul McWhorter
Views: 6,750
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: vAnd0AC6u1Q
Channel Id: undefined
Length: 36min 1sec (2161 seconds)
Published: Tue Sep 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.