Raspberry Pi Servo Motor Control

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to another video from explaining computers comm this time it's a kind of stepping-stone video as we're going to learn how to control these things servo motors using a Raspberry Pi this will prove critical in certain upcoming projects so let's go and get started so here we have a servo or more specifically this is an SG 90 servo which is the most common type of small servo in the world on which is often used in things like model boats and aircraft and SG 90s like this are made by many different manufacturers and I bought this one in a pack of five Amazon co uk for eight pounds 99 with a similar pack costing $9.99 on comm and with each individual servo you get a set of three actuator arms or horns with screws to fasten them to the rotating shaft although here I simply pushed the actuator arm on the top it just goes on like that and stays on pretty well just by a push fit I can be fine for the test purposes in this video oh and I should note it's not a good idea to try and rotate the shaft manually you could damage the mechanism inside the servo to explain what a servo is and how its controlled it's best to compare a servo to a motor and so here we have a very small motor with a gearbox on the front and like all motors this has got an axle here which will spin continuously in one direction or the other depending on how the power is applied and to control the motor like this for the Raspberry Pi we'd need to use a motor controller such as this l-29 8 M which have used in previous robotics projects and when this is all wired up power for the motor is supplied to the controller and signals from 2 GPIO pins on the PI are used to turn the motor either one way or the other now in contrast to a motor a servo does not spin continuously rather it allows precise control of the angular position of its shaft and actuator arm and to achieve this inside a servo there's a motor some gearing a potentiometer or other feedback sensor to read the angle of the shaft and finally some control electronics it really is amazing that we get all of this in a tiny case for less than a few pounds or dollars now as you can see while a motor has two wires a servo has three wires coming out of it and two these are positive and negative power leads which for this SG 90 need to supply between four point eight and six volts with a positively being read and the ground well being brown the third orange wire is a new to supply a control signal which needs to be a pulse width modulation or p MW square wave like most analog servos the SG 90 accepts a 50 Hertz square wave so her pulse is expected every naught point two seconds the angle of the servo is controlled by the length of the positive pulse the longer the length of the pole saw duty cycle the larger the angle the servo turns to and attempts to hold itself at the range of potential values of the duty cycle there is a little between servos but for my SG 90s is from two and twelve percent of the period of the wave with a two percent duty cycle corresponding to a servo angle of 0 degrees and a twelve percent duty cycle corresponding to a server angle of a hundred and eighty degrees we'll return to these figures again when we look at our servo control code as you can see the wires from an SG 90 are terminated in a single three pin female connector and the easiest way to connect this to the pin to the Raspberry Pi is to use a number of these male two female jumper lead which connecting to the end of this connector like this there we are that could now plug into different GPIO pins on a Raspberry Pi and if you're wondering where'd you get jumper leads like this when you buy them in the ribbons like these where you can just pick off the ones you want and as with everything else I'll provide appropriate links in the video description you right I've now connected our SG 90 to a raspy PI and specifically this is a Raspberry Pi 3 B+ which I think remains the most popular model in common use although you can use any model of Pi do note over to raspberry pi is not the world's most accurate servo controller as most of its GPIO pins output software generated PWM signals which are less accurate than those created by dedicated hardware the Python code modules that were using in this video also only work with software generated PWM signals but for all that the most critical applications everything will work just fine as we can see in this diagram the servo signal wire is connected to GPIO pin 11 while it's negative power wire goes to pin 6 and the positive to pin fork which supplies 5 volts usually servos are powered with an independent power supply such as a battery pack with only the control signal and ground rails connected to the PI this is because servos control a lot of power which could potentially crash or we set the PI however as we're only using one tiny SG 90 servo taking power from the PI's 5 volt rail should be okay and I am using a 3 amp power supply here so let's move from the pie itself to the desktop this is a raspberry latest version of raspbian raspbian Buster and in raspbian bus trip would look in the menu under programming we don't have idle anymore which I might to use in the past we are have to use Sony a Python at least we could install Idol but I'll use Tony here because it's pre-installed so here I've run up Sony there it is and as you can see I've got some test pieces of code to control the server so what does this did well basically initially we were importing some libraries GPIO library and what time libraries going to need those and we're setting one numbering for G playing on the PI 2 of the board numbering mode which means that pinned a numbered 1 2 3 4 5 6 across the board in what I think is the most logical fashion I always use board numbering next we're setting up pin 11 as an output and setting servo 1 as pin level with a PWM control so do you go set up 11 GPIO out at set spin 11 as an output and then were defining servo 1 as a GPIO pwn 11:50 11 there is the pen 50 is the pulse frequency which is 50 Hertz for analog servo that's where gull 1150 after that we're going to start PWM running on that pin but with the pulse turned off so we're doing servo 1 start but 0ver have the pulse off so that means nothing's been sent to the server to type to do anything it won't be drawing power to and move things around it'll simply be waiting to see what's gonna happen next when after that the way for 2 seconds we do a lot of this in this code just so we can keep track of what's going on after that we're gonna move the servo so let's move down a little bit and this various bits of code hit had move it around a bit to give you a principle but initially we're going to a rotated to 180 degrees in 10 steps 18 degrees steps why 18 degree steps I guess it's what I happened to do is what this code does so to do this goes to find a variable called Duty which we have a duty cycle of the PWM signal going into the servo initially at a value of 2 and now here we've got a while loop as you can see and this is basically the cycle from the values of 2 to 12 which represent the position of the servo from nought 280 degrees until the cycle through those waiting a second between each so basically while Duty is less than or equal to 12 which industry these so value of 2 it'll say servo 1 change duty cycle 2 duty initially to sending a wait for a second Duty equals dou T plus wanna go back to there it'll be 3 4 5 6 7 8 9 10 11 12 and we have rotated this servo through all of those positions waiting a second between each after that will waiting a couple of seconds more why not it was good to wait a few seconds and then after that a couple of bigger moves with a thermos over back to 90 degrees by setting servo change duty cycle to 7 which is halfway between 2 and 12 you think about it again wait a couple of seconds and then we'll turn back to 0 which is a value of 2 and then finally on the end or was a good idea very important clean things up we'll set the servo to stop we'll stop the servo clean up ah gee if I opened and will print goodbye so that's what I'm gonna do so let's now bring up the servo onto the screen light with that and then we'll run this code it's very exciting isn't let's execute the code or wait in two seconds and now yes this little steps are starting it's a bit jittery isn't it yes stops him moving around and you might see a jitters a bit between the actual stages it steps toven from band now and again it's little bit unsafe little bit unsteady and what's going on there let's just run that code again to show you what's happening let's run the code second time the wait a second and then start doing its steps but it's sort of hesitating it's due to in a bit on each step when it gets there it's what the servo is doing it's been sent a signal to hold it at a particular position and F which sensor is trying to sense its position there and then it sometimes isn't quite there so the servo moves back and forth it jitters a little bit how can we get rid of that jitter you cry well we could do it using another piece of code which I happen to have over here what a surprise this opens up exactly the same way libraries and board numbering setting up the pin accessory no waiting for a bit after weaver set things up but now I've altered a little loop because here we're going to say while Duty is less than or equal to 12 and we'll set the duty cycle to be duty but then we're going to set it to the value they angle we've asked it to go to wait for 0.3 seconds which just gives it time to get there we're then gonna put duty cycle to 0 which is basically no pulse so basically we've moved the servo and then we stop moving it and then we wait for the rest of the time we were looking to wait for 0.7 seconds here could be any number you wanted to and then we'll go through the cycle again and similarly down here does exactly the same as the thing we've got I'm waiting point 5's let server move around you have to experiment a bit and it will wait for in the half seconds and out here again the same sort of thing and still clean up on the end so let's bring the servo back in and run the code again and hopefully you'll see when it runs what you wait for two seconds first and then after each move it's steadier this is moving stopping moving stopping moving stopping smoothly stopping moving something moving stopping wait a second big move coming up or where we are nice and steady nice and steady now the one thing to point out that there was a flaw in this method which is that when you're sending a post of a service a two-stage particular angle it's using its motor to stay there so if it was a tension the motor will be trying to keep it at position whereas if we turn it off then there's nothing to stop it moving this said if you go to a survey look this isn't know where this is quite sturdy so unless you have a lot of torque on this it's perfectly reasonable to move it and turn it off this issue of jitter you'll get in Cheaper servos and analog servos remember this is a very cheap server we have to accept its limitations the other bit of code I've got here which is servo test code 3 on my column is available to you our links in the video description if you want to play with this code this just shows you go to particular angles so again we set these up the same way imported libraries number setting up in starting etc but now what we've got is a bit of code to allow the user to input a value and the server will go to that value so this is an infinite loop here until I put the infinite loop he said while true while true is true forever so it is an infinite loop I put that inside a try finally setup and the reason for that is we've got an infinite loop so we'll have to break out of it by pressing ctrl C if you use try and finally finally it always executed even if you break out of a loop to vote just means on the end of it code will always stop the servo cleanup with GFI opens and of course print goodbye very important to print goodbye on the end of this program anyway back to the important bit which is the loop here so while true whether it's going to basically do is define angle by asking for an input so angle equals float input float means it's gonna be a floating pointer variable it's going to be a number rather than a piece of text we're inputting and it says enter an angle between naught and 180 server won't change duty cycle to angle over 18 plus 2 you think about that's the code you need because 2 is 0 and 12 is their total rotation so we basically divide by 18 to get the number out to to it to get the right angle if it's made any sense at all it'll work obviously then we're going to wait four point five seconds then we're going to turn this over off again to leave it at that position and it'll cause it'll go back and keep asking for a figure so we execute this piece of code we'll go up here and execute the code it's come up and right angle between Norton 180 I've got to think of an angle now let's pick 30 it's gone to 30 let's pick up 45 let's pick 90 let's pick one three five and being very regular on time let's pick 67 point six four that's exactly right is now it's good one eighty there we are back to thirty back to zero it's proving a principle isn't it and if we press a ctrl C it'll come out in a fairly controlled way so that's that's nice and good so there we are that's showing us some basic code to actually control a single servo using a Raspberry Pi right here I am back again and as you can see I've now got two SG nineties two servos connected to a Raspberry Pi I'm going to call them servo 1 and servo 2 and the wiring here has got the second servo connected to a GPIO pin 12 4 it's a signal pin and it's using pin 9 as its ground rail it's negative rail and pin 2 4 5 volts which is a second 5 volt pin on the Raspberry Pi and I should say as I said earlier normally when you power servos you use a separate independent power supply it should be a lot more like this here but I'm only running two small servos and I'm using a 3 amp power supply the Raspberry Pi so this will work this is okay for now so let's go back to our code here we are back in a sunny in the raspbian this code is pretty much as we have before imports libraries sets the numbering except here we now set up two pins to be servos as you can see we're defining servo 1 as we did previously but also servo two but after that we just scroll down a little bit with a start on both running with a PWM and then we're going to do a few moves to show you in control two servos at once first of all turn the first servo to 90 which is going on there then we're going to wait for a couple of seconds we like waiting for a few seconds in this now don't wait to shows us what's going on and if we go down a bit further where are we down here we're then going to move servo to 290 in servo 1 back to 0 which is that code there without going to wait for 2 seconds again and then we're going to do some more rotating what servo - to 180 and servo 1 to 90 and then finally we'll move them back to via the starting positions so that's what should it go on this is just demonstrating to you we can control two servos so let's bring the servos into view and execute this code well I don't know about you but I think that was one of the most exciting things ever we prove we can control multiple servos using a Raspberry Pi as we'll see in future videos the ability to control servo motors presents all kinds of possibilities for building robots as well as Raspberry Pi vehicles and home automation but now that's it for another video if you enjoy what you see in here please press that like button if you haven't subscribed please subscribe and I hope to talk to you again very soon [Music] you
Info
Channel: ExplainingComputers
Views: 256,955
Rating: 4.9606023 out of 5
Keywords: 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, Christopher Barnatt, Barnatt, servo, servos, Raspberry Pi servos, using servos on a Raspberry Pi, Software PWM, GPIO servo control
Id: xHDT4CwjUQE
Channel Id: undefined
Length: 17min 35sec (1055 seconds)
Published: Sun Jan 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.