ESP32 - Servo Motor controlled by Potentiometer

This tutorial instructs you how to use ESP32 with servo motor and potentiometer. In detail, the angle of a servo motor is in proportion with the value read from a potentiometer.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Servo Motor
1×Potentiometer
1×Potentiometer Kit
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Expansion Board for ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to Servo Motor and Potentiometer

We have specific tutorials about servo motor and potentiometer. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP32, ESP32 code... Learn more about them at the following links:

Wiring Diagram

ESP32 Servo Motor Potentiometer Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-servo-motor-controlled-by-potentiometer */ #include <Servo.h> #define PIN_POTENTIOMETER 36 // ESP32 pin GPIO36 (ADC0) onnected to potentiometer #define PIN_SERVO 26 // ESP32 pin GPIO26 onnected to servo motor 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(PIN_SERVO); // attaches ESP32 pin to the servo object } void loop() { // reads the value of the potentiometer (value between 0 and 4095) int analogValue = analogRead(PIN_POTENTIOMETER); // 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("Analog value: "); Serial.print(analogValue); Serial.print(" => Angle: "); Serial.println(angle); delay(100); }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Type ServoESP32 on the search box, then look for the servo library by Jaroslav Paral. Please be aware that both version 1.1.1 and 1.1.0 are affected by bugs. Kindly choose a different version.
  • Click Install button to install servo motor library for ESP32.
ESP32 servo motor library
  • Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
Arduino IDE Upload Code
  • Open Serial Monitor on Arduino IDE
How to open serial monitor on Arduino IDE
  • Adjust the potentiometer
  • See the servo motor's rotation
  • See the result on Serial Monitor. It looks like the below:
COM6
Send
Analog value: 0 => Angle: 0 Analog value: 85 => Angle: 14 Analog value: 201 => Angle: 35 Analog value: 286 => Angle: 50 Analog value: 370 => Angle: 65 Analog value: 444 => Angle: 78 Analog value: 521 => Angle: 91 Analog value: 608 => Angle: 106 Analog value: 690 => Angle: 121 Analog value: 793 => Angle: 139 Analog value: 907 => Angle: 159 Analog value: 4095 => Angle: 180 Analog value: 4095 => Angle: 180
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Line-by-line Code Explanation

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

※ NOTE THAT:

This tutorial uses the analogRead() function to read values from an ADC (Analog-to-Digital Converter) connected to a potentiometer. The ESP32 ADC is good for projects that do NOT need high accuracy. However, for projects that need precise measurements, please note:

  • The ESP32 ADC is not perfectly accurate and might need calibration for correct results. Each ESP32 board can be a bit different, so you need to calibrate the ADC for each individual board.
  • Calibration can be difficult, especially for beginners, and might not always give the exact results you want.

For projects that need high precision, consider using an external ADC (e.g ADS1115) with the ESP32 or using an Arduino, which has a more reliable ADC. If you still want to calibrate the ESP32 ADC, refer to ESP32 ADC Calibration Driver

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES