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

Поділитися
Вставка
  • Опубліковано 26 жов 2024

КОМЕНТАРІ • 150

  • @martinlewis645
    @martinlewis645 3 роки тому

    I'm jumping back and forth between the newer Python tutorials and these. Lots of fun. Thanks again!

  • @mbv41
    @mbv41 3 роки тому +2

    6 years later and this is still a great tutorial. Thank you Paul

  • @Lecon60
    @Lecon60 8 років тому +2

    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 7 років тому +3

    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 :)

  • @johncgibson4720
    @johncgibson4720 7 років тому +1

    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.

  • @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! 👋

  • @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.

  • @adamc2787
    @adamc2787 3 роки тому

    This lesson helps me understand how to start coding small things its awesome. You knowledge helps a lot . Big Up

  • @nimfbdhdkdm
    @nimfbdhdkdm 2 роки тому

    FINALLY A DECENT TUTORIAL ON THIS

  • @Ty2rad
    @Ty2rad 4 роки тому

    thank you for these tutorials. still incredibly helpful and relevant 4+ years later

  • @rajcodes100
    @rajcodes100 5 років тому +1

    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.

  • @MartinJohnstone
    @MartinJohnstone 4 роки тому +1

    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!

  • @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!

  • @erygion
    @erygion 5 років тому +1

    Wonderful lesson Mr. Mcwhorter, (I love the long ones) all the possibilities with the other lessons makes quite the tool set. Thank you sir. ❤🍇🥧

  • @jingyiwu7746
    @jingyiwu7746 7 років тому

    Superior!!! All the control that you can imagine for control a servo!!!!

  • @jatinmayekar3779
    @jatinmayekar3779 7 років тому

    Thanks a lot sir!!Wish we had teachers like you

  • @minhtruong9408
    @minhtruong9408 2 роки тому

    Thank you Paul you are the BEST!

  • @prathamva7392
    @prathamva7392 7 років тому

    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 роки тому +1

    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

  • @michaelnoardo3315
    @michaelnoardo3315 4 роки тому

    you are my hero, the most inteligent maker in youtube

  • @shampavmancg
    @shampavmancg 6 років тому +1

    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

  • @aynursunagatullin7029
    @aynursunagatullin7029 3 роки тому

    I really liked your explanation especially the formulae, theory and code

  • @anarchylabs9997
    @anarchylabs9997 5 років тому +2

    Just to keep a good video going. the 9g servo stats are: full left = 2. mid = 7. full right =12

  • @mirak1470
    @mirak1470 7 років тому +1

    very ,,,,VERY good..... the real lesson....is like a school .... THANKS

  • @SanjivVenkatram
    @SanjivVenkatram 4 роки тому

    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.

  • @manishasonar1733
    @manishasonar1733 3 роки тому

    Thank u i am stucked at this point but you solved it. Thank u

  • @ADJai
    @ADJai 6 років тому

    very nice and understandable lecture, I love all your PI projects. Thanks Mr. Paul.

  • @cryptok1d726
    @cryptok1d726 3 роки тому

    Thank you for sharing your knowledge!! great lessens

  • @윤준영-g3g
    @윤준영-g3g 2 роки тому

    This great video helped me a lot. Thank you very much!!

  • @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!

  • @schwarz500
    @schwarz500 8 років тому

    you're awesome i didn't understood it but now i understood. Good Job! Keep it up. Thank you

  • @muhammadalam2498
    @muhammadalam2498 5 років тому

    This is great! Many thanks from Pakistan!

  • @Cobacanal
    @Cobacanal 8 років тому

    Thanks From Chile
    you are a great Teacher !
    Greetings

  • @1412dante
    @1412dante 7 років тому

    great instructor atuk! thanks!

  • @AwakenedOne-qu
    @AwakenedOne-qu 7 років тому

    U R a great teacher!

  • @chrisklutts4096
    @chrisklutts4096 5 років тому +8

    def analysis(){
    if (content == awesome) {
    subscriber++ ;
    print("You've got a new subscriber!")
    }
    }
    analysis();
    console: You've got a new subscriber!

  • @dexnaranjo3037
    @dexnaranjo3037 7 років тому

    Great tutorials, Mr. McWhorter. Thanks for sharing your knowledge!

  • @turboromy
    @turboromy 7 років тому +1

    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

  • @ExcellentEngineering1966
    @ExcellentEngineering1966 6 років тому

    This is so helpful for learning python

  • @Qornv
    @Qornv 6 років тому

    great thorough explanations

  • @spiderschwein3443
    @spiderschwein3443 4 роки тому

    your tutorials are awesome thank you very much for helping me out (:

  • @Sneaky1ne
    @Sneaky1ne 4 роки тому

    Great explanation man, good job

  • @JooshyBaby
    @JooshyBaby 6 років тому

    Thank you so much for this! Seriously saved my skin!

  • @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.

  • @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!

  • @fernandowosniak8782
    @fernandowosniak8782 8 років тому +2

    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.

  • @WCollinsWhiteIII
    @WCollinsWhiteIII Рік тому

    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)

  • @asimkhan2557
    @asimkhan2557 8 років тому

    Thank you Sir its really very helpfull keep it up......
    Love From Pakistan

  • @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.

  •  3 роки тому

    Grate job, perfect.

  • @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!

  • @mikewhite90
    @mikewhite90 9 років тому +1

    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 років тому +2

      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 ;)

  • @arunbm123
    @arunbm123 6 років тому

    This is awesome ..100 Thumbs to you !!!

  • @auke1260
    @auke1260 4 роки тому

    Thank you very much! Would some similar be possible with a large motor aswell?

  • @TheNewfiePilot
    @TheNewfiePilot 4 роки тому

    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?

  • @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!

  • @LAStupidDrivers
    @LAStupidDrivers 8 років тому +3

    How to do with continuous servo motor?

  • @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 років тому +1

      50MHz for a servo cycle?! Also, RPi.GPIO library has noticeable jitter when PWM'ing. Maybe a better PWM library for python on Pi?

  • @freddyheryanto95
    @freddyheryanto95 7 років тому

    Nice tutorial, very helpful, easy to understand, thanks a lot! :)

  • @omarhoran2317
    @omarhoran2317 7 років тому

    What a nice video.
    thank you

  • @BiplobHossainSorker
    @BiplobHossainSorker 16 днів тому

    That's great 👍👍

  • @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?

  • @Ruya_Meltem
    @Ruya_Meltem 3 роки тому

    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?

  • @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?

  • @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?

  • @iswoo10
    @iswoo10 7 років тому

    Never mind, I forgot to add
    import time. : )

  • @cathyomalley2772
    @cathyomalley2772 6 років тому

    Freaking awesome, thanks mate.

  • @varkeygeorge3209
    @varkeygeorge3209 9 років тому

    Great tutorial . Is i possible to conrol multiple servos using GPIO ?

    • @paulmcwhorter
      @paulmcwhorter  9 років тому +1

      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.

  • @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.

  • @govindabaweja3549
    @govindabaweja3549 5 років тому

    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.

  • @dannymeadows5448
    @dannymeadows5448 6 років тому

    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

  • @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

  • @CatGuyTX
    @CatGuyTX 9 років тому

    It would be great if you uploaded the servo document to a shared public folder and gave a link for downloading.

  • @javierdarlington4858
    @javierdarlington4858 6 років тому

    Hi from Spain. Do you have any lesson about controlling servo with DMA?

  • @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)

  • @fertorres330
    @fertorres330 4 роки тому

    hi the question is, are you using rasbian?

  • @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 років тому +1

      +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.

  • @TechMedium
    @TechMedium 5 років тому

    wonderful.

    • @TechMedium
      @TechMedium 5 років тому

      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 :)

  • @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

  • @alokmishra2458
    @alokmishra2458 7 років тому

    which software u r using to record your screen and face is it free

  • @SonnyLando
    @SonnyLando 5 років тому

    any ideas on controlling a continuous servo with a feedback wire?

  • @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??

  • @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

  • @bubigany
    @bubigany 5 місяців тому

    Thanks for the great explanation 🫡👍🏻

  • @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

  • @penguin5096
    @penguin5096 7 років тому +4

    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?

  • @ApplySkills
    @ApplySkills 6 років тому +1

    everything works fine in python interpreter but once I put it a script nothing works. I even used sudo python servo.py.

  • @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?

  • @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...

  • @jmdesros
    @jmdesros 6 років тому +1

    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.

  • @petemitsch6346
    @petemitsch6346 7 років тому

    thank you

  • @christiangorman6058
    @christiangorman6058 6 років тому

    Any direction helping with using C++ as a language to program?

  • @gustavoamontante9134
    @gustavoamontante9134 7 років тому

    MASTER!

  • @khaledmohsen6087
    @khaledmohsen6087 5 років тому

    Thanks sir

  • @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 ?

  • @kalipc4910
    @kalipc4910 4 роки тому

    Can you please do a lesson about stepper motor and raspberry pie. Please !!

  • @nathanielbajo130
    @nathanielbajo130 3 роки тому

    Is there a way I can get the rust code for this please?

  • @parthusmeda3420
    @parthusmeda3420 5 років тому

    thanks

  • @CryhavocMike
    @CryhavocMike 8 років тому

    How would you add a potentiometer to control the duty cycle?