ESP32 - Joystick - Servo Motor
In this tutorial, we are going to learn how to use ESP32 and a joystick to control two servo motors or a pan-tilt kit with servos.
A joystick has two built-in potentiometers square with each other (called X-axis and Y-axis). These potentiometers output the analog values (called X-value and Y-value) on VRX and VRY pins. We will use these analog values to control two servo motors independently: X-value controls servo #1 and Y-value controls servo #2. In the case of the pan-tilt kit, the movement of two servo motors creates 3-D movement.
There are two application use cases:
- Use case 1: The servo motors rotate according to the movement of the joystick's thump:
- The angles of servo motors are in proportion to the values of the joystick's X-value and Y-value.
- When we release the joystick's thump ⇒ all values returns back to the center values ⇒ servo motors automatically returns back to the center position
- Use case 2: use the joystick to command servo motors (up/down/left/right commands)
- When joystick's thump is pushed to the left, move the servo motor #1 in a clockwise direction step-by-step
- When joystick's thump is pushed to the right, move the servo motor #1 in an anti-clockwise direction step-by-step
- When joystick's thump is pushed up, move the servo motor #2 in clockwise direction step-by-step
- When joystick's thump is pushed down, move the servo motor #2 in an anti-clockwise direction step-by-step
- When we release the joystick's thump ⇒ the servo motors will not return to the center position
- If the joystick is pressed (push button), The servo motors will return to the center position
- 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.
- On Arduino IDE, Navigate to Tools Manage Libraries
- Type ESP32Servo on the search box, then look for the keypad library by Mark Stanley, Alexander Brevig
- Click Install button to install servo motor library for ESP32.
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to ESP32
- Open Serial Monitor
- Push the joystick in some direction
- See the servo motor's rotation
- See the result on Serial Monitor
- On Arduino IDE, Go to Tools Manage Libraries
- Search “ezButton”, then find the button library by ESP32GetStarted.com
- Click Install button to install ezButton library.
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to ESP32
- Open Serial Monitor
- Push the joystick in some direction
- See the servo motor's rotation
- See the result on Serial Monitor
We will explore the code for both use cases in the next parts.
Hardware Used In This Tutorial
Introduction to Joystick and Servo Motor
If you do not know about joystick and servo motor (pinout, how it works, how to program ...), learn about them in the following tutorials:
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
ESP32 Code
ESP32 Code - The servo motors rotate according to the movement of the joystick's thump
Quick Instructions



ESP32 Code - Use the joystick to command servo motors
Quick Instructions


Code Explanation
Read the line-by-line explanation in comment lines of source code!
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.