Now that our servo is connected to our Raspberry Pi, let's write some code at the command line to control it. We will use the Raspberry Pi Python library GPIO Zero
to do this.
Load up Thonny and click on Shell
:
Type the following in the shell:
from gpiozero import Servo
After a short delay, the cursor should return. What we have done here is load the servo
object from gpiozero
into memory. We will assign pin GPIO 17
with the following statement:
servo = Servo(17)
We will now move the servo motor to the minimum (min
) position. Type the following into the command line:
servo.min()
You should hear the servo motor moving, and the needle will go to its farthest position (if it is not already there).
Let's move the servo motor to the maximum ( max
) position with the following command:
servo.max()
Now, move the servo to the middle ( mid
) position with the following command:
servo.mid()
The servo motor should move to its middle position.
When you place your hand over...