Arduino Mega - Joystick
This guide shows how to use a joystick with an Arduino Mega board. We will cover:
- What a joystick does and how it works
- How to connect a joystick to Arduino Mega
- How to program Arduino Mega to read joystick data
- How to turn joystick data into useful actions (for example X and Y positions or motor directions)

Hardware Preparation
| 1 | × | Arduino Mega | |
| 1 | × | USB 2.0 cable type A/B (for USB-A PC) | |
| 1 | × | USB 2.0 cable type C/B (for USB-C PC) | |
| 1 | × | Joystick | |
| 1 | × | Jumper Wires |
Or you can buy the following 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
You may have seen a joystick used as a game controller, a toy controller, or even in big machines like excavators.
The joystick has two pots arranged in a square shape and one push button. It gives these outputs:
- An analog reading (0 to 1023) for the left-right position (X)
- An analog reading (0 to 1023) for the up-down position (Y)
- The button's digital state (ON or OFF)
When you combine two analog values, they create 2-D coordinates. These coordinates are centered when the joystick isn’t moved. You can easily see the real direction of these coordinates by running a test code, which we’ll explain in the next section.
Some apps may use all three outputs, while others may use only some of them.
Pinout
A joystick has five pins.
- GND pin: connect to ground (0V)
- VCC pin: connect to power (5V)
- VRX pin: provides an analog signal for the horizontal position (X-axis)
- VRY pin: provides an analog signal for the vertical position (Y-axis)
- SW pin: comes from the joystick's push button. It is usually open. A pull-up resistor makes this pin go HIGH when not pressed and LOW when pressed.

How It Works
- Left/Right Movement
- When you move the joystick left or right, the voltage at the VRX pin changes.
- Move left makes the voltage go to 0 V.
- Move right raises the voltage to 5 V.
- The Arduino reads this voltage and turns it into a number from 0 to 1023. 0 V = 0, 5 V = 1023.
- Up/Down Movement
- Similarly, the VRY pin changes with up and down movement.
- Up makes the voltage go to 0 V.
- Down raises the voltage to 5 V.
- The Arduino reads this voltage too, turning it into a number from 0 to 1023, just like with VRX.
- Combined Movements
- In any direction, both VRX and VRY pins change based on how far you move on each axis.
- Pressing the Joystick
- Pushing the joystick down acts like pressing a built-in button.
- If you connect a pull-up resistor to the SW pin, the voltage changes from 5 V to 0 V when pressed.
- The Arduino reads this as a digital signal: HIGH (5 V) when not pressed, LOW (0 V) when pressed.
In short: moving the joystick changes the voltages at VRX and VRY, and the Arduino reads them as numbers from 0 to 1023. Pressing the joystick changes the voltage at the SW pin, and the Arduino reads it as either HIGH or LOW.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
How To Program For Joystick
The joystick has two parts: an analog part (for X and Y movement) and a digital part (a push button).
- To get the X and Y values, just use the analogRead() function on the analog input pin.
- For the digital part, the pushbutton is just a button. A simple and effective way is to use the ezButton library. This library helps you manage the button so it works reliably and also runs an internal system to keep the button steady. You can learn more in the Arduino Mega - Button tutorial. We will show you how to use the code in the next part of this tutorial.
After you read the values from the analog pins, you might need to change them into usable numbers. The next section shows sample code for this.
Arduino Mega Code
Here are some example programs for Arduino Mega:
- Example code: reads the joystick's analog values
- Example code: reads the joystick's analog values and checks if a button on the joystick is pressed
- Example code: converts an analog value into commands: MOVE LEFT, MOVE RIGHT, MOVE UP, MOVE DOWN
- Example code: converts analog values into angles to control two servo motors (like a pan-tilt camera)
Reads analog values from joystick
Detailed Instructions
Follow these steps, one by one.
- Connect the joystick to the Arduino Mega using the given diagram.
- Connect the Arduino Mega to your computer with a USB cable.
- Open the Arduino IDE on your computer.
- Choose the correct board (Arduino Mega) and the COM port.
- Copy the code above and paste it into the Arduino IDE.
- Click the Upload button in the Arduino IDE to upload the code to your Arduino Mega.
- Move the joystick to its edge and turn it in a circle (clockwise or counterclockwise).
- Check the results in the Serial Monitor.
- When you move the joystick, watch the Serial Monitor. If X is 0, that means left; the opposite is right. If Y is 0, that means up; the opposite is down.
Reads analog values and reads the button state from a joystick
Detailed Instructions
- Click the Libraries icon on the left side of the Arduino IDE.
- Find ezButton and its library from ArduinoGetStarted.com.
- Click the Install button to add the ezButton library.

- Copy the code and open it in the Arduino IDE.
- Click the Upload button in the Arduino IDE to upload the code to the Arduino Mega.
- Move the joystick left, right, up, or down.
- Press down on the top of the joystick.
- See the results on the Serial Monitor.
Converts analog value to MOVE LEFT/RIGHT/UP/DOWN commands
Detailed Instructions
- Copy the code above and open it in the Arduino IDE.
- Click the Upload button in the Arduino IDE to upload the code to the Arduino Mega.
- Move the joystick to the left, right, up, down, or in any direction.
- See the results in the Serial Monitor.
※ NOTE THAT:
Sometimes there may be no command, one command, or two commands at the same time (for example, UP and LEFT together).
Converts analog values to angles to control two servo motors
This tutorial explains how the Arduino Mega joystick works with a servo motor.