Servo: Servo Motor
This Python code demonstrates how to control a servo motor. The Arduino main control board is connected to a Windows or Linux computer, and a servo motor is connected to the D4 pin of the main control board.
# -*- coding: UTF-8 -*-
# Experiment effect: Servo control
# Connection: Connect an Arduino main control board to a Windows or Linux computer, and connect a servo to D4
import time
from pinpong.board import Board,Pin,Servo # Import Servo library
Board("uno").begin() # Initialization, select the board type (uno, microbit, RPi, handpy) and port number. If no port number is entered, automatic recognition will be performed
#Board("uno","COM36").begin() # Initialization by specifying the port under Windows
#Board("uno","/dev/ttyACM0").begin() # Initialization by specifying the port under Linux
#Board("uno","/dev/cu.usbmodem14101").begin() # Initialization by specifying the port under Mac
s1 = Servo(Pin(Pin.D4)) # Initialize the servo pin by passing Pin into Servo
while True:
#s1.angle(0) # Control the servo to turn to the 0 degree position Method 1
s1.write_angle(0) # Control the servo to turn to the 0 degree position Method 2
print("0")
time.sleep(1)
#s1.angle(90) # Control the servo to turn to the 90 degree position
s1.write_angle(90) # Control the servo to turn to the 90 degree position Method 2
print("90")
time.sleep(1)
#s1.angle(180) # Control the servo to turn to the 180 degree position
s1.write_angle(180) # Control the servo to turn to the 180 degree position Method 2
print("180")
time.sleep(1)
#s1.angle(90) # Control the servo to turn to the 90 degree position
s1.write_angle(90) # Control the servo to turn to the 90 degree position Method 2
print("90")
time.sleep(1)