ESP32 - Servo Motor controlled by Potentiometer

In a previous tutorial, We have learned how a potentiometer triggers a servo motor. In this tutorial, We are going to learn how to rotate a servo motor according to the potentiometer's output value

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Servo Motor
6×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About Servo Motor and Potentiometer

If you do not have knowledge of servo motor and potentiometer (pinout, how they work, how to connect them to ESP32, how to program for them...), learn about them in the following tutorials:

Wiring Diagram

ESP32 Servo Motor Potentiometer Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

How To Program

  • Reads the value of the potentiometer (value between 0 and 1023)
int analogValue = analogRead(A0);
  • Scales it to angle (value between 0 and 180)
int angle = map(analogValue, 0, 1023, 0, 180);
  • Sets the servo position according to the angle

ESP32 Code

/* * Created by * * This example code is in the public domain * * Tutorial page: */ #include <Servo.h> Servo myServo; // create servo object to control a servo void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); myServo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { // reads the value of the potentiometer (value between 0 and 1023) int analogValue = analogRead(A0); // scales it to use it with the servo (value between 0 and 180) int angle = map(analogValue, 0, 1023, 0, 180); // sets the servo position according to the scaled value myServo.write(angle); // print out the value Serial.print("Analog: "); Serial.print(analogValue); Serial.print(", Angle: "); Serial.println(angle); delay(100); }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE, select the right ESP32 board and COM port
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Arduino IDE Upload Code
  • Open Serial Monitor on Arduino IDE
  • How to open serial monitor on Arduino IDE
  • Rotate the potentiometer
  • See the servo motor's rotation
  • See the result on Serial Monitor. It looks like the below:
  • COM6
    Analog: 0, Angle: 0 Analog: 85, Angle: 14 Analog: 201, Angle: 35 Analog: 286, Angle: 50 Analog: 370, Angle: 65 Analog: 444, Angle: 78 Analog: 521, Angle: 91 Analog: 608, Angle: 106 Analog: 690, Angle: 121 Analog: 793, Angle: 139 Analog: 907, Angle: 159 Analog: 1023, Angle: 180 Analog: 1023, Angle: 180
    Autoscroll Show timestamp
    Clear output
    9600 baud  

Code Explanation

Read the line-by-line explanation in comment lines of source code!

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Alternative Servo Motors

The above code also works with the following servo motors:

Alternative Potentiometers

The above code also works with the following rotary angle sensors:

Servo Motor on Commercial Products

Potentiometer on Commercial Products


Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.


  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere