Raspberry Pi LESSON 28. Controlling a Servo with Raspberry Pi and Python

  Рет қаралды 164,507

Paul McWhorter

Paul McWhorter

Күн бұрын

Пікірлер: 150
@mbv41
@mbv41 3 жыл бұрын
6 years later and this is still a great tutorial. Thank you Paul
@paulmcwhorter
@paulmcwhorter 3 жыл бұрын
Glad it was helpful!
@Lecon60
@Lecon60 8 жыл бұрын
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.
@albireo9
@albireo9 8 жыл бұрын
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 :)
@martinlewis645
@martinlewis645 3 жыл бұрын
I'm jumping back and forth between the newer Python tutorials and these. Lots of fun. Thanks again!
@berndklee5561
@berndklee5561 8 жыл бұрын
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! 👋
@johncgibson4720
@johncgibson4720 8 жыл бұрын
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.
@resrussia
@resrussia 6 жыл бұрын
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.
@mikehauk6079
@mikehauk6079 7 жыл бұрын
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!
@MartinJohnstone
@MartinJohnstone 4 жыл бұрын
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!
@Ty2rad
@Ty2rad 4 жыл бұрын
thank you for these tutorials. still incredibly helpful and relevant 4+ years later
@rajcodes100
@rajcodes100 5 жыл бұрын
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.
@nimfbdhdkdm
@nimfbdhdkdm 3 жыл бұрын
FINALLY A DECENT TUTORIAL ON THIS
@adamc2787
@adamc2787 3 жыл бұрын
This lesson helps me understand how to start coding small things its awesome. You knowledge helps a lot . Big Up
@paulmcwhorter
@paulmcwhorter 3 жыл бұрын
Glad to hear that!
@michaelnoardo3315
@michaelnoardo3315 4 жыл бұрын
you are my hero, the most inteligent maker in youtube
@anarchylabs9997
@anarchylabs9997 5 жыл бұрын
Just to keep a good video going. the 9g servo stats are: full left = 2. mid = 7. full right =12
@jatinmayekar3779
@jatinmayekar3779 8 жыл бұрын
Thanks a lot sir!!Wish we had teachers like you
@jingyiwu7746
@jingyiwu7746 7 жыл бұрын
Superior!!! All the control that you can imagine for control a servo!!!!
@prathamva7392
@prathamva7392 8 жыл бұрын
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" :)
@Fiskers22
@Fiskers22 4 жыл бұрын
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
@LAStupidDrivers
@LAStupidDrivers 8 жыл бұрын
How to do with continuous servo motor?
@aynursunagatullin7029
@aynursunagatullin7029 4 жыл бұрын
I really liked your explanation especially the formulae, theory and code
@shampavmancg
@shampavmancg 6 жыл бұрын
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
@sskey89
@sskey89 8 жыл бұрын
Is there a way to have the servo report its position back to you? Or would that depend on the type of servo?
@minhtruong9408
@minhtruong9408 2 жыл бұрын
Thank you Paul you are the BEST!
@rob26r
@rob26r 4 жыл бұрын
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!
@auke1260
@auke1260 4 жыл бұрын
Thank you very much! Would some similar be possible with a large motor aswell?
@erygion
@erygion 5 жыл бұрын
Wonderful lesson Mr. Mcwhorter, (I love the long ones) all the possibilities with the other lessons makes quite the tool set. Thank you sir. ❤🍇🥧
@mirak1470
@mirak1470 7 жыл бұрын
very ,,,,VERY good..... the real lesson....is like a school .... THANKS
@fernandowosniak8782
@fernandowosniak8782 8 жыл бұрын
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.
@TheNewfiePilot
@TheNewfiePilot 5 жыл бұрын
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?
@ADJai
@ADJai 7 жыл бұрын
very nice and understandable lecture, I love all your PI projects. Thanks Mr. Paul.
@Chretze
@Chretze 4 жыл бұрын
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!
@LucaPR0
@LucaPR0 6 жыл бұрын
what is the maximum torque this thing can output? i would like to use it to flip a switch on a power strip
@muhammadalam2498
@muhammadalam2498 5 жыл бұрын
This is great! Many thanks from Pakistan!
@manishasonar1733
@manishasonar1733 3 жыл бұрын
Thank u i am stucked at this point but you solved it. Thank u
@schwarz500
@schwarz500 8 жыл бұрын
you're awesome i didn't understood it but now i understood. Good Job! Keep it up. Thank you
@1412dante
@1412dante 8 жыл бұрын
great instructor atuk! thanks!
@SanjivVenkatram
@SanjivVenkatram 5 жыл бұрын
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.
@Cobacanal
@Cobacanal 9 жыл бұрын
Thanks From Chile you are a great Teacher ! Greetings
@SonnyLando
@SonnyLando 5 жыл бұрын
any ideas on controlling a continuous servo with a feedback wire?
@adulthumor8203
@adulthumor8203 4 жыл бұрын
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.
@turboromy
@turboromy 7 жыл бұрын
Works with zero problem. Ah, but servo motor is getting pretty hot. Great video.
@turboromy
@turboromy 7 жыл бұрын
Problem was solved when I change the power supply from supplied to one of those power bank which supplies 2.1A at 1.5v
@CryhavocMike
@CryhavocMike 8 жыл бұрын
How would you add a potentiometer to control the duty cycle?
@alokmishra2458
@alokmishra2458 8 жыл бұрын
which software u r using to record your screen and face is it free
@WCollinsWhiteIII
@WCollinsWhiteIII 2 жыл бұрын
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)
@Sneaky1ne
@Sneaky1ne 4 жыл бұрын
Great explanation man, good job
@WarpDriveM
@WarpDriveM 8 жыл бұрын
you`re the best! good work and thx for your lessons!
@Mobi2525
@Mobi2525 7 жыл бұрын
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.
@dbhanderi1
@dbhanderi1 8 жыл бұрын
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
@CatGuyTX
@CatGuyTX 9 жыл бұрын
It would be great if you uploaded the servo document to a shared public folder and gave a link for downloading.
@ApplySkills
@ApplySkills 6 жыл бұрын
everything works fine in python interpreter but once I put it a script nothing works. I even used sudo python servo.py.
@AwakenedOne-qu
@AwakenedOne-qu 7 жыл бұрын
U R a great teacher!
@javierdarlington4858
@javierdarlington4858 6 жыл бұрын
Hi from Spain. Do you have any lesson about controlling servo with DMA?
@Qornv
@Qornv 6 жыл бұрын
great thorough explanations
@JooshyBaby
@JooshyBaby 7 жыл бұрын
Thank you so much for this! Seriously saved my skin!
@nathanielbajo130
@nathanielbajo130 3 жыл бұрын
Is there a way I can get the rust code for this please?
@varkeygeorge3209
@varkeygeorge3209 9 жыл бұрын
Great tutorial . Is i possible to conrol multiple servos using GPIO ?
@paulmcwhorter
@paulmcwhorter 9 жыл бұрын
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.
@cryptok1d726
@cryptok1d726 3 жыл бұрын
Thank you for sharing your knowledge!! great lessens
@paulmcwhorter
@paulmcwhorter 3 жыл бұрын
Glad it was helpful!
@christiangorman6058
@christiangorman6058 6 жыл бұрын
Any direction helping with using C++ as a language to program?
@saider895
@saider895 8 жыл бұрын
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
@paulmcwhorter
@paulmcwhorter 8 жыл бұрын
+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.
@spiderschwein3443
@spiderschwein3443 4 жыл бұрын
your tutorials are awesome thank you very much for helping me out (:
@Ruya_Meltem
@Ruya_Meltem 4 жыл бұрын
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?
@ExcellentEngineering1966
@ExcellentEngineering1966 6 жыл бұрын
This is so helpful for learning python
@ramanandkumar9052
@ramanandkumar9052 6 жыл бұрын
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??
@윤준영-g3g
@윤준영-g3g 2 жыл бұрын
This great video helped me a lot. Thank you very much!!
@paulmcwhorter
@paulmcwhorter 2 жыл бұрын
Glad it helped!
@sreelekha4727
@sreelekha4727 2 жыл бұрын
May I know the software used here?
@shawnerz98
@shawnerz98 7 жыл бұрын
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!
@asimkhan2557
@asimkhan2557 8 жыл бұрын
Thank you Sir its really very helpfull keep it up...... Love From Pakistan
@dexnaranjo3037
@dexnaranjo3037 8 жыл бұрын
Great tutorials, Mr. McWhorter. Thanks for sharing your knowledge!
@govindabaweja3549
@govindabaweja3549 6 жыл бұрын
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.
@WirelessDevelopment
@WirelessDevelopment 8 жыл бұрын
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.
@paulmcwhorter
@paulmcwhorter 8 жыл бұрын
Make sure you have a good servo, not one of the cheep ones. Then also make sure you power supply is sourcing adequate curring.
@RinksRides
@RinksRides 7 жыл бұрын
50MHz for a servo cycle?! Also, RPi.GPIO library has noticeable jitter when PWM'ing. Maybe a better PWM library for python on Pi?
@battery_solar_ev
@battery_solar_ev 7 жыл бұрын
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?
@Tomdavies2k11
@Tomdavies2k11 7 жыл бұрын
build a circuit based around a transistor that flicks a "switch" and thus turning on a program that would move the servo
@valentin68706
@valentin68706 8 жыл бұрын
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?
@fertorres330
@fertorres330 5 жыл бұрын
hi the question is, are you using rasbian?
@foreducationalpurposes7386
@foreducationalpurposes7386 Жыл бұрын
Can raspbery 3 model b carry 3 servoo +? Like that big?
@larbigueraiche6721
@larbigueraiche6721 6 жыл бұрын
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?
@arunbm123
@arunbm123 7 жыл бұрын
This is awesome ..100 Thumbs to you !!!
@susheelselvamani6664
@susheelselvamani6664 7 жыл бұрын
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?
@jamesaxiak8084
@jamesaxiak8084 6 жыл бұрын
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?
@RSuarez66
@RSuarez66 7 жыл бұрын
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!
@kunal_265
@kunal_265 7 жыл бұрын
why did you put -1 there in the second while loop
@jmdesros
@jmdesros 7 жыл бұрын
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.
@penguin5096
@penguin5096 7 жыл бұрын
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?
@chevycamaro-rp6tr
@chevycamaro-rp6tr 7 жыл бұрын
Penguin individually or collectively?
@Denverse
@Denverse 6 жыл бұрын
use threading
@bmoviestgams
@bmoviestgams 8 жыл бұрын
Can i use this way to controll a brushless motor ? (esc) Thanks btw !
@paulmcwhorter
@paulmcwhorter 8 жыл бұрын
+bmoviestgams To control a motor, you can to use some sort of motor driver between the pi and the motor. The pi does not provide the current needed for a motor.
@bmoviestgams
@bmoviestgams 8 жыл бұрын
Thanks for your reply. It has an esc between the raspberry and the motor. Im still searching if you can help me in a way that i can controll the motor through the esc or with servohat for the raspberry maybe. Thanks anyway any help will be acceptable.
@mateus10pras14
@mateus10pras14 5 жыл бұрын
My servo keeps rotating when i use the same code that you are using.
@aatifibrahimi8567
@aatifibrahimi8567 8 жыл бұрын
how can i generate a single clock pulse of 50 ms
@vardhamanvasawade2022
@vardhamanvasawade2022 2 жыл бұрын
Plz send the code for the 360
@pharrell6532
@pharrell6532 8 жыл бұрын
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
@iswoo10
@iswoo10 7 жыл бұрын
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...
@yuangao9731
@yuangao9731 8 жыл бұрын
why is my servo jitter?
@TheBml104
@TheBml104 8 жыл бұрын
+高原 Mine jitters until pwm.cleanup() runs
@mikewhite90
@mikewhite90 9 жыл бұрын
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
@mikewhite90
@mikewhite90 9 жыл бұрын
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 ;)
@georgebes3519
@georgebes3519 3 жыл бұрын
I want to control a servo from a remote control car, but it has five wires(brown, yellow, white, red, black). Which wire does what. Can someone help me please?
@AmirAli-yk4xb
@AmirAli-yk4xb 7 жыл бұрын
Hello Paul, Nice tutorial. I had a question for you how to make a servo motor to set on middle (90 D)
@invinity3982
@invinity3982 8 жыл бұрын
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.
@gabrieljulioarmandosiagian1921
@gabrieljulioarmandosiagian1921 4 жыл бұрын
CMIIW, It can be concluded that the servo can be controled because its 3rd cable is connected into raspberry GPIO, am i right ?
@iswoo10
@iswoo10 7 жыл бұрын
Never mind, I forgot to add import time. : )
@narayanasamythiruvengadam9155
@narayanasamythiruvengadam9155 7 жыл бұрын
Good Evening I want to use arduino as a Up counter with preset option using LCD. Any body help me to find out the suitable solution?.
@kalipc4910
@kalipc4910 4 жыл бұрын
Can you please do a lesson about stepper motor and raspberry pie. Please !!
@chrisklutts4096
@chrisklutts4096 5 жыл бұрын
def analysis(){ if (content == awesome) { subscriber++ ; print("You've got a new subscriber!") } } analysis(); console: You've got a new subscriber!
@freddyheryanto95
@freddyheryanto95 7 жыл бұрын
Nice tutorial, very helpful, easy to understand, thanks a lot! :)
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН
Raspberry Pi LESSON 19: Simple Control of Servo From Raspberry Pi
27:14
Raspberry Pi Servo Motor Control
17:36
ExplainingComputers
Рет қаралды 396 М.
Servo Motoring with the Raspberry Pi Pico and CircuitPython
10:49
Prof. John Gallaugher
Рет қаралды 2,9 М.
Raspberry Pi Linux LESSON 27: PWM Output on GPIO Pins from Python
39:36
The Best 20 Arduino Projects of the year 2024!
14:59
ToP Projects Compilation
Рет қаралды 117 М.
Raspberry Pi Servo Motor Control - No Jitter!
13:52
Gary Explains
Рет қаралды 56 М.
Programming an Arduino Robot with Python 🐍
8:30
YouMakeTech
Рет қаралды 127 М.
Raspberry Pi Stepper Motor Tutorial
27:32
rdagger68
Рет қаралды 503 М.
Servos on a Raspberry Pi - No Jitter!
15:18
Lori Pfahler
Рет қаралды 2,8 М.
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН