Control of a servo motor using an ESP32 and the ESP32Servo library.
Project OverviewOutput Device
For this week I worked with an output device by controlling a servo motor using an ESP32.
The purpose of this assignment was to understand how a microcontroller can generate signals
to move a physical actuator.
A servo motor is an ideal output device because it converts an electrical signal into a controlled mechanical movement.
Unlike a standard DC motor, a servo can move to a specific angle, which makes it useful for robotic systems,
mechanisms, doors, arms, and positioning systems.
In this exercise, the servo rotates in steps from 0° to 180° and then returns to its initial position.
Servo MotorActuator
A servo motor is an electromechanical device capable of moving to a specific angular position.
It contains:
A DC motor
A gearbox
A control circuit
A potentiometer for position feedback
Servo motors are controlled through PWM signals.
The width of the pulse determines the target angle of the shaft.
Pulse Width
Approximate Angle
500 µs
0°
1500 µs
90°
2400 µs
180°
ConnectionsHardware
The servo motor was connected directly to the ESP32.
Three wires are required:
Servo Wire
Connection
Red
5V Power
Brown / Black
GND
Orange / Yellow
GPIO 0
GPIO 0 was used as the control pin for the servo signal.
The code starts by including the required libraries:
Arduino.h for standard Arduino functions.
ESP32Servo.h to control servo motors with the ESP32.
Servo Object
A servo object named Servo_1 is created.
This object is used later to send commands to the motor.
Pin Definition
The servo signal wire is connected to GPIO 0:
const int Pin_Servo_1 = 0;
Setup Function
Inside the setup function, the servo is attached to the selected pin:
Servo_1.attach(Pin_Servo_1, 500, 2400);
The values 500 and 2400 represent the minimum and maximum pulse width in microseconds.
These values correspond approximately to 0° and 180°.
The servo is initialized at 0°:
Servo_1.write(0);
Loop Function
In the loop, the servo rotates from 0° to 180° in increments of 45°:
for (int Angle = 0; Angle <= 180; Angle += 45)
The movement sequence is:
0°
45°
90°
135°
180°
After reaching 180°, the servo returns to 0° and repeats the cycle.
ResultMotion Test
The final result was a servo motor that moved repeatedly between different angles.
The motion was smooth and stable because the servo library generated the PWM signal automatically.
Correct servo response
Stable angular positions
Repeatable movement cycle
Servo motor moving through programmed positions.
Reflection
This week helped me understand how output devices can be controlled through PWM signals.
Servo motors are especially useful because they provide precise positioning rather than continuous rotation.
Learning how to control a servo motor is important for future applications such as robotic arms,
moving mechanisms, smart locks, adjustable vents, and automated systems.
This assignment also reinforced the relationship between software and hardware,
since the microcontroller code directly controlled the physical movement of the actuator.