Arduino Nano ESP32 - Potentiometer Servo Motor
This tutorial instructs you how to use Arduino Nano ESP32 to control the angle of a servo motor based on the input value from a potentiometer. In detail, we will learn:
- How to connect the potentiometer and servo motor to Arduino Nano ESP32
- How to program Arduino Nano ESP32 to read the value from a potentiometer and control a servo motor accordingly.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
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
If you are unfamiliar with servo motors and potentiometers, including pinout, functioning, and programming, the following tutorials will be helpful:
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Here is the same wiring without a breadboard:

This image is created using Fritzing. Click to enlarge image
Loose wires are a common cause of strange bugs. Simply plug the Arduino Nano ESP32 into a Screw Terminal Expansion Board for Arduino Nano. Each wire goes to the screw terminal of the same pin, as in the image below.

This image is created using Fritzing. Click to enlarge image
How To Program
- Read the value from the potentiometer (ranging from 0 to 1023)
- Convert it it to an angle in the range of 0 to 180.
- Control the servo to the specified angle.
Arduino Nano ESP32 Code
Detailed Instructions
- If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
- Wire the components as shown in the diagram.
- Connect the Arduino Nano ESP32 board to your computer using a USB cable.
- Open Arduino IDE on your computer.
- Choose the Arduino Nano ESP32 board, and its respective COM port.
- Copy the code above and open it in the Arduino IDE.
- Click the Upload button in the Arduino IDE to transfer the code to the Arduino Nano ESP32.

- Open the Serial Monitor
- Turn the potentiometer
- Check out the servo motor's rotation
- View the result in the Serial Monitor
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!