ESP32 S3 UNO - Potentiometer Servo Motor

This ESP32 S3 Uno potentiometer servo motor tutorial shows you how to turn a knob and move a servo to the matching angle. You wire a potentiometer and a servo motor to the ESP32 S3 Uno form board, then write Arduino code that reads the knob and sets the servo position from 0° to 180°.

In this tutorial, you will:

  1. Wire a potentiometer and a servo motor to the ESP32 S3 Uno
  2. Read the potentiometer value with analogRead()
  3. Convert the 0 to 4095 reading into a servo angle with map()
  4. Control the servo motor with the ESP32Servo library
ESP32 S3 Uno potentiometer servo motor

Hardware Preparation

1×ESP32 S3 Uno-form Board
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×Potentiometer
1×Alternatively, 10k Ohm Trimmer Potentiometer
1×Alternatively, Potentiometer Kit
1×Alternatively, Potentiometer Module with Knob
1×Breadboard
1×Jumper Wires

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 and Potentiometer

This project joins two parts you may already know. If the servo motor or the potentiometer is new to you, learn each one first. It makes this project much easier to follow.

The servo tutorial explains the servo pins, how the angle is set, and how to power it. The potentiometer tutorial explains how the knob position becomes a voltage and an ADC value.

ESP32 S3 Uno Pinout

The picture below shows the pinout of the ESP32 S3 Uno form board. Use it to find the D9 and A0 headers and their GPIO numbers before you wire the circuit.

ESP32 S3 Uno pinout diagram

Wiring Diagram

You can use a classic three-pin potentiometer or a potentiometer module with a knob. Both wire the same way: the knob output goes to A0, and the servo signal goes to D9.

Wiring with a classic potentiometer

The wiring diagram between ESP32 S3 Uno Servo Motor Potentiometer

This image is created using Fritzing. Click to enlarge image

Wiring with a potentiometer module

The wiring diagram between ESP32 S3 Uno Servo Motor Potentiometer module

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Uno Pin
Potentiometer VCC 3.3V
Potentiometer GND GND
Potentiometer Output A0 (GPIO2)
Servo VCC (red) 5V
Servo GND (brown or black) GND
Servo Signal (orange or yellow) D9 (GPIO46)

WARNING

D9 is GPIO46 on the ESP32 S3 Uno. It is a boot strapping pin, which the chip reads at startup to choose the boot mode. A servo signal wire is usually safe here, but if the board does not boot or upload, unplug the servo signal wire from D9, upload the code, and plug it back in.

WARNING

ESP32 S3 Uno pins are NOT 5V tolerant. Power the potentiometer from 3.3V, not 5V, so its output never goes above 3.3V. The servo can still use 5V for power, because its signal wire only receives the 3.3V signal from the board.

How To Program

The whole program is just three steps that repeat again and again. Read the knob, turn the reading into an angle, then send that angle to the servo.

  1. Read the potentiometer. The ESP32 S3 Uno ADC is 12-bit, so the value goes from 0 to 4095. The A0 header is GPIO2.
int analogValue = analogRead(2);
  1. Scale the reading into an angle between 0 and 180.
int angle = map(analogValue, 0, 4095, 0, 180);
  1. Move the servo to that angle.
myServo.write(angle);

ESP32 S3 Uno Code

The sketch below reads the potentiometer on A0 (GPIO2) every 100 ms. It maps the value to an angle, moves the servo on D9 (GPIO46), and prints both numbers to the Serial Monitor.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-potentiometer-servo-motor */ #include <ESP32Servo.h> #define SERVO_PIN 46 // ESP32 S3 Uno pin D9 (GPIO46) connected to the servo motor #define POT_PIN 2 // ESP32 S3 Uno pin A0 (GPIO2) connected to the potentiometer Servo myServo; // create servo object to control a servo void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); myServo.attach(SERVO_PIN); // attaches the servo on pin D9 (GPIO46) to the servo object } void loop() { // reads the value of the potentiometer (value between 0 and 4095) int analogValue = analogRead(POT_PIN); // scales it to use it with the servo (value between 0 and 180) int angle = map(analogValue, 0, 4095, 0, 180); // sets the servo position according to the scaled value myServo.write(angle); // print out the value Serial.print("Potentiometer's Value: "); Serial.print(analogValue); Serial.print(" => Servo Motor's Angle: "); Serial.println(angle); delay(100); }

The code uses ESP32Servo.h instead of Servo.h, because the standard Servo library does not support ESP32 boards. It also calls analogSetAttenuation(ADC_11db) in setup(). By default, the ESP32 S3 Uno ADC can only measure a small voltage. With 11 dB attenuation, the analog pin reads the full range from 0V up to about 3.3V. Without it, the reading hits 4095 too early and the servo reaches 180° before the knob is fully turned.

Detailed Instructions

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Install the library. Open the Library Manager, search for ESP32Servo, and install the one by Kevin Harrington.
  6. Copy the code above and paste it into the Arduino IDE.
  7. Upload the code. Click the Upload button to send the sketch to the ESP32 S3 Uno.
  8. Open the Serial Monitor and set the baud rate to 9600.
  9. Turn the knob of the potentiometer slowly from one end to the other.
  10. Watch the servo rotate and check the values in the Serial Monitor.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
Potentiometer's Value: 0 => Servo Motor's Angle: 0 Potentiometer's Value: 340 => Servo Motor's Angle: 14 Potentiometer's Value: 804 => Servo Motor's Angle: 35 Potentiometer's Value: 1144 => Servo Motor's Angle: 50 Potentiometer's Value: 1480 => Servo Motor's Angle: 65 Potentiometer's Value: 1776 => Servo Motor's Angle: 78 Potentiometer's Value: 2084 => Servo Motor's Angle: 91 Potentiometer's Value: 2432 => Servo Motor's Angle: 106 Potentiometer's Value: 2760 => Servo Motor's Angle: 121 Potentiometer's Value: 3172 => Servo Motor's Angle: 139 Potentiometer's Value: 3628 => Servo Motor's Angle: 159 Potentiometer's Value: 4095 => Servo Motor's Angle: 180 Potentiometer's Value: 4095 => Servo Motor's Angle: 180
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Tip: If the servo shakes at one position, the reading may be noisy. Add a small capacitor (about 100 nF) between A0 and GND, or average a few readings in the code.

Code Explanation

Each line of the sketch has a comment that tells you what it does. Read the code from top to bottom with the comments, and the flow is easy to follow: set up the servo and the ADC once, then read, map and write in a loop.

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

Function References

These pages explain the functions used in this project in more detail. The Servo functions work the same way with the ESP32Servo library.

FAQ

Can I use the normal Servo library with the ESP32 S3 Uno?

No. The standard Servo.h library only works on AVR and some other boards. On the ESP32 S3 Uno, install the ESP32Servo library by Kevin Harrington and include ESP32Servo.h. The functions attach(), write() and read() work the same way.

Should I power the potentiometer from 3.3V or 5V?

Use 3.3V. The ESP32 S3 Uno pins are not 5V tolerant, so a potentiometer on 5V could send up to 5V into A0 and damage the pin. With 3.3V, the full knob range maps to 0 to 4095.

Why does the code call analogSetAttenuation(ADC_11db)?

It sets the ESP32 S3 Uno ADC input range to about 0V to 3.3V, so the full potentiometer signal maps to 0 to 4095. Without it, the range is smaller and the reading reaches 4095 too early. The UNO R4 does not need this line, it is for ESP32 boards only. Keep the potentiometer output at 3.3V or less.

Can I power the servo motor from the ESP32 S3 Uno 5V pin?

A small servo like the SG90 usually works from the 5V pin when the board is powered by USB. Bigger servos, or several servos, draw more current and can reset the board. In that case, use an external 5V supply and connect its GND to the board GND.

Why is the servo on D9, which is a warning pin?

D9 is GPIO46, a boot strapping pin. The wiring keeps the same D9 position as the UNO version of this project. A servo signal input usually does not disturb booting, but if you have upload or boot problems, unplug the signal wire during upload or move the servo to another PWM pin like D7 (GPIO14) and change SERVO_PIN in the code.

What if my project needs more pins than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs. GPIO15 and GPIO16 are I/O, PWM and analog. GPIO45 and GPIO35 to GPIO42 are I/O and PWM. GPIO47 and GPIO48 are output only, with PWM. Solder pin headers to use them and write the GPIO number in code. For this project, a second potentiometer fits GPIO15 or GPIO16, and extra servos fit GPIO35 to GPIO42. Be careful with two things. First, GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Troubleshooting

Problem Possible Cause Solution
No COM port in Arduino IDE Missing USB driver or board not in upload mode Install the CP210x or CH340 driver, try another cable, or hold BOOT while pressing RESET
Upload fails or board does not boot Servo signal on D9 (GPIO46) affects the boot strapping pin Unplug the servo signal wire during upload, then plug it back in
Compile error about Servo.h The standard Servo library does not support the ESP32 S3 Uno Install ESP32Servo by Kevin Harrington and include ESP32Servo.h
Servo reaches 180 degrees before the knob is fully turned ADC attenuation not set or potentiometer on 5V Keep analogSetAttenuation(ADC_11db) in setup() and power the potentiometer from 3.3V
Servo does not move at all Signal wire on the wrong pin or no common ground Check the signal wire is on D9 and the servo GND is tied to the board GND
Board resets when the servo moves Servo draws too much current from the board Use an external 5V supply for the servo and share GND with the board
Servo jitters at a fixed knob position Noisy analog reading Add a 100 nF capacitor from A0 to GND or average several readings
Serial Monitor shows strange characters Wrong baud rate Set the Serial Monitor to 9600 baud

※ 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!