ESP32 S3 - Servo Motor

The ESP32 S3 is a capable microcontroller that makes controlling servo motors straightforward using PWM signals and the ESP32Servo library. This tutorial walks you through everything you need — from wiring to code — so you can have your servo sweeping between 0° and 180° in minutes.

What you'll build:

  1. A servo motor connected to your ESP32 S3 board
  2. A sweeping motion from 0° to 180° and back, driven by Arduino code
  3. A reliable power setup that protects your board from overcurrent
  4. A foundation for robotics, automation, and mechatronics projects
ESP32 S3 - Servo Motor

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Servo Motor
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Buy Note: For controlling multiple servo motors, use the PCA9685 16 Channel PWM Servo Driver Module to save MCU pins and simplify wiring.

Overview of Servo Motor

A servo motor is a rotary actuator capable of rotating to a precise angular position between 0° and 180°, making it an ideal output device for robotics and automation. Unlike ordinary DC motors, a servo integrates a feedback mechanism internally so it holds the commanded angle without any extra circuitry. This combination of precision and simplicity is why servo motors pair so well with the ESP32 S3.

Key Specifications

Standard hobby servo motors operate on a 5 V supply and accept a PWM signal with a period of 20 ms. The pulse width determines the target angle: roughly 1 ms corresponds to 0° and 2 ms corresponds to 180°, with 1.5 ms centering the shaft at 90°. Most servos draw between 100 mA and 700 mA depending on load, which is why careful power planning matters.

Servo Motor Pinout

The servo motor uses a simple three-wire interface that connects directly to the ESP32 S3 and a power source:

  1. GND pin (brown or black) — connects to GND (0 V)
  2. VCC pin (red) — connects to 5 V power
  3. Signal pin (yellow or orange) — receives the PWM control signal from an ESP32 S3 GPIO pin
Servo Motor Pinout

How Servo Motor Works

See How servo motor works

Wiring Diagram between Servo Motor and ESP32 S3

Connect the servo motor to your ESP32 S3 board using the table below. The signal wire goes to GPIO pin GPIO9, which the ESP32 S3 drives with a standard 50 Hz PWM waveform generated by the ESP32Servo library.

Safety Notes

When powering the ESP32 S3 via USB, do not draw servo power from the Vin or VBUS pins — the USB port's current limit is too low for most servos under load and can permanently damage the board. For anything beyond light bench testing, use a dedicated 5 V external power supply for the servo and share only the GND rail with the ESP32 S3.

When powering via USB port:

The wiring diagram between ESP32 S3 Servo Motor

This image is created using Fritzing. Click to enlarge image

Servo Motor Pin ESP32 S3 Pin
GND (Brown/Black) GND
VCC (Red) 5V
Signal (Yellow/Orange) GPIO9

When powering via Vin pin with external power supply:

The wiring diagram between ESP32 S3 servo motor external power supply

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

IMPORTANT SAFETY WARNING: When powering the ESP32 S3 board via USB port, do NOT power the servo motor via Vin pin or VBUS pin. This can cause excessive current draw and may permanently damage your board. Use an external power supply for the servo motor if you need more power.

ESP32 S3 Code

The following sketch demonstrates continuous servo sweeping on the ESP32 S3. It uses the ESP32Servo library to attach the servo to pin GPIO9, then steps the angle from 0° to 180° in 1° increments before reversing back, creating a smooth back-and-forth motion that repeats indefinitely. Reading the inline comments in the code will give you a line-by-line explanation of how each part works.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-servo-motor */ #include <ESP32Servo.h> #define SERVO_PIN 9 // The ESP32 S3 pin connected to servo motor Servo servoMotor; void setup() { servoMotor.attach(SERVO_PIN); // attaches the servo on ESP32 pin } void loop() { // rotates from 0 degrees to 180 degrees for (int pos = 0; pos <= 180; pos += 1) { // in steps of 1 degree servoMotor.write(pos); delay(15); // waits 15ms to reach the position } // rotates from 180 degrees to 0 degrees for (int pos = 180; pos >= 0; pos -= 1) { servoMotor.write(pos); delay(15); // waits 15ms to reach the position } }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Set up the Arduino IDE for ESP32 S3 by following the ESP32 S3 software installation guide.
  3. Wire the servo motor to the ESP32 S3 according to the wiring diagram above.
  4. Connect the ESP32 S3 board to your computer with a USB Type-C cable.
  5. Open the Arduino IDE on your computer.
  6. Select the ESP32 S3 board and its corresponding COM port under Tools.
  7. Open Library Manager by clicking the Libraries icon on the left sidebar.
  8. Type "ESP32Servo" in the search box.
  9. Install the servo library by Kevin Harrington and John K. Bennett.
  • Search for ESP32Servo created by Kevin Harrington,John K. Bennett and click the Install button.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Library Manager
Type:
All
Topic:
All
ESP32Servo by Kevin Harrington,John K. Bennett
This library provides advanced PWM control for ESP32 boards using LEDC hardware, with enhanced MCPWM support on ESP32S3.
ESP32S3: 20 PWM channels (8 LEDC + 12 MCPWM) with intelligent allocation
All ESP32 variants: LEDC-based PWM with variable-frequency support
Supports variable-frequency PWM and fixed-frequency servo control
Automatic hardware fallback ensures maximum channel availability
More info
3.2.0
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32S3 Dev Module on COM15
1
  1. Copy the code above and paste it into a new Arduino IDE sketch.
  2. Click the Upload button to compile and upload the code to the ESP32 S3.
How to upload ESP32 S3 code on Arduino IDE
  1. Observe the servo shaft rotating 180° clockwise, then reversing counter-clockwise, repeating continuously.
  2. Pro Tip: Start with slow sweep speeds when first testing a servo — it reduces mechanical stress and makes it easier to spot wiring or calibration issues before adding load.

Line-by-line Code Explanation

The above ESP32 S3 code contains line-by-line explanation. Please read the comments in the code!

How to Control Speed of Servo Motor

See How to Control Speed of Servo Motor

Applications and Project Ideas

The ESP32 S3 servo motor combination unlocks a wide range of practical builds, from simple classroom demonstrations to full mechatronics systems:

  1. Robotic arm: Build a multi-joint robotic arm using several servos for pick-and-place operations.
  2. Automatic pet feeder: Use a servo-controlled gate to dispense measured portions of food on a timer.
  3. Smart door lock: Design a WiFi-controlled locking mechanism driven by a servo on the ESP32 S3.
  4. Solar panel tracker: Mount a servo to tilt a panel toward the sun based on a light sensor reading.
  5. Camera pan-tilt: Build a two-axis mount for a small camera, ideal for surveillance or photography rigs.
  6. Automated plant watering: Use a servo valve to release water when a soil moisture sensor triggers it.

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

The instruction and source code for the above video available at how to control servo motor via web tutorial

Challenge Yourself

Once your servo is sweeping reliably on the ESP32 S3, try these challenges to deepen your understanding of motor control and embedded programming:

  1. Beginner: Modify the sweep range to move between 45° and 135° instead of the full 0°–180° arc.
  2. Beginner: Adjust the delay values to make the servo sweep faster or slower and observe the effect on smoothness.
  3. Intermediate: Add a push button so the servo only sweeps while the button is held, then holds position when released.
  4. Intermediate: Drive two servo motors simultaneously from the same ESP32 S3 sketch using separate GPIO pins.
  5. Advanced: Build a web interface served by the ESP32 S3 that lets you set the servo angle remotely via WiFi.
  6. Advanced: Implement joystick control by reading an analog joystick and mapping its X/Y axes to servo angles in real time.

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!