Paul McWhorter arduino lesson 28

 


 
What we are going to do in this lesson is we are going to show you how you can control a servo using the Raspberry Pi a couple of things. I need to tell you right off the bat number one. This is not going to be as easy as controlling a servo with the Arduino. I will say I’m going to show you step by step and it’s very straightforward it’s, not just a simple matter like it is on the Arduino, though of just writing to the servo and say go to a certain position: we’ve got to do a little bookkeeping. Okay, so what you need to know is in order to control the position of the servo. You have to send PWM signals to the Raspberry Pi GPIO pins. Good news is in lesson number 27. I showed you all that you needed to know about doing PWM on your GPIO pins. What I am suggesting now is is that if you haven’t done less than twenty seven yet or it’s been a while since you’ve done, lesson 27 go back and look at that lesson, and that will make this lesson 28. Very easy. Where is lesson 27? Go to my most excellent website, WWE at Raspberry Pi with lies, Linux, lessons and then come down and you can click on lesson number 27, and that shows you how to get GPIO PWM on your Raspberry Pi, okay. So that is just a heads up refresh your PWM.

You could probably do this lesson without it, but this lesson will be easier if you go back and you look at that lesson. Okay, item number two understand that your raspberry pi can only put out so much current and if you hook a big old servo to your raspberry pi, that wants more current than what your raspberry pi can produce. You can actually damage the raspberry pi and so before. I hook this servo up to my raspberry pi. I looked at the spec sheet of the servo and verified that it does not require more current than what the Raspberry Pi can easily produce. If you are worried about that, what you can see is is that I am actually powering my servo through physical pin two on the Raspberry Pi. If you are worried about burning out your Raspberry Pi, take this power line off and go connect it to an external five volt supply drive the servo power, the servo from an external 5 volt supply, and you can still control it from the Raspberry Pi through this Yellow wire, ok, just power it from an external voltage source control. It from the Raspberry Pi make sure that you use a common ground. You have to connect all your grounds together for that to work. Okay, so don’t burn out your Raspberry Pi don’t hook this up in power unless you’re sure that you’re okay, I am sure that I am ok so I’m configuring, my servo as follows.

I connect the red wire to physical pin 2, which is 5 volts out on the Raspberry Pi. The black wire is ground. I am connecting that to pin 1 3 5 7 9 physical pin 9 on the Raspberry Pi as a ground, then physical pin 11. I use to control the servo on my servo. Black is ground red, as power in yellow is control. If you do not have a black, a red and a yellow wire coming off of your servo. Look in the instructions and see what your color codes are, but whatever the color codes are, you can put power to pin 2, you can put ground to pen physical pen set 9 and you can put a control to fiscal pin 11. So let me just make sure 1 3 5 7 9 physical pin 9 is ground. Physical pin 11 is control okay, so you can hook yours up like this and then you can follow along in these lessons. Alright I’ve got to stop and I’ve got to talk a little bit about how servos actually work, because if you are from the arduino world, you think servos work by just saying: write, zero or white, write 180 or write 90 or write. Whatever your desired angle is – and it just pops over there and does that on the raspberry pi it’s – not that easy and we have to apply pwm signals and we have to sort of understand how the servo works and so i’ve got some important points here.

I’Ve got a little document. I want to go over with you and this will allow you to understand how the servos work first of all, servos or servos are controlled using PWM signals which you learned about in lesson. 27. In lesson. 27. I showed you how to send and control PWM to your GPIO pins on the Raspberry Pi. Second thing is most servos like to see a 50 Hertz control signal. So when you set up your PWM on your GPIO pins, you want to set it up for 50. Hertz I’ll show you how to do that in a minute, but just understand you can’t it’s, not good to just go in and try to use any old frequency. You want to set the frequency of your PWM signal to 50 Hertz. Good news is that’s easy enough to do. Third, the position of the servo depends on the pulse width of the on part of the cycle. Okay, so when you have PWM, you have a pulse train coming across like this. That you’re repeating where the servo goes to depends on the pulse width of the on part of the cycle. How long you leave that on is what defines where the servo is going to point most servos are in the middle. This would be the middle like this. Most servos are in the middle, with a 1.5 millisecond pulse width. So if I send a PWM signal and the width of the on part of that is, 1.

5 milliseconds I’ll be kind of up and down in the middle on the servo, most servos are in the full right position at two milliseconds. And so, if I turn this on with the two millisecond pulse, it’ll spin over here, like this alright and most uh servos are in the full left position. Let’S see I’m. Sorry I’d need to just go over these again. Full left is one millisecond. Middle is 1.5 millisecond and full right is two milliseconds okay. Now the thing is: when you do a PWM command in the Raspberry Pi, it doesn’t ask you for pulse width. It asks you for duty cycle, so we need to go through some definitions that a little math to understand how to go from duty cycle to pulse width. The first thing that we have to understand is the period is equal to 1 over the frequency. When we set the raspberry pi up when we do setup the PWM, we tell it what the frequency is well, what you’ve got to understand is the period is 1 over the frequency. So what frequency did we see say we wanted to use 50 Hertz? I will show you how to do this in a minute or this stuff is ringing a bell if you looked at lesson number 27, so for a 50 Hertz signal, the period of the signal is what the period is 1 over. The frequency 1 over 50 is equal to 0.02 seconds, which would be 20 milliseconds.

So what you need to understand is the full amount of time that we have to work with with a 50 Hertz signal. The full period is 20 milliseconds to be in the full lab position. We would want a pulse width of 1 milliseconds. One millisecond would be a duty cycle of 5. Why? Because what’s the full period, the full period, is 20 milliseconds, what’s 5 of 20 that’s point O 5 times 20. That would equal one millisecond. So if I send the pin a command to go to a duty cycle of 5 percent with a 50 Hertz signal that would equal one millisecond, so that would move the servo to the full left position. Okay, to be in the middle position, we would want to pulse width of one point: five milliseconds one point: five milliseconds would be a duty cycle of 7.5, since 7.5 percent is 0.075 times 20 milliseconds would be one point, five milliseconds and remember from most servos. One point five milliseconds would put the servo up and down and finally to be in the right position all the way over here we would want a pulse width of two milliseconds, which would be a duty cycle of 10 since 0.1, that’s 10 times 20 milliseconds that’s. Our whole period would be 2 milliseconds, so we can see sort of 3 values on duty cycle to go from 0 to 90 to 180 bottom line to move the servo full left. We need a duty cycle of 5 for the center position, a duty cycle of seven and a half percent, and for full right a duty cycle of 10.

This is all based on setting up a 50 Hertz signal. To begin with, intermediate values would just be linearly scaled between that, so you would just linearly scale your duty cycle from 5 to 10 to go from a position of 0 to 180. Okay. Now we must test these to see if they are reasonable in experimentation. We found so we got to test these things and see if actually, our particular servo really does what we think it should at five, seven and a half and ten and before we go in and do more math. We need to sort of tweak these values. For our particular servo, so what do we need to do? Well, we need to call up our odd. We need to call up our let’s, not that we need to call up our terminal window, which I should be able to do here. Okay, pardon me, I should have had this pulled up already, but I will get it done here very quickly. Give me just a second to get this configured where you can see it: okay and I’m. Sorry, I did not have this set up beforehand, but now we are in a terminal window and hopefully you can see this all right so now we’re going to start operating the servo. What I like to do is I like to we’re going to do this? All in Python and we’re going to write a Python program to do this, but as I’m learning I like to run Python in the shell, because then I can give commands one at a time and see if it does what I want so we’re going to learn.

How to do this, giving the commands one at a time in the shell and then we’ll come back and we’ll write a program in a minute but to enter the shell. How do we do it? We say Python, but we want to enter the shell. Is a super user pseudo Python because, okay, because uh, the normal PI user, does not have access to the GPIO pins? And so you want to go into the Python shell as super user, and that way it will take your commands to mess with the pins. So we’re going to go sudo python, ok, boom. Those three little arrows means that we are now in the Python shell, so what’s. The first thing we need well. We need to get the rpi library, so we’re going to say import, we’re going to say RP little. I dot GPIO as GPIO, so we’re going to import the RPI library boom. It likes that if you get an error here, you need to exit out of the Python shell and you need to you do that with a ctrl D and you need to go in and do a sudo apt get update and a sudo apt get upgrade, because The later versions of the operating system will have this pre loaded, and so, if you update and upgrade your system, you will have this ok, so we have loaded the library. What do we need to do now? We need to tell it what pin configuration, what pin numbering configuration we want to use.

I like using the physical pins, the physical pins are the easiest because it just goes. 1. 2, 3. 4. 5. 6, like that and that’s easy for me to remember, so I need to tell it to use the physical pin numbering convention. How do I do that? Well, I tell it GPIO dot set mode and then I’m going to go GPIO dot board. When I say dot board that’s saying to use the physical pin code configuration if I’d said dot DCM, then it uses that goofy gpo GPIO table I’ve got that. I think in lesson 25, but if you do board, you can just sort of remember what to do so, let’s see if it likes that boom it likes that now. What do I need to do? I need to tell it that this pin 11 is an output 10 remember. This is pin 1 3. 5. 7 9 is ground and we’re connecting and controlling through 10 11, and so we need to set up 10 11 as an output, so I’m going to say, GPIO dot, set up and then I’m going to say, which pin 10 11 and it’s a what it’s a Gpio dot out like that boom pin 11 is now an output. Okay, now we’ve got to set up the pulse width modulation. So what am I going to do I’m going to create an object called PWM and I’m going to set it equal to GPIO dot? Pwm that’s the command, and what do I have to tell it? Well, I’ve got to tell it the pin that’s 11, and then I have to tell it what the frequency now that should be.

I got to tell it the frequency what frequency do servos want to see 50 Hertz. We talked about that. So I see 50 okay boom. I now have 10 11 set up to be a 50 Hertz PWM signal, but what I’ve got to do now is. I have got to tell it to start the PWM, so I’m going to say PWM dot start and then what do I do in this particular case? I need to give it the duty cycle well for a full weft. I think we said a duty cycle of 5 should put it about pull left now, we’re going to have to tweak these values and sort of see exactly what they are, but when I give this command, this should turn almost all the way to the left. Let’S see if it does not quite so let’s let’s play around with this a little more let’s go now. What I got to do is I’ve got to play with the duty cycle and the way you do that is you say: pw m, pw m dot, change. Duty cycle, okay and let’s say you see, i didn’t go as far to the left, as I wanted so let’s say: go to a duty cycle of 2 and let’s see if that gets that over there black boom that got it over to the left. Let’S say: if we go to 3, it should be a little bit more to the right. Ok, it moves a little bit to the right now.

What did we say roughly full up and down would be. That was 7.5. If I remember right, roughly okay and that’s kinda it’s a little bit past, but it’s kind of in the middle and then all the way to the right, I believe we said, would be 10, okay and that’s kind of like almost all the way to the right. So you see those general purpose: numbers of 5, a duty cycle of 5, 7, 12 and 10 got us in the ballpark, but before we actually do the math we want to kind of you know, tweak those numbers so that we get it working just exactly right. With this particular servo, so I want to see what the full left position is and so, instead of 5 I’m going to go all the way down to one so watch the servo. Okay, that sure went all the way over, but I might have gotten too far. So I’m going to look just to move it at all, no to didn’t move it. So two is still full left what it would three be full left, no three moved a little bit to the right. So if I go back to two, you can see. Two is really pretty darn close to all the way to the left. So now I need to see what is all the way to the right. Well, normally, we said so I’m going to write this down. Full left is two and that’s a duty cycle of two percent is what that is two percent duty cycle on a 50 Hertz signal, so let’s see what will take us all the way to the right.

Ten does not quite take us there let’s see. If 11 does okay, 11 took us a little further let’s see 12 okay 12 seems to even take us a little bit further, and so that 12 seems to be pretty good. So I have two and I have 12. If I go back to 11, you can see it backs off a little bit. If I go to 12, it goes a little further. Let’S go back to all the way to the left, to let’s go all the way to the right 12. Okay. So where would we expect halfway to be well halfway between 2 and 12 would be 7? I do believe, and that is pretty darn right straight up and down in the middle okay. So what I need to remember is the two points that take me to the two extremes or a duty cycle of two is full left and a duty cycle of 12 is full right, so let’s go back and let’s do a little math, because I want to Just send it a command. You know basically of an angle that I want to go to, and then I want to go to that angle based on doing some math, but before I leave here before, I forget it I’m going to turn the PWM off so I’m. Going to say, PWM stop, okay, that turns it off and then I’m going to do GP, io, dot, clean up boom; okay, that should kind of shut things down, and you know I’ve sort of left, gracefully and I’m going to ctrl D.

To get back. To my blunt Linux, command line all right now, let’s go in and let’s do some math okay. So what we did was we found that two percent and 11 percent? What we found was that the 5 percent 7 12 and 10 percent weren’t the perfect numbers for our servo for our servo. We found that 2 percent was right on the low end and 12 percent was right for the full right position. This might be different on your servo, but if 2 was full left or if I was full left, just put that full left and full right just make note for me, it was 2 percent was full left and 12 percent was full right. Yours will be a little different, but you can still follow along with the way I’m doing this. Ok. Well, if we want to input a degree setting and have it moved to that position, what we would want to do is we would sort of need two points and draw a line between them. If we enter 0 degrees, we want it to be full left, which we know is a duty cycle of two percent. We just did the experiment, so the first point we want to work with is the point 0, which is the desired degree and 2, which is the duty cycle which would give me 0 degrees. Okay, if we enter 180 degrees, we want it to be full right, which would be a duty cycle of what I’d said.

11 percent, but really it was 12 percent. For me, yours might be a little bit different. This would be the point 180 12, because if we put a duty cycle of 12 it was making it go all the way to 180. So I have 2 points all right. If I want to go to zero, I apply a duty cycle of two. If I want to go to 180, I apply a duty cycle of twelve and any value between zero and 180. You just come up with an equation of the line and you can move it to any position. You want perfectly with two points. We can calculate the slope of the line. The slope of the line is y2 minus y1 over x2 minus x1. Well, that would be 12 minus 2 for me, divided by 180, minus 0 and 12. Minus 2 would be 10 over 180, which would be 1 over 18. Now you can do this. You do this with your own numbers that you find with your servo don’t use my numbers: do it the way I’m doing it, but you’ve got to get the number here, and here this 2 and the 12 that were what moved your servo. Hopefully that makes sense. Now remember the point slope form of the line is y. Minus y1 is equal to M over m times X minus x1. So we got to pick one of these points. You can pick either point 0, 2 or 180 12, but it’d be easier to use 0.

2, so we’re going to pick that point so Y minus 2 is equal to the slope, which was this became 1 over 18. For me, he’s became 1 over 18. For me, so Y minus y1 is M onto X, minus X, 1 Y minus y2 is equal to the slope, which was 1 over 18 times X. Minus 0 that’s that zero there so Y is equal to 1 over 18 times X 2. So we input the degree position we desire is X and then we calculate Y, which will be the duty cycle so let’s take this and let’s use our real variable names. The duty cycle is going to be equal to 1 divided by 20 times the desired angle plus 2. Then you just plug in your desired angle, and it will calculate the duty cycle that will take you to that desired. Pretty cool huh, okay, let’s see. If we can write a Python program to do that, if we are going to write a Python program, we need to do what. Well I Nica LS and C I’ve got a probe. I’Ve got a folder called my Python I’m, going to put things there. If you don’t have one you could do a make directory my Python from your home directory mine is already there so I’m not going to do that. I am going to go down into it. I like to go down into my folder, because that way I don’t have to keep typing in my path, name so I’m going to go to my Python and then LS.

You can see that I got different things there. I need to RM servo dot PI, because I want to make that ID kind of practiced it before I got on camera so now. What do I want to do? I want to create a program. How do I create a Python program? Why do Nano and then what do I want to create the program? Servo dot PI come up with a bright spankin flashy new empty file. So how do I edit it what’s? The first thing that we do we’ve got to import the library. What was the library? Our P little I got GPIO as GPIO, okay, capitalization matters. You have to do it exactly this way: big R, big, P, little dot, GPIO all caps as GPIO all caps all right. What do we have to do now? We have to do GPIO dot set mode. Okay, GPIO is all caps set mode, is all lowercase, and then what do we do? Gpio? What is this command doing? It’S saying what pin out diagram I want to use, I want to use the physical pin out so I’d say what Bo AR D remember that from earlier and from the earlier lessons ECM, if you want to use GP at those GPIO configurations. But here this is just straight: the physical pins on the board easy to remember: okay! Now what do I do well? I’M, just going say months, I want to give my servo servo pin I’m going to call that physical pin 11 and that way I don’t have to keep typing 11 in what do I need to do now? I need to set that as an output, so I do GPIO dot set up and then, which can am i working with servo pin and then what do I want it to be? I want it to be a GPIO dot out so I’m setting servo pin to be an output, 10 servo pin is pin 11 and that’s where I’m connected here all right now is where things get a little tricky.

I need to create my PWM objects, so I’m going to say PWM. You could name this whatever you want, but then you’ve got to use that vein from then on out. So PWM is going to be equal to GP, io, dot, PWM and then, which pin am. I setting up I’m taking setting up servo 10 that’s, where I want to do this and then what frequency do? I want? Okay, I think I’m going to come up now. I’Ll just do it here, so I’ll just say: 50 Hertz, remember: servos want to see what frequency they want to see 50 Hertz. So I tell it 50 Hertz there that should make it happy all right now, let’s see I’m going to do a for loop, because I want to loop through this and in the loop. I want it to ask me what position you want to go to and I want to go to that position and I won’t say what position you want to go to and then I want to go to that position. What position you want to go to? I want to go to that position so I’m, going to do it in a 4 week so I’m going to say for I in range, okay, and where do I want to go? I want to go let’s say I want to go 20 times, and so I go from 0 to 20, which really will go. Stop at 19 and 0 to 19 is 20 choices and so I’m going to be able to do this 20 times all right.

Now. How do you indicate a for loop in Python, you tab over so what’s? The first thing I need to do I’m say desired. Position is equal to input. Where do you want the servo question mark and then what we are going to say is from from 0 to 180 that’s, just telling them what range they can use and then I’m going to put that, and then I close that off like that. Okay, where do you want the servo? Alright, I think that’s going to work I’m. Sorry that this is let me let me bring this on out so that you can see it a little bit better. Ok, so you see that that’s just saying: where do you want the servo question mark and prompting it that you can enter between 0 between 9 and 180? Ok, I just want to see that on one line, so you didn’t get confused by that dollar sign alright. So that is straightforward now. What do I need to do I needed to before? I did this I needed to start before the for loop. I need to start it, so I need to do PWM dot start and I just got to start somewhere so I’m going to start roughly in the middle, which should be about seven okay, so PWM start. What you put here is your starting duty cycle, and so I started at a duty cycle of seven, which you put it about somewhere in the middle right.

When I write when I turn the thing on okay now I need to come down, and so I input from the user the desired position. Then I want to apply that position. So what do I do? Pwd WM and then what do I do? I say change duty cycle. Alright. What do I want to change duty cycle? Well, I want to change it to duty. Sakuragi see all right. Well, what is DC. I better calculate that. Okay, let me tab this over okay, so I better calculate duty cycle. So how do I calculate duty cycle well, I’m, going to say duty cycle is equal to my slope. I need to peak over here again my slope was 1 over 18 okay, 1 divided by 18. Now, listen to me if you just put 1 divided by 18 or 1, divided by whatever your slope was for your servo. An integer divided by an integer is 0 and your thing’s not going to work. So you have to force this to be floating point by saying 18 point. So if I say let’s say 1 point divided by 18 point that forces those two numbers that forces those two numbers to be floating point numbers, which means that you’ll get a real slope when you divide that and not just get 0. Hopefully that makes sense times what times our desired position: okay and then, if I go back and look at my equation, I believe it was plus 2, so I say plus 2, so I asked for the desired position based on the math we did.

I know that the duty cycle should be 1 divided by 18 times. The desired position plus to your equation might be a little bit different for your servo. Alright, then, I need to apply that with a PWM change duty cycle to D which I’ve just calculated and now that should put it there all right. So as long as I’m in that loop it’s just going to ask me: where do you want to go I’m going to go there? Where do you want to go I’m going to go there? Where do you want to go I’m going to go there and now in the program we need to exit gracefully, which means we need to do some cleanup. So I need to do my or I need to do. Pwm stop like that to turn the PWM off and then I need to do a GPIO dot clean up and that frees those pins up, and so you want to do this. You want to do this stuff so that your to keep in you know keeping your bookkeeping good and keeping cleaning up after yourself all right. So now, how do we save it? Control? Oh and then it says file name right, servo, dot, pi! I will accept that. So I click enter and then how do I exit out, control X now, remember you have to run this as pseudo. You have to run the program as pseudo so that you can access those pins.

It won’t let you as PI access them so I’m going to say sudo. I am down in the same folder let’s look LS I’m in the same folder with the program servo PI, so I don’t have to give it a path name. So I can say sudo python: what program to run servo, PI and we’ll see if I made some mistakes in there whoa. Where do you want to get set, it 909 to 180 yeah, that’s, that’s, no good control, X, control, C, all right, let’s let’s go back and edit that again nano servo, PI okay. Why did I put it? Why did I put an on here that’s a zero window from I always try to use the mouse when you can’t use the mouse? Okay, that should have been zero and also I’ll scoot this over. So you can see it. I don’t want my answer to the question to be jammed up against the zero sup should put a space there, and that way it looks a little bit nicer when I run all right, and so now we should be able to bring this back over here, like That okay brings back over here a little bit and now we should control. Oh, do I like that file name enter and then control X. Okay, now let’s do a sudo python, servo, dot, dot, pi import. I must have a. I must have an Arabic, so let’s go in and look and see what I did wrong.

I probably accidentally typed something in there. Yeah look at that I’d hit a key that I shouldn’t have not have an O there. So hopefully you guys saw that. Hopefully, you don’t have that problem so I’m, going to ctrl, o and then enter and then ctrl X, now I’m going to sudo python, servo PI, all right now, it’s giving me an error it’s, giving me a warning because I killed the program. The poke program crashed before it got to that code that cleans things up. It should still run. It just gives me a warning. So where do I want to go? I thought I’d change that non till. Maybe I thought I’d change that darn. It I’m sorry guys. Let’S go back and look at it: okay, sudo python, servo, PI, control, vex! You will see something too making nano servo PI. I need to edit it okay, I see what I did like an idiot. I added the zero but I didn’t take the nine out so zero to 180 control. Oh filename right, servo pie. I like that, so I will click enter and then I will click control X to get out of here and now sous sous go Python and then what program do I want to run servo dot, pi? Okay? Where do you want to set the servo let’s? Try? Zero now you guys all eyes on the servo over here all eyes on the servo. Look at that all the way to zero let’s see if we can go to 180 boom, all the way to 180 let’s see if we can go naughty in the middle right.

There in the middle and do the bath don’t just guess and play with it, do the math like I did and you see when you do the math, your servo works exactly and it’s. Not it works exactly right and it’s, not like every other servo. It works perfectly for the servo that it is let’s try, 45 degrees boom I’m at 45. Let’S try 135 boom I’m at 135. Let’S go back to zero. Okay, I am at zero 90. Okay, 180. Okay, let’s see let’s go 175 let’s see if I can just back off just a little bit. Okay, you see how that moved. Just a little bit let’s go to zero. Now let’s just go to five okay, see how it moved just a little bit. This is perfect, servo control and to get the perfect servo control. You do the calibration like we did earlier in the lesson, and then you do the math and then this thing will do whatever you want it to do. You can imagine now that we could write a program that would just smoothly step from zero to 180. I know you guys get tired of me, but I just got a. I just got to do that. Okay, so let’s let’s play around with this let’s go suit. Let’S go nan, oh servo pie and this time I’ll save it as a different is a different name. But what I’m going to do is I’m going to say, for I in range, ah let’s go 0 to 180 degrees, okay, so I’m, not going to input! It I’m just going to tell it to go from 0 to 180 degrees and then I’m going to come here and I delete this I’ll go to the end and then delete all this okay, so I’m telling it.

I want to go from 0 to 100 degrees. I still have to calculate my DC, but my DC is going to be based on I this time because I’m not inputting something so it’s going to be I okay and then PWM duty cycle. That should, I think that should do it. Okay and then I’m going to kind of show my lack of remembering about Python, but I think I can go for I and range. I think I can go backwards. I’M, going to say: go 180, comma 0. I might need to put like a minus 1 in there, but we will see if this will work again. I work with so many different programming languages that I’m always switching between them. Sometimes I forget the syntax, but we are going to say DC is equal to 1 divided by 18 point times I plus 2, and then TW m dot, change any cycle and then DC. Do you see what I’m trying to do I’m trying to get it to smoothly go from 0 to 180 and then from 180 back to 0 and I’m, just not sure about the second for loop? But we can. We can give this a try. I’M, not sure. If you got to specify a step size on here or or Python, we’ll figure that out okay now I’m going to control X. No, yes, I want to yes save it, but I want to save it as servo, because I want to keep my first program.

Ok and then enter. Ah, yes, ok now let’s just look at it just to make sure. So how do we do that cat serve bowtie? Make sure that your servo 2 pi let’s just look at it. Ok that looks like it’s the new program so now we’re going to do a sudo python and then what’s the program this time so let’s see what happens all eyes on the servo. Okay, you know what I’m probably going too fast. We probably need to slow it down, so let’s go back to a nano servo to dot pod okay. So what we’re going to do here is we’re going to import time, okay and then down down here we got to give it a little time to settle out. Okay, so we’re going to do a crystal time: dot sleep of 0.1 seconds 0.05 let’s, see. If it can go that fast and then here we need to do the same thing. We need to do a time, dot sleep. When you do point 0.05, we might need to rest longer than that if this doesn’t work control – oh yes, control, X! Okay, now we are going to sudo python server, 2 pi all eyes on the servo. Okay, look at that there. It goes nice and smooth all the way over. Is it going to go back or is it going to crash okay? It crashed because I probably have to put a minus 1 in there for the kind of the step, and so let’s come back and again.

I am revealing my lack of freshness in some of these commands I’m just kind of going on my own here so comma, minus 1 let’s see if it’ll take that control, Oh to write out, enter in control X and let’s. Try running it again: pseudo Python server! Ok, ok, so now we’re going to go nice and smooth from 0 to 180 and aah. Oh, I must build something: ok, hopefully you guys caught that in a servo and look at that pwm Seibel I shouldn’t cycle control. Oh enter control X and we’re, going to run it again: sudo python, servo, hi. Okay. Now here we come from 0 to 180. Everybody is happy and will it turn around and go back? Oh yes, success! It is going to go forward in backwards and forwarding back that’s, alright, but it’s going to stop when it gets here. What, if we want to make it just continue to loop, what we want is we want something sorta, like the you remember, the old void loop. You kind of get a void loop for free in arduino. Here we’ve got to set one up, and so what I could do here is, I could say while and then I could just say while 1, because 1 is always equal to 1. So this is a while loop that’s going to loop forever, but now I’ve got to increase my in debt everywhere by 1, so that’s got to be indented.

That’S got to be indented right. All these clauses are defined by the indents, and so everything is defined by the indent, and so, if I’m, adding a loop, I have to add indention to all of these things. Ok, alright! So let’s look at this. The while loop starts here and then it ends here. This is the first command after the while loop, the for loop. The first one starts here, and this is the last indented. So all these are part of that for loop and then all of these are part of that for loop, and then these two for loops are indented from. There seem kind of got nested indentation, so let’s look at control o to write out, enter and now control X and now let’s. Do it servo nano servo to dot PI? Will it work? It should just keep going back and forth nice and smooth oops sudo python. Servo to PI that runs it all eyes on the servo boom here, it’s coming from 0 to 180, see how nice that is, and now it’s going to go back nice and easy nice and easy it goes back, and then it goes forwards. It’S going to keep running it’s going to keep running isn’t that great I wish I could make it go a little faster now. If I make it go too fast, it’s going to really mess up but I’m a risk taker, so we’re going to see. If we can make it go faster, so I’m, going to ctrl C to stop the program.

Okay, now we’re, going to edit the program again and let’s, see if we can make it go like about twice as fast, so we’re going to stop a smaller one. Oh 2.0 to control Oh control, enter and then control X now let’s, look at it pseudo Python, server! Whoa! Look at that thing! Go look at it go! That is a thing that’s, a beautiful thing to behold doing exactly what we wanted. I wish it was going. A little faster control C, you know what we’re going to do now, we’re just going to edit it okay, I was going to input the speed, but I won’t put you through that. I won’t put you through that. You know where you would input how fast you want it to go, but that will be for another day, let’s see if we can still make it go twice as fast. Okay, ctrl o enter ctrl X. Ok, now let’s go sudo python, servo whoa. Look at that thing, it’s flying it’ll. I I actually probably would not try to make it go much faster than this. You notice that I’m getting the full extent of motion and it’s it’s operating smoothly, but I would probably not try to make it run any faster than that. Ok guys. This has been a great lesson. You now can control servo using Python and the Raspberry Pi. The great thing is: is that you’ve got a whole bunch of GPIO pins that you can use, you could control something that was walking around.

You can really go in and start doing things now. The secret to robotics is being able to do servo control and you have got full control of your servo now using Python thanks to top tech boy, comm, palma quarter, if you like this, give us a thumbs up. Think about sharing the video maybe subscribe to us. We will see you guys later in lesson.

 
 

Paul McWhorter arduino lesson 28 Video

 

 

 

 

 

 

Paul McWhorter arduino lesson 28 news

 

 

     

     

     

     

     

    Paul McWhorter arduino lesson 28 Social

     

     

     

     

    official.arduino
    2019-10-28T18:46:20+0000

    We’re opening the Arduino IoT Cloud to other platforms, starting with the ESP8266 by Espressif Systems — NodeMCU, SparkFun’s ESP Thing, ESPDuino, and Wemos (to name a few) — along with other inexpensive, commercially available plugs and switches based on this module.
    safe image.php?d=AQB6YIkvOaOwGAQg&w=720&h=720&url=https%3A%2F%2Fblog.arduino.cc%2Fwp content%2Fuploads%2F2019%2F10%2FDSC00029 - Paul McWhorter arduino lesson 28
    official.arduino
    2019-10-28T17:47:41+0000

    How fast can you run the 40-yard dash? Find out with your own wireless timing gate system.
    safe image.php?d=AQDAnlSUYJivJY5S&w=720&h=720&url=https%3A%2F%2Fhackster.imgix.net%2Fuploads%2Fattachments%2F1008638%2Fblob mOWHBn2WBw - Paul McWhorter arduino lesson 28

     

     

    (Visited 1 times, 1 visits today)

    About The Author

    You might be interested in

    Comment (20)

    1. I get that it starts at 0 degrees and goes to 180 and back, but how do i change it to start at 180 and go to 0 degrees and back?

    2. This was very informative. Thanks.
      I’ve started a project to build my own gimbal for which the PWM explanation and this video will be very helpful..

      Thanks

    3. Thanks for an excellent video on controlling servo motors. As a result of your video, my comfort working with the RPi and servo motors has increased tremendously. Keep up the good work.

    4. Hello I have a question, I am using similar code for servo motor, but I want to use distance sensor with it. So I wrote a code, for servo motor to move when distance sensor senses some specific distance. But in my code, servo motor is continuously moving and not coming to the rest position. Is there any way you can help me with this please? Is there any way I can share my code with you so that you can look at it, and let me know what is going on. Thank you for your help in advance.

    5. Hey I’m trying to run it on with a separate power supply of 4.8 volts and it won’t run. Is that the issue?

      1. PI draws quiet a bit of power so make sure you have suitable current supply. Some external power supplies are not strong enough

    6. Thanks Sir Paul – you have surely add to my insight how to control electronics using software – really appreciated.Your pace allows beginners to understand better and you explain stuff nicely.Thanks so much again – I have a pi but I guess i need to get a servo and a external power supply to try this out.

    7. def analysis(){
      if (content == awesome) {
      subscriber++ ;
      print(“You’ve got a new subscriber!”)
      }
      }
      analysis();
      console: You’ve got a new subscriber!

    LEAVE YOUR COMMENT

    Your email address will not be published. Required fields are marked *