Servo motors are a type of motor that don't spin around in a complete circle. It is used to move in precise rotational or angular motion by convering the appropriate electical signals.
Servo motors are controlled using pulse width modulation (PWM) signals. The control signal consists of a repeating pulse with a variable width. The width of the pulse determines the desired position of the servo motor. By varying the pulse width, the servo motor can be positioned at specific angles within its range of motion.
- Tower Servo Motor
- 3 - Female to male connectors
Note that Board numbering is used, NOT Broadcom numbering
Refer to this diagram to understand the pins we are using
Note that Board numbering is used NOT Broadcom numbering
This implimentation prompts the user for a duty cycle to move the servo motor by. The duty cycle parameters for 00 to 1800 is 2 < x < 12 where x is the duty cycle.
PWM.
# The code for a servo motor
import RPi.GPIO as GPIO
from time import sleep
# Board Setup
GPIO.setmode(GPIO.BOARD)
# Servo pin
servo = 12
GPIO.setup(servo, GPIO.OUT)
servoCtrl = GPIO.PWM(servo, 50) # Servo control object running at 50Hz
servoCtrl.start(0)
try:
print("<ctrl> + c to exit ")
while True:
dutyCycle = float(input("Enter Servo duty cycle: "))
servoCtrl.ChangeDutyCycle(dutyCycle)
except KeyboardInterrupt:
GPIO.cleanup()
print("\nExiting...\n")
The code. link