Raspberry Pi Servo Motor Control - No Jitter!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is gary sims and this is gary explains today we're going to be looking at servo motors and how you can control them from the raspberry pi so if you want to find out more please let me explain of course the raspberry pi is a very versatile single board computer you can do loads of stuff in software python cc plus java golang rust whatever it is that you want to do and not only can you interact with the display or with over the network you can also interact with the real physical world through the gpio pins it's the general purpose input output pins and probably the simple example of that is being able to flash an led but you can control much more than just leds you can you know do temperature control there's stuff you can do humidity there's all the different kind of sensors that you can get and also you can control motors stepper motors and servo motors and today we're going to be looking at servo motors and how you use them from a raspberry pi now a server motor is different to a stepper motor a stepper motor is basically an engine you go forward you go backwards you can use it for robotics for controlling belts whatever it is that you want to do now a servo motor is different in that you tell it you want to go to a certain position and it goes to that position so it doesn't go round and round around forwards and backwards it just goes to a certain position and that's great for example you know steering on a rudder on an airplane steering on a car steering on some kind of robot opening and closing things you know anything we need a precision control to move something physically now i had an idea to build a kind of a mini radar system i've bought a timer flight sensor i think measures about 10 meters and i thought would it be great if i could sweep left and sweep right using this time of flight since i could always fix on a certain position as well and to do that i need a servo motor that allows me to move this thing in in a precision way so that i can actually build this mini radar system so that's what we're the aim is in a future video we're going to be looking at this mini radar system with a timer flight sensor but to control it to move it physically we need to use a servo okay so this is a servo motor these are very very cheap they only cost a few dollars a few pounds a few euros you can buy them individually or in packs of five and ten you can get them all over the place any electronic store any store that stock stuff for arduinos or for raspberry pi's you can even get them on amazon and basically in here you have the motor and all the gears that can control this top part here to spin to a certain position now out on the top you normally get a set of what they call horns which is basically the arms and you can put them on a few different shapes available you can screw that in there as well now the motor is controlled using this cable is built in here and at the other end there is a connector and there are three cables you can see brown red and orange in this case the red is for the positive the brown is for the negative and the orange is the control cable now because you would then connect this to a raspberry pi using the pins there are no three pins that satisfy that combination on the raspberry pi so we kind of need to break out from there you could cut the end off here and use them somehow or you can use these jumper cables again they're available from anywhere that does raspberry pi or arduino stuff okay and it's a male and a female so basically you put the brown here into the brown part okay keep the colors the same and now i have a header here that i can connect to a pin on the raspberry pi so for example here is a raspberry pi and we can connect this to one of the ground pins you'd connect the positive to one of the positive pins and then you connect the control pin to whichever pin you choose i use gpio12 and i'll talk more about that in a minute just worth pointing out that i'm using the five volts from the raspberry pi that's okay for this demo if you're doing a more complicated project you probably want to use a separate 5 volt power supply just need to make sure that when the servo is connected to that power supply the earth wire goes to both the power supply and to the raspberry pi and normally at the end of the video i would say if you like this video please like and subscribe so on i think i'll move that segment forward in my videos for a little while see if that helps build up the community here so if you do like this video please do give it a thumbs up and if you'd like this kind of videos please hang around by subscribing to the channel okay so let's go ahead over to our raspberry pi and look at the python code for controlling the servo okay so here's our first program really quite simple we import the gpio0 library because that has a component for controlling servo so it understands servos it understands the uh pulse width modularization stuff so it's really simple and using it is really simple all you do you say i want to create a new servo and then you give it the pin number that you connected the servo to in this case pin 12. now remember this is gpo pin 12 not board number 12. if we have a look at an actual diagram of the raspberry pi you can see that gpo 12 here is actually pin number 32 so there's board numbering one two three four or up to forty then there's these numbers here gpio numbers which are related to how they're connected actually to the raspberry pi's processor so gpo 12 pin 32 and that's what i've connected the servo to and then it's really simple what we're going to say is we're going to print out and say start in the middle and then you say servo mid and that's really easy it says put it in the middle position we're then going to sleep for five seconds we're then going to say go to the minimum position and then so we say servo min then we're gonna sleep for five seconds then we're gonna say go to the max position and we say servo max very simple server i mean server max server mid sleep each time and then finally back to the middle where we do uh server mid again and then this last call here actually stops the uh signals being sent all together so the motor just freezes in the position it's in okay so we're going to run this and see what happens and it's not actually as nice as we think so of course we start off by going to the mid position and you can see it's having a hard time keeping it there in the mid position then we say go to the min and again it's kind of it when there it kind of came back and this is because these uh pulses that are being sent if they are not exactly at the right width of course really it's telling it to move somewhere else and these are software generated which means that they're done by the software and as you can see that as we went from min to max back to middle again it really had a hard time struggling to do that now that dancing around we saw there is called jitter and the reason it's happening is because the pulses are generated by software and if the software is out by even one millisecond then the motor thinks it needs to be somewhere else because these are this is done on a millisecond basis now the great thing is the gpio0 library allows you to use different subsystems for controlling the pins in this particular case we want to use pi gpio which is a special library let's go and have a look at it that's got some great features and the main one is here there's lots of hardware based timing for pwm and for servo pulses so the great thing is if we use this library it's actually going to be a hardware base rather than the software base and that means it's mega accurate so let's go back to the code so the way we do that is we say from gpio pins dot pi gpio import and then the way it calls it is called a factory the thing that's used to control the pins and when you create the servo here now you first of all you create a new factory according to the pi gpio library want to use and then when you create the servo here still on pin 12 you say use this pin factory and the first time you run using the pi gpo library you need to make sure that its server component is running so to do that you type sudo and then you type pi gpiod at the end because it's running as a background task and that will then fire that up so now we can go back to the code over here and the code below it is exactly the same so you've always changed this factory part here saying we want to use a different way to control the pins don't use software if we go back here to the original program actually see there was a warning given out saying that it was actually using the software fallback and that to reduce the server jitter use the pi gpo pin factories what we're doing so there is this error message actually that comes up when you try to run it using the old method now in the new method we use this uh pi gpo factory and the rest of the stuff is exactly the same mid min max all that stuff it's active so now if we run this one we'll see it's much much smoother so go to the middle there you go it's not wobbling about it's not lifting from shaking from side to side now go to the minimum there you go it's turned now it's going in a second it's going to go to the other end there you go to the maximum okay and then it will go back to the middle again so that's pretty rock solid there no jittering no dancing and that's that's absolutely a much better solution but did you notice something let's run it again did you notice that it doesn't go all the way to 90 degrees it's kind of like what is that 60 degrees there there you go look that's not all the way around to 90 degrees and when it goes back the other way it's exactly the same there's a bit of a problem now let's just talk about that for the moment what it turns out is that if you look at the specifications for one of these sg90 um servos again here is a data sheet for one of these sg90s and it says here it should be able to do 180 degrees 90 degrees in each direction okay and it gives you the stuff here it says look between 1.5 means in the middle 1.5 millisecond pulses two millisecond pulses means uh to all the way to the right and one millisecond pulse means all the way to the left and that's how it should work and here is a kind of a little diagram to show you the width of the poles between one and two milliseconds but it turns out that the ball the motors i bought even though this is actually the same data sheet that you get for it one has actually got slightly different it actually needs to be um nor 0.5 milliseconds uh all the way to the left 2.5 milliseconds all the way to the right and still 1.5 for the middle so it's actually it's got a slightly bigger range which is why my ones are not going to where they should now the good news is that inside the gpio0 library you can say set the minimum pulse so in this case i've said 0.5 milliseconds set the maximum 2.5 and then it does all the rest of the calculations for you so again pin 12 we're passing in the other factory the other way of controlling the pins hardware control now not software and i've actually changed the range and now if we run this one we can see it actually works much better so we start off in the middle that's still working absolutely fine and then after we've waited our few seconds there we go now that's 90 degrees you can see that's absolutely much better to what we were doing before and back down the other way there you go so that's now 90 degrees and then it will come back to the middle again in a moment there you go no jitter and now the full range so hopefully if you buy one of these to use on your raspberry pi it will actually have the standard values of um 1 000 1.5 and uh and 2000 but if it doesn't play around with the minimum and the maximum here and that's how i got my one to work properly so the final program i want to show here is that the the reason we're doing this i want to make a little mini radar project using a flight of time sensor that i've bought and i want to mount it on this and i want to do sweeps sweeps to the left and sweeps to the right like a radar would do so what we're going to do here is this beginning stuff this code is actually the same we can see here we're setting up a servo i've changed the widths here the the beginning and the end as we discussed previously still on pin 12 but what i do here is i say what go from 0 to 360 and then set the servo value to be whatever the sine value is which of course gives us a number between minus one and one which is what the value is expecting is somewhere between uh minus one and one with zero being the center and then because this is in uh you have to convert it to radians that's always be going along there and then sleep a little bit so this is a really simple program but basically using a cyan wave will sweep from the left to the right from the left to the right and that looks actually looks quite nice so let's just run that and see that running so there we go you can see it's swinging and then because it's a sine wave of course it slows at the end comes back around again slows it's a nice sine wave here and there we go now imagine a little radar unit pumped on top of that and it's kind of sweeping left and right that will be the next project that we do now that we have this absolutely working now before i go i just want to say i do have a monthly newsletter called gary explains where i cover everything i've been doing here on this youtube channel everything i've been doing over on android authority plus whatever else i found interesting on the internet if you want to subscribe go over to gary explains.com type in your email address no spam just the newsletter and i hope you enjoy it okay that's it my name is gary sims this is gary explains i really hope you enjoyed this first look at servo motors as i said another video coming in the future where i put a timer flight sensor on top of that uh rotator on top of the horn and i build a mini radar system okay that's it i'll see in the next one [Music] you
Info
Channel: Gary Explains
Views: 41,473
Rating: undefined out of 5
Keywords: Gary Explains, Tech, Explanation, Tutorial, Servo motor, SG90, SG90 servo, Raspberry Pi, Raspberry Pi servo motor, Raspberry Pi servo control, Raspberry Pi PWM, Pi servo control, Python servo control, servo control code, Raspberry Pi robotics, servo, servos, Raspberry Pi servos, using servos on a Raspberry Pi, Software PWM, GPIO servo control, gpiozero, gpio zero, pigpio, pigpiod, Raspberry Pi 4
Id: _fdwE4EznYo
Channel Id: undefined
Length: 13min 52sec (832 seconds)
Published: Tue Jun 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.