Today I decided that I would get into Raspberry Pi and I thought i'd do a search and came upon this lesson. I used to have fun with turbo languages and was pretty upset when MSDOS went away. I haven't done any code since 99. This may be the beginning of a new hobby for me. Thank you.
Paul: Thank you for the great video! Guys: I added Python code to display DutyCycle values onscreen and to make a clean departure on Ctrl-X. The code below is calibrated for my particular servo. # Python code for Raspberry Pi: Rotate servo horn from 0 to 180 degrees; 180 to 0 degrees forever (Ctrl-X to stop) import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) # That is BCM pin number servoPin=18 GPIO.setup(servoPin,GPIO.OUT) pwm=GPIO.PWM(servoPin,50) # BCM pin 18 at 50Hz frequency pwm.start(2) # DutyCycle 2 is "full-left" position (0 degrees, if you will) on my servo try: while(1): for i in range(0,180): DC=1./18.*(i)+2 pwm.ChangeDutyCycle(DC) print("A:DutyCycle: %s" % DC) # Display DC values onscreen time.sleep(.01) for i in range(180,0,-1): DC=1/18.*i+2 pwm.ChangeDutyCycle(DC) print('\033[1;36mB:DutyCycle: %s\033[1;m' % DC) # Display cyan-colored DC values onscreen time.sleep(.01) except KeyboardInterrupt: # Clean departure on Ctrl-X pwm.stop() # Stop the servo GPIO.cleanup() # Housekeeping :)
Your PWM library is very good and stable. I have seen other people's programming that the servo arm is always twitching. Your servo control is very precise and no twitching at all.
Thank you so so much for your videos! There are so many others, but either they're only 2 minutes long (which is clearly not enough to explain such a complex thing), or they take about an hour, but do not manage to explain it anyway! And this is why I normally don't watch those videos from the beginning to the end, but YOUR Videos are simply worth the time! I owe you a lot, thank you 👍 Greetings from Germany! 👋
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.
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.
Awesome clarification of PWM! Man, I've been looking for a lesson like this! Very clear and concise, I checked out lesson 27 - great clarification! Thank you!
I will admit... frustration doesnt come close to how I was feeling... I sat back and told myself, "your overthinking everything. Let the pi do the work..." WORKS like a charm!
Thank you for the Amazing tutorial.I wish i had Mathematics Professor like you.The Math logic you used in the program made me to Realize the importance of" Lines and Angles" :)
When you calculate "DC" and multiply 1./18. to make it (float) the variable must also be a float, input must be a float also. float(input("text") this is so you multiply Float times Float. See what I mean... Saw this error while running "Thonny Pi IDE" Loved the lesson. John
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
Greetings and thanks much for sharing Paul. Very clear, easy to follow and here's wishing you and family a very happy new year! Here's to making it a great one! Kind regards.
Thanks for posting this video. It explained a lot. I do most of my stuff with Pi Zero's. So, from gpiozero I was able to import PWMOutputDevice and use (and understand what I was doing) to control a servo. Thanks!
Very good video. I will have to watch the entire series. Could you tell me the purpose of a PWM driver? I recently purchased a PI robot kit and it has a 16 channel 12-bit PWM driver.
Great video - any viewing/reading suggestions on how to move the servo motor based on a tensorflow lite object detection results on a Pi. For example, move it 30 degrees if object detection is apple and move it 160 degrees if banana? Thanks!
great video and explanations but, I noticed on the previous video that the pwn generated by the pi is not that stable. I tried this same code on my servo (futaba s3003) and I am having problems with "vibrations". How is it possible? Like, how do you keep your motor on the right position with an unstable pwm? Thanks for the help.
please would you make a video where one can control more more than 1, forexample 3 servo motors with Raspberry pi. so in an application if the amount of rotation for motor 1 is 1 full cycle, then 2 full cycles for motor 2, and 3 full cycles for motor 3. i really love you teaching method, it is so motivating and easy to follow.
I am a rookie in programming and I would like to know how to drive servos with RPi using C. How does a code for this look like? Thank you very much. Love your videos!
Thanks for a Great tutorial, Best I've found so far, your enthusiasm really shines through and helped me no end with the "Scary maths". One thing I found is that my servo was quite "Twitchy" when it was supposed to be at rest, can we have a tutorial on the Pi's hardware PWM pin please
mike white Discovered Most of my "Twitchy judders" was due to a loose jumper wire on the GPIO pin, pinched them all a little tighter but yet to reassemble and test again, fairly confidant as worked Ok when I held the wires in contact, Would Still Love a tutorial on the Pi's PWM hardware channel though ;)
Can you turn any servo 180 degrees like that? With my normal RC radios I can get my standard servos to turn about 90 degrees total. Is that because these standard radios only send a limited range of PWM signals? Or are you using a special servo that turns further? Thanks in advance!
Great Instructor! My servo is jerking a little bit when I stop it at any given angle. I'm afraid that the PI isn't putting out a perfect 50MHz. Everything working as it should.
Thanx you explained it verry well, just two question: how do I connect a second servo and give specific instructions to each servo individualy (is that easy or do I really have to buy a Servo Board?) Oh and olso on what site do I find the same servo as yours?
Thank you! I have a question. I want the same thing (servo go down and up). It goes down but it does not go up. For the down: duty = 2 while duty = 2: p.ChangeDutyCycle(duty) time.sleep(1) duty = duty - 1 Could you look at this, please? Where is the problem?
Hi, great and useful lesson. I want to control a steeper motor using raspberry Pi3 but before doing that I want to simulate the schematic of Raspberry+driver motor+Steeper motor. Is that possible? if yes which software is adequate?
Varkey George You should be able to control multiple servos by using different GPIO pins. Just remember that servos require lots of current so I would not try and power them from the Pi . . . just control them from the Pi.
my servo needs more power. I am connecting an external power souce to power the servo but it's going haywire. I would like to know how would you connect a common ground.
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.
Hi Paul you sead it wright the world at our finger tips learning Linux arduino Raspberrypi. Now to touch the stars with a high altitude baloon. THANK YOU wa6mqz 73's
Great tutorial, but i don't understand one thing, i was under the impression that PWM controls the power strength as demonstrated in previous lesson with the LED, how is it able to actually control which position to go to in here? thanks
+Haider A PWM means Pulse Width Modulation, and it controls the duty cycle of a pulse train. In effect, half power is full voltage on for half time. Full power is full voltage on for the full time cycle.
Your tutorial helped in understanding how servo works and control it by doing simple math. guess work would have taken ages to figure out the right configuration. Thank you. Keep up the good work :)
i have a parallax hb 25 motor controller which says it is to be coded as a continuous rotation servo but i am having difficulties trying to get it to work with my pi
why am i not able to move my servo motor connected to raspberry to rotate at different angle in spite of correct code as directed by you and i have also installed servoblaster please give me solution what to do??
First off im not even close to a programmer but i am a computer and electronic tech. What i need is this raspberry to take an input (3-5 volts) at random from an outside source and respond by making the servo move 90 degrees left or right and when the input stops the servo goes back to 0. Can that be done?
Hi, I don't know why the program does not work in my Pi and my servo, I ve checked the pins blinking a led, I downloaded the GPIO libraries, neither the lesson 27 worked for me, I checked the servos in arduino, I know that the problem is the pwm, but I don't know how to fix it or what Im doing wrong. Very good vids, grettings from Mexico
I'm trying to control the servo with Raspberry Pi but my servo is resting in a stable position. I'm seeing some glitch like a random pulse. What would be the reason for this?
I'm having issues with servo-2.py. I followed everything the video said but I can't do the 180 degree turn. I get an error message File "servo-2.py", line 10, in time.sleep(.05) I've been following all the tutorials but I can't get to work. I have a Rpi ver 1 Model B. what am I doing wrong? please advise...
I'm jumping back and forth between the newer Python tutorials and these. Lots of fun. Thanks again!
6 years later and this is still a great tutorial. Thank you Paul
Glad it was helpful!
Today I decided that I would get into Raspberry Pi and I thought i'd do a search and came upon this lesson. I used to have fun with turbo languages and was pretty upset when MSDOS went away. I haven't done any code since 99. This may be the beginning of a new hobby for me. Thank you.
Paul: Thank you for the great video!
Guys: I added Python code to display DutyCycle values onscreen and to make a clean departure on Ctrl-X. The code below is calibrated for my particular servo.
# Python code for Raspberry Pi: Rotate servo horn from 0 to 180 degrees; 180 to 0 degrees forever (Ctrl-X to stop)
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM) # That is BCM pin number
servoPin=18
GPIO.setup(servoPin,GPIO.OUT)
pwm=GPIO.PWM(servoPin,50) # BCM pin 18 at 50Hz frequency
pwm.start(2) # DutyCycle 2 is "full-left" position (0 degrees, if you will) on my servo
try:
while(1):
for i in range(0,180):
DC=1./18.*(i)+2
pwm.ChangeDutyCycle(DC)
print("A:DutyCycle: %s" % DC) # Display DC values onscreen
time.sleep(.01)
for i in range(180,0,-1):
DC=1/18.*i+2
pwm.ChangeDutyCycle(DC)
print('\033[1;36mB:DutyCycle: %s\033[1;m' % DC) # Display cyan-colored DC values onscreen
time.sleep(.01)
except KeyboardInterrupt: # Clean departure on Ctrl-X
pwm.stop() # Stop the servo
GPIO.cleanup() # Housekeeping :)
Your PWM library is very good and stable. I have seen other people's programming that the servo arm is always twitching. Your servo control is very precise and no twitching at all.
Thank you so so much for your videos! There are so many others, but either they're only 2 minutes long (which is clearly not enough to explain such a complex thing), or they take about an hour, but do not manage to explain it anyway! And this is why I normally don't watch those videos from the beginning to the end, but YOUR Videos are simply worth the time! I owe you a lot, thank you 👍 Greetings from Germany! 👋
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.
This lesson helps me understand how to start coding small things its awesome. You knowledge helps a lot . Big Up
Glad to hear that!
FINALLY A DECENT TUTORIAL ON THIS
thank you for these tutorials. still incredibly helpful and relevant 4+ years later
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.
Awesome clarification of PWM! Man, I've been looking for a lesson like this! Very clear and concise, I checked out lesson 27 - great clarification! Thank you!
I will admit... frustration doesnt come close to how I was feeling...
I sat back and told myself, "your overthinking everything. Let the pi do the work..."
WORKS like a charm!
Wonderful lesson Mr. Mcwhorter, (I love the long ones) all the possibilities with the other lessons makes quite the tool set. Thank you sir. ❤🍇🥧
Superior!!! All the control that you can imagine for control a servo!!!!
Thanks a lot sir!!Wish we had teachers like you
Thank you Paul you are the BEST!
Thank you for the Amazing tutorial.I wish i had Mathematics Professor like you.The Math logic you used in the program made me to Realize the importance of" Lines and Angles" :)
When you calculate "DC" and multiply 1./18. to make it (float) the variable must also be a float, input must be a float also. float(input("text") this is so you multiply Float times Float. See what I mean... Saw this error while running "Thonny Pi IDE" Loved the lesson. John
you are my hero, the most inteligent maker in youtube
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
I really liked your explanation especially the formulae, theory and code
Just to keep a good video going. the 9g servo stats are: full left = 2. mid = 7. full right =12
very ,,,,VERY good..... the real lesson....is like a school .... THANKS
Greetings and thanks much for sharing Paul. Very clear, easy to follow and here's wishing you and family a very happy new year! Here's to making it a great one! Kind regards.
Thank u i am stucked at this point but you solved it. Thank u
very nice and understandable lecture, I love all your PI projects. Thanks Mr. Paul.
Thank you for sharing your knowledge!! great lessens
Glad it was helpful!
This great video helped me a lot. Thank you very much!!
Glad it helped!
Thanks for posting this video. It explained a lot. I do most of my stuff with Pi Zero's. So, from gpiozero I was able to import PWMOutputDevice and use (and understand what I was doing) to control a servo. Thanks!
you're awesome i didn't understood it but now i understood. Good Job! Keep it up. Thank you
This is great! Many thanks from Pakistan!
Thanks From Chile
you are a great Teacher !
Greetings
great instructor atuk! thanks!
U R a great teacher!
def analysis(){
if (content == awesome) {
subscriber++ ;
print("You've got a new subscriber!")
}
}
analysis();
console: You've got a new subscriber!
Great tutorials, Mr. McWhorter. Thanks for sharing your knowledge!
Works with zero problem. Ah, but servo motor is getting pretty hot. Great video.
Problem was solved when I change the power supply from supplied to one of those power bank which supplies 2.1A at 1.5v
This is so helpful for learning python
great thorough explanations
your tutorials are awesome thank you very much for helping me out (:
Great explanation man, good job
Thank you so much for this! Seriously saved my skin!
you`re the best! good work and thx for your lessons!
Very good video. I will have to watch the entire series. Could you tell me the purpose of a PWM driver? I recently purchased a PI robot kit and it has a 16 channel 12-bit PWM driver.
Great video - any viewing/reading suggestions on how to move the servo motor based on a tensorflow lite object detection results on a Pi. For example, move it 30 degrees if object detection is apple and move it 160 degrees if banana? Thanks!
great video and explanations but, I noticed on the previous video that the pwn generated by the pi is not that stable. I tried this same code on my servo (futaba s3003) and I am having problems with "vibrations". How is it possible? Like, how do you keep your motor on the right position with an unstable pwm? Thanks for the help.
This is the best! Many thanks. I'm now attending your Pi U. I was looking for the code for this is it available? (Looked online and on your site)
Thank you Sir its really very helpfull keep it up......
Love From Pakistan
please would you make a video where one can control more more than 1, forexample 3 servo motors with Raspberry pi. so in an application if the amount of rotation for motor 1 is 1 full cycle, then 2 full cycles for motor 2, and 3 full cycles for motor 3.
i really love you teaching method, it is so motivating and easy to follow.
Grate job, perfect.
I am a rookie in programming and I would like to know how to drive servos with RPi using C. How does a code for this look like?
Thank you very much. Love your videos!
Thanks for a Great tutorial, Best I've found so far, your enthusiasm really shines through and helped me no end with the "Scary maths". One thing I found is that my servo was quite "Twitchy" when it was supposed to be at rest, can we have a tutorial on the Pi's hardware PWM pin please
mike white Discovered Most of my "Twitchy judders" was due to a loose jumper wire on the GPIO pin, pinched them all a little tighter but yet to reassemble and test again, fairly confidant as worked Ok when I held the wires in contact, Would Still Love a tutorial on the Pi's PWM hardware channel though ;)
This is awesome ..100 Thumbs to you !!!
Thank you very much! Would some similar be possible with a large motor aswell?
Paul, I am writing a simple script to move a servo from 0-90 degrees and was wondering if there is a way to slow down the speed of the servo?
Can you turn any servo 180 degrees like that?
With my normal RC radios I can get my standard servos to turn about 90 degrees total. Is that because these standard radios only send a limited range of PWM signals? Or are you using a special servo that turns further?
Thanks in advance!
How to do with continuous servo motor?
Great Instructor! My servo is jerking a little bit when I stop it at any given angle. I'm afraid that the PI isn't putting out a perfect 50MHz. Everything working as it should.
Make sure you have a good servo, not one of the cheep ones. Then also make sure you power supply is sourcing adequate curring.
50MHz for a servo cycle?! Also, RPi.GPIO library has noticeable jitter when PWM'ing. Maybe a better PWM library for python on Pi?
Nice tutorial, very helpful, easy to understand, thanks a lot! :)
What a nice video.
thank you
That's great 👍👍
Thanx you explained it verry well, just two question: how do I connect a second servo and give specific instructions to each servo individualy (is that easy or do I really have to buy a Servo Board?) Oh and olso on what site do I find the same servo as yours?
Thank you! I have a question. I want the same thing (servo go down and up). It goes down but it does not go up. For the down:
duty = 2
while duty = 2:
p.ChangeDutyCycle(duty)
time.sleep(1)
duty = duty - 1
Could you look at this, please? Where is the problem?
Is there a way to have the servo report its position back to you? Or would that depend on the type of servo?
Hi, great and useful lesson.
I want to control a steeper motor using raspberry Pi3 but before doing that I want to simulate the schematic of Raspberry+driver motor+Steeper motor. Is that possible? if yes which software is adequate?
Never mind, I forgot to add
import time. : )
Freaking awesome, thanks mate.
Great tutorial . Is i possible to conrol multiple servos using GPIO ?
Varkey George You should be able to control multiple servos by using different GPIO pins. Just remember that servos require lots of current so I would not try and power them from the Pi . . . just control them from the Pi.
my servo needs more power. I am connecting an external power souce to power the servo but it's going haywire. I would like to know how would you connect a common ground.
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.
Hi Paul
you sead it wright the world at our finger tips learning Linux arduino Raspberrypi.
Now to touch the stars with a high altitude baloon.
THANK YOU wa6mqz 73's
what is the maximum torque this thing can output? i would like to use it to flip a switch on a power strip
It would be great if you uploaded the servo document to a shared public folder and gave a link for downloading.
Hi from Spain. Do you have any lesson about controlling servo with DMA?
Hello Paul, Nice tutorial. I had a question for you how to make a servo motor to set on middle (90 D)
hi the question is, are you using rasbian?
Great tutorial, but i don't understand one thing, i was under the impression that PWM controls the power strength as demonstrated in previous lesson with the LED, how is it able to actually control which position to go to in here? thanks
+Haider A PWM means Pulse Width Modulation, and it controls the duty cycle of a pulse train. In effect, half power is full voltage on for half time. Full power is full voltage on for the full time cycle.
wonderful.
Your tutorial helped in understanding how servo works and control it by doing simple math. guess work would have taken ages to figure out the right configuration. Thank you. Keep up the good work :)
i have a parallax hb 25 motor controller which says it is to be coded as a continuous rotation servo but i am having difficulties trying to get it to work with my pi
which software u r using to record your screen and face is it free
any ideas on controlling a continuous servo with a feedback wire?
why am i not able to move my servo motor connected to raspberry to rotate at different angle in spite of correct code as directed by you and
i have also installed servoblaster
please give me solution what to do??
First off im not even close to a programmer but i am a computer and electronic tech. What i need is this raspberry to take an input (3-5 volts) at random from an outside source and respond by making the servo move 90 degrees left or right and when the input stops the servo goes back to 0. Can that be done?
build a circuit based around a transistor that flicks a "switch" and thus turning on a program that would move the servo
Thanks for the great explanation 🫡👍🏻
Hi, I don't know why the program does not work in my Pi and my servo, I ve checked the pins blinking a led, I downloaded the GPIO libraries, neither the lesson 27 worked for me, I checked the servos in arduino, I know that the problem is the pwm, but I don't know how to fix it or what Im doing wrong. Very good vids, grettings from Mexico
I'm trying to control multiple servos at the same time with my Raspberry Pi. Can someone direct me to a proper video/post of how I can do this?
Penguin individually or collectively?
use threading
everything works fine in python interpreter but once I put it a script nothing works. I even used sudo python servo.py.
I'm trying to control the servo with Raspberry Pi but my servo is resting in a stable position. I'm seeing some glitch like a random pulse.
What would be the reason for this?
I'm having issues with servo-2.py. I followed everything the video said but I can't do the 180 degree turn. I get an error message
File "servo-2.py", line 10, in
time.sleep(.05)
I've been following all the tutorials but I can't get to work. I have a Rpi ver 1 Model B. what am I doing wrong? please advise...
Put your cleanup call both at the beginning of your python script and at the end.
If you break your script it will not give you that warning.
thank you
Any direction helping with using C++ as a language to program?
MASTER!
Thanks sir
CMIIW, It can be concluded that the servo can be controled because its 3rd cable is connected into raspberry GPIO, am i right ?
Can you please do a lesson about stepper motor and raspberry pie. Please !!
Is there a way I can get the rust code for this please?
thanks
How would you add a potentiometer to control the duty cycle?