Writing about Using Python to control a servo for educational and informational purposes only. however, do not hesitate to use this information on your own risk as we make no warranty of any kind.
How to control a servo using Raspberry Pi and Python The wiring
First let’s understand how the wiring works. In this example I’m using the SG90 mini servo, but this could apply to any servo in general.
Servo comes with 3 wires:
Black, which is ground, I’m using Pin 6 here.
Red and this is your 3.3 V from the Raspberry Pi, I’m using Pin 1 for this example.
Yellow or Orange sometimes, this is the data cable and need a PWM GPI Pin such as Pin 22 which I’m using in this example.
You can add a resistor for extra safety between the pin and the data cable, but I skipped it here since the goal is to keep it simple.
After you complete the wiring, run the following code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT)
TXP = GPIO.PWM(22, 60)
TXP.start(1)
i = 1
try:
while True:
TXP.ChangeDutyCycle(i)
time.sleep(0.25)
i = i + 1
if i > 9:
i = i - 9
except KeyboardInterrupt:
TXP.stop()
GPIO.cleanup()
All content and information on this web site is intended for educational and entertainment purposes only.