Raspberry Pi Pico Servo Motors via PWM

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello folks my name is tim and welcome back to my channel so in today's video we're going to be looking at how to control a micro servo motor using a hardware pwm signal generated by the raspberry pi pico and yes as some of you have commented in my previous videos this is in fact a picot and not a pico never mind hey slightly embarrassing but i'll just have to get it right going forward so without further ado let's introduce the star of our show this is a little micro servo motor it is in fact an sg90 now much like the stepper motor we looked at previously this is a very common component and quite inexpensive if you've got one of those bulk project kits it's probably got at least one of these things in it and if you have a random servo it's probably an sg90 so given how common this thing is it's going to be very useful to understand how it works and how we can drive it so what is a servo motor well the stepper motor that we looked at previously this guy here this moves using relative positioning that is to say that each movement you make with it is stacked on top of all of the previous movements you've made so if i'm at the zero degree position with this stepper motor and i say move by 10 degrees then i'm going to end up at the 10 degree position but if i'm at the 90 degree position and i say move by 10 degrees i'm going to end up at the 100 degree position so you can see where you end up as the result of a movement depends on where you started that movement it's a relative position now the servo on the other hand this guy this uses absolute positioning so what that means is if i'm at 0 degrees and i say move to the 10 degree position that's moved to 10 degrees rather than by 10 degrees then the server will move me to 10 degrees but if i'm at the 90 degree position and i say move to 10 degrees the servo will actually move 80 degrees in the other direction to put me at the 10 degree position that i asked for so with a servo you always tell it where you want it to be and you let it work out how it has to move in order to put itself at that position so that's a very useful feature of a servo and it's the fundamental difference between a servo motor and something like this stepper motor so in order to make that happen a servo motor has to understand where it is and it needs to know where its zero position is and where its maximum movement position is so with this little sg90 it has a zero degrees minimum movement position and a maximum movement position of only 180 degrees so that means it can swing between zero degrees and 180 degrees it can do a half rotation so you can't tell it to spin around four times it can't do that but you can tell it to move anywhere you like between the zero and 180 degree position so that's still quite useful and it should be noted that that movement arc limitation isn't a specific feature of servo motors it's just this sg90 you can get servos and indeed they look a lot like stepper motors that can perform multiple rotations and you can use them like a regular motor but they're quite expensive and quite rare and i don't have one to play with today all i've got is this little sg90 so that's what we're going to look at right enough talking let's get this thing wired up and moving [Music] okay so that's all there is to wiring it up it's uh it's very very simple the servo motor itself just has three connecting pins what we have is a a ground pin this is a common ground we have a voltage in and we have a single signal line so on the sg90 they're color coded like this the brown wire is the common ground the red wire that's the one in the middle that's where you put your voltage in and an sg90 like this runs on around about five volts and the orange wire this final wire that's the signal wire and that's how you control the thing so this is basically about as easy to wire up as it can be it's just three wires in total and one single gpio pin so here i've got this connected to gpio pin zero and this other connector here that's just the common ground back to the uh the ground on my power supply here so there we are that's how you wire it up let's run some software and make it move all right well that's definitely got it moving so here you can see the servo is rapidly swinging backwards and forwards between the zero degrees position and the 180 degrees maximum movement position backwards and forwards backwards and forwards now that little white piece of plastic that it's waving around so frantically there that's called a servo horn and when you buy a servo it'll come with a selection of little plastic attachments different shaped horns that you can connect to it and use to interface with various projects and you can of course put a gear wheel or something on there if that's the appropriate thing to use for your application so there's actually a lot of torque in that movement the sg90 has a torque rating of 1.5 kilograms at one centimeter so what that means is at a distance of one centimeter from the central shaft the sg90 can move loads up to 1.5 kilograms so that's actually very impressive and it's because of this high torque and precise movement that servos are excellent candidates for robotics projects but how do we actually tell this servo to move around how are we controlling this thing well a servo like this uses something called pwm or pulse width modulation and that's really useful because the raspberry pi pico here is an excellent board when it comes to pwm applications it actually has eight pwm generators built into the hardware and each one of those generators has two outputs for a total of 16 signals and even better than that you can map any one of those 16 signals to any gpio pin so that's very very useful we have lots of signals and lots of flexibility and i'm sure it won't be long before we see some of those nice servo hexapod robot chassis being controlled with a raspberry pi pico as the brain in fact i think that will make for a very interesting video so how do we go about creating a pwm control signal for use with our servo well it's actually quite easy and here we are on my raspberry pi 400 desktop and i've loaded up the source code to the little demo we've been looking at so here on line 9 we create a pwm object and we bind it to a particular gpio pin in this case we're using pin zero so that means our pwm signal will be emitted on gpio pin zero the next thing we do is to set the frequency of that signal and as the name might suggest a pwm signal is a pulsing signal so we need to tell it how fast we'd like that pulse to be generated so here we've selected a pulse of 50 that means 50 times per second or 50 hertz and that matches the control signal that the servo expects so that means we're going to be toggling the selected gpio pin between the high and low states 50 times a second so the second parameter that we care about and actually the more important parameter in this case is the duty cycle so what that means is if we were to look at our pwm signal that we're generating and we zoomed in so that we were looking at just 1 50th of a second of it one single slice of that frequency we would see the pin high for a certain amount of that time and the remainder of the time it would be low the duty cycle specifies what percentage of an individual pulse the pin remains high for and the remaining time it will be no so if you have a 50 duty cycle then you have 50 percent of the pulse in the high state and 50 percent in the low if you have an 80 duty cycle then you have eighty percent in the high and the remaining thirty percent is low and you can specify whatever duty cycle you want of course you can even have a zero percent duty cycle or a one hundred percent duty cycle and that would mean your pin would be either fully on or fully off so for the servo we have here the data sheet says that a 5 duty cycle corresponds to these zero degrees position and the 10 duty cycle corresponds to the 180 degree maximum movement position it's not quite as clear-cut as that you actually have to calculate those numbers but that's what it works out as now on the raspberry pi pico the micro python environment actually uses an integer to represent the duty cycle so you have to sort of calculate what a 50 or whatever duty cycle will look like the maximum value of the duty cycle parameter is 65 025 so if you want a duty cycle of 5 of the 65 025 maximum you would end up with a number 3251.25 so that there is five percent uh duty cycle 10 duty cycle unsurprisingly it's twice the 5 ignoring the 0.25 or 0.5 because this is an integer and that should give us let's say 3252 and 6500 but it didn't quite work out that way when i plugged those numbers in i found that the servo wasn't moving between the absolute minimum and maximum ranges that it was supposed to so really what i had to go and do was to tune it in and i just kept adjusting those numbers and watching the servo until i found some numbers that matched the maximum and minimum range so the moral of this story seems to be the data sheet is just a guide and you may have to do some fiddling around to find out what your actual servo duty cycles look like but once you've found what those numbers actually are all you need to do is to set the duty cycle of your pwm signal and your servo should rotate to the desired orientation so the rest of this program down here this little loop all it's doing is it's starting at the minimum duty cycle the zero degrees position and it's working its way up towards the maximum the 180 degree position and once it gets there it starts working its way back down again just traveling smoothly from the minimum position all the way to the maximum and then once it gets there it travels back again from the maximum all the way back to the minimum and loops around so there you are really that's all there is to controlling one of these servos once you've worked out what your minimum maximum movement ranges are and the minimum and maximum duty cycles that correspond to those movement positions you can then just set the value to anywhere in that range and the motor will turn accordingly as usual i will upload this code to my github page and if anybody's interested they're very welcome to go check it out i hope this video has been helpful and thank you for watching
Info
Channel: Tinker Tech Trove
Views: 11,351
Rating: undefined out of 5
Keywords:
Id: NqchLYWHCzA
Channel Id: undefined
Length: 12min 49sec (769 seconds)
Published: Fri Feb 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.