Arduino Nano ESP32 - Joystick - Servo Motor
In this tutorial, we are going to learn how to use Arduino Nano 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 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 according to the provided diagram.
- Connect the Arduino Nano ESP32 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the Arduino Nano ESP32 board and its corresponding 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 Arduino Nano ESP32.
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino Nano ESP32
- Open Serial Monitor
- Push the joystick in some direction
- See the servo motor's rotation
- See the result on Serial Monitor
- Open the Library Manager by clicking on the Library Manager icon on the left navigation bar of Arduino IDE
- Search “ezButton”, then find the button library by ArduinoGetStarted.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 Arduino Nano ESP32
- Open Serial Monitor
- Push the joystick in some direction
- See the servo motor's rotation
- See the result on Serial Monitor
- The Arduino Nano ESP32's ADC is not perfectly accurate and might require calibration for correct results. Each Arduino Nano ESP32 board can vary slightly, so calibration is necessary for each individual board.
- Calibration can be challenging, especially for beginners, and might not always yield the exact results you want.
We will explore the code for both use cases in the next parts.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of 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
Arduino Nano ESP32 Code
Arduino Nano ESP32 Code - The servo motors rotate according to the movement of the joystick's thump
Detailed Instructions
To get started with Arduino Nano ESP32, follow these steps:
You may have noticed that the servo motor does not move proportionally to the joystick movement. This issue is not due to the joystick or the servo motor itself, but rather the ADC of the Arduino Nano ESP32. The end of this tutorial will explain why this happens.
Arduino Nano ESP32 Code - Use the joystick to command servo motors
Detailed Instructions
Code Explanation
Read the line-by-line explanation in comment lines of source code!
※ NOTE THAT:
This tutorial uses the analogRead() function to read values from an ADC (Analog-to-Digital Converter) connected to a sensor or component. The Arduino Nano ESP32's ADC is suitable for projects that do not require high accuracy. However, for projects needing precise measurements, keep the following in mind:
For projects requiring high precision, consider using an external ADC (e.g ADS1115) with the Arduino Nano ESP32 or using another Arduino, such as the Arduino Uno R4 WiFi, which has a more reliable ADC. If you still want to calibrate the Arduino Nano ESP32's ADC, refer to the ESP32 ADC Calibration Driver.