Arduino - Joystick - Servo Motor
In this tutorial, we are going to learn how to use Arduino 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
- Connect Arduino to PC via USB cable
- Open Arduino IDE, select the right board and port
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Open Serial Monitor
- Push the joystick in some direction
- See the servo motor's rotation
- See the result on Serial Monitor
- Navigate to the Libraries icon on the left bar of the 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
- 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 Required
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
About 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 Code
Arduino Code - The servo motors rotate according to the movement of the joystick's thump
Quick Steps
Arduino Code - Use the joystick to command servo motors
Quick Steps
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.