First, we should import the ServoRPi.servo library. We should also import the RPi.GPIO as you will need it to control the servo.
import ServoRPi.servo as servo
import RPi.GPIO as GPIO
Next, we will set up the output pin type (.BOARD or .BCM). In our case, it is .BOARD
GPIO.setmode(GPIO.BOARD)
Now, we will set up the pin as an output
GPIO.setup(11, GPIO.OUT)
We will now give the pin as the PWM pin and name it servo1, pin no. as 11, and frequency as 50 hertz (most servos are 50 hertz)
servo1 = GPIO.PWM(11, 50)
Now let’s start the servo and instead of the number, we will write the function servo.startposition(<angle>) where <angle>will be the angle from 0 to 180. So the line will be servo1.start(servo.startposition(x))
servo1.start(startposition(0))
Then we will now change the direction of the servo by we will write the line servo1.ChangeDutyCycle() where the is the function – servo1.write() and where is the angle you want to output. So the whole line is now –