Servos can be used with the ESP32 on any output ports that support PWM (pulse width modulation). The current version of Micropython for the ESP32 does not include direct support for servos, but you can use the PWM library to control them as discussed in the ESP8266 Micropython documentation.
But there is a simple Servo Python class definition originally by Radomir Dopieralski that provides an easier interface for standard hobby servos.
- Download - Get the servo Python class from here
- Install Servo Library - Use ampy to put the servo.py module on your device
ampy -p /dev/tty.SLAB_USBtoUART put servo.py
Orange Pins (GPIO, not GPI) 4, 5, 13, 14, 15, 16, 17, 18, 19, 25, 26, 27, 32, 33 (Pin 12 is not recommended)
Diagram: Jon Froehlich
Pins - In this example, we're using Pin(15) for the Servo output
Servo - Connect the servo in the following way
- signal control (typically yellow or white) to Pin(15) or your choice of output pin
- power to the 3V output
- ground to the GND
The ESP32 may not be able to properly power a full size servo (especially more than one at at time), so you may need to use an external power supply of 5 to 6 Volts (most hobby servos run on this). This can be 4 AA or 4 AAA batteries, or a 3.3V LiPo with a 5V converter. The servo(s) should connect to the power and ground of the battery, and the output pin on the ESP32.
In addition, you must connect the battery ground to the ESP32 ground. This diagram shows this wiring approach for 2 servos connect to the ESP32 pins 15 and 32, but you can choose your other PWM pins.
This simple example creates a servo object connected to Pin(15), then moves it from angle 15 to 165 and then to 90 with pauses in between each.
import time
import machine
from servo import Servo
servo_pin = machine.Pin(15)
my_servo = Servo(servo_pin)
my_servo.write_angle(15)
time.sleep(2)
my_servo.write_angle(165)
time.sleep(2)
my_servo.write_angle(90)- Note that each servo is different, and may not travel it's entire range with the default values of the Servo object. You can experiment by changing the min_us and max_us values when you create the Servo object if you use the fuller initialization arguments. See the servo.py module source code below, and this page for more details.
# Servo() Args:
# pin (machine.Pin): The pin where servo is connected. Must support PWM.
# freq (int): The frequency of the signal, in hertz.
# min_us (int): The minimum signal length supported by the servo.
# max_us (int): The maximum signal length supported by the servo.
# angle (int): The angle between the minimum and maximum positions.
# default values
my_servo = Servo(servo_pin, 50, 600, 2400, 180)This example uses a for loop to move a servo back and forth from 15 degree to 165 degrees.
import time
import machine
from servo import Servo
servo_pin = machine.Pin(15)
my_servo = Servo(servo_pin)
delay = 0.01
min = 15
max = 165
while True:
for i in range(min,max):
my_servo.write_angle(i)
time.sleep(delay)
for i in range(max, min, -1):
my_servo.write_angle(i)
time.sleep(delay)



