ESP32 Servo Motor in MicroPython - SG90 Guide
ESP32 Servo Motor in MicroPython - SG90 Guide
ESP32
ESP32 Installation
in MicroPython
ESP32 Programming
(Updated at 01/06/2023)
ESP32 Pinout
Arduino Code
MicroPython
Basics
Sensors
Actuators
Servo SG90
Relay
Screens
IoT
Raspberry Pi Pico
Theory
Servo motors, frequently shortened to “servo”, are a special form of motor that can be fixed to a
specific position with great precision. This position is maintained until a new instruction is given.
They have an excellent power-to-weight ratio. TowerPro’s popular SG90 blue servo has a torque of 1.5
kg/cm for only 9g. Its low price and ease of control from an ESP32 make it a popular choice for makers!
Note
Servos are widely used in model making (wheel steering in remote controlled cars, control of rudder and elevator
on airplanes, etc.), but also in robotics and industry, for example to regulate liquid flows in valves.
Warning
The plastic stop on the actuator that limits rotation is relatively fragile. It is important to avoid turning the actuator
shaft by hand and forcing it to the stop.
Operation of a servomotor
A small DC motor is connected to a potentiometer via an electronic circuit, which allows the speed of
the motor to be finely regulated according to the position of the potentiometer. A series of gears is
attached to the motor’s output shaft to multiply the torque while reducing its speed. When the motor
turns, the gears drive the movement of the arm which in turn drives the potentiometer. If the
movement stops, the electronic circuit continuously adjusts the motor speed to keep the potentiometer
and therefore the arm at the same position. This feature is particularly useful for robot arms that do not
fall back under their own weight when the movement stops.
Note
This small servomotor is controlled using a pulse width modulated (PWM) signal with a frequency of 50
Hz, i.e. one pulse every 20ms. The position of the actuator is determined by the duration of the pulses,
usually varying between 1ms and 2ms.
1 ms
1,5 ms
2 ms
20 ms
Note
A servo motor consumes a lot of current, especially when it exerts a lot of torque. Since the 5V pin on
most ESP32 boards comes directly from the USB bus, you will be limited to a maximum of 500mA. With
1 or 2 servo motors connected the ESP32 should hold the load.
Beyond 2, use a separate power supply instead. In this case, be sure to connect a pin GND from the
board to the negative terminal of the actuator power supply 😉.
Warning
On uPesy ESP32 boards, the self-resetting fuse may trip if the current is too high.
5V Rouge 5V ou 3V3
Note
On some servo motor models, the signal wire color is yellow or white instead of orange.
Any output pin of the ESP32 can be used to control the servo motor because the ESP32 pins are all
capable of producing a PWM output.
Basic Python script to drive the servo with its own calculations
With the SG90 servo from TowerPro, the minimum position corresponds to a pulse width of 0.5ms and
the maximum position to one of 2.4ms. By doing some calculations, we can deduce the duty-cycle of
the PWM, then the value in bits of the pulse width. I will not detail the calculations, because we will
rather use a ready-made library for the continuation.
Note
The difficulty is to find the right PWM pulse width to obtain a given angular position.
# 0.025*1024=25.6
# 0.12*1024=122.88
while True:
sg90.duty(26)
time.sleep(1)
sg90.duty(123)
time.sleep(1)
I grant you that it is not very easy to understand. That’s why we are going to use a library!
Warning
In the current version of MicroPython for the ESP32 (v1.19), this library is not present as standard. Only the
Pyboard board has a library servo included as standard in MicroPython.
class Servo:
# these defaults work for the standard TowerPro SG90
__servo_pwm_freq = 50
__min_u10_duty = 26 - 0 # offset for correction
__max_u10_duty = 123- 0 # offset for correction
min_angle = 0
max_angle = 180
current_angle = 0.001
Warning
The code of this library works only for ESP32 boards (the code is slightly different for the Raspberry Pi Pico for
example)
To use the library, it is very simple. After importing the class Servo object, we define a Servo which
represents our blue servomotor. We specify the pin used to drive it in the manufacturer’s parameters.
Then we indicate the desired angular position with the function .move(angle) .
Previous Next
Actuators Control electrical devices with a Relay and an ESP32
in MicroPython
Terms of Sales
Email
Legal Notices
By confirming your subscription, you agree to receive news from uPesy
Privacy Policy by e-mail. You may cancel your subscription at any time.
Delivery - Return
© 2025, uPesy