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

preview_player
Показать описание
In this lesson we present a simple step-by-step tutorial on controlling a servo with the Raspberry Pi. The lesson shows how to accurately achieve any desired position.

Full lesson details at:
Рекомендации по теме
Комментарии
Автор

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

mbv
Автор

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

martinlewis
Автор

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.

Lecon
Автор

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

albireo
Автор

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

berndklee
Автор

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.

johncgibson
Автор

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.

resrussia
Автор

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

adamc
Автор

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

Tyrad
Автор

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!

MartinJohnstone
Автор

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.

rajcodes
Автор

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

shampavmancg
Автор

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

prathamva
Автор

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

jatinmayekar
Автор

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!

mikehauk
Автор

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

erygion
Автор

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

aynursunagatullin
Автор

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

jingyiwu
Автор

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

anarchylabs
Автор

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

Fiskers
join shbcf.ru