Arduino Mega - Joystick

This guide shows how to use a joystick with an Arduino Mega board. We will cover:

Arduino Mega and Joystick

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)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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.
Joystick Pinout

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

The wiring diagram between Arduino Mega Joystick

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.
int xValue = analogRead(A1); int yValue = analogRead(A0);
  • 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

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-joystick */ #define VRX_PIN A1 // The Arduino UNO R4 pin connected to VRX pin #define VRY_PIN A0 // The Arduino UNO R4 pin connected to VRY pin int xValue = 0; // To store value of the X axis int yValue = 0; // To store value of the Y axis void setup() { Serial.begin(9600) ; } void loop() { // read analog X and Y analog values xValue = analogRead(VRX_PIN); yValue = analogRead(VRY_PIN); // print data to Serial Monitor on Arduino IDE Serial.print("x = "); Serial.print(xValue); Serial.print(", y = "); Serial.println(yValue); delay(200); }

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.
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • 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

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-joystick */ #include <ezButton.h> #define VRX_PIN A1 // The Arduino UNO R4 pin connected to VRX pin #define VRY_PIN A0 // The Arduino UNO R4 pin connected to VRY pin #define SW_PIN 2 // The Arduino UNO R4 pin connected to SW pin ezButton button(SW_PIN); int xValue = 0; // To store value of the X axis int yValue = 0; // To store value of the Y axis int bValue = 0; // To store value of the button void setup() { Serial.begin(9600) ; button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first // read analog X and Y analog values xValue = analogRead(VRX_PIN); yValue = analogRead(VRY_PIN); // Read the button value bValue = button.getState(); if (button.isPressed()) { Serial.println("The button is pressed"); // TODO do something here } if (button.isReleased()) { Serial.println("The button is released"); // TODO do something here } // print data to Serial Monitor on Arduino IDE Serial.print("x = "); Serial.print(xValue); Serial.print(", y = "); Serial.print(yValue); Serial.print(" : button = "); Serial.println(bValue); }

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.
Arduino Mega button 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.
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Converts analog value to MOVE LEFT/RIGHT/UP/DOWN commands

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-joystick */ #define VRX_PIN A1 // The Arduino UNO R4 pin connected to VRX pin #define VRY_PIN A0 // The Arduino UNO R4 pin connected to VRY pin #define LEFT_THRESHOLD 400 #define RIGHT_THRESHOLD 800 #define UP_THRESHOLD 400 #define DOWN_THRESHOLD 800 #define COMMAND_NO 0x00 #define COMMAND_LEFT 0x01 #define COMMAND_RIGHT 0x02 #define COMMAND_UP 0x04 #define COMMAND_DOWN 0x08 int xValue = 0 ; // To store value of the X axis int yValue = 0 ; // To store value of the Y axis int command = COMMAND_NO; void setup() { Serial.begin(9600) ; } void loop() { // read analog X and Y analog values xValue = analogRead(VRX_PIN); yValue = analogRead(VRY_PIN); // converts the analog value to commands // reset commands command = COMMAND_NO; // check left/right commands if (xValue < LEFT_THRESHOLD) command = command | COMMAND_LEFT; else if (xValue > RIGHT_THRESHOLD) command = command | COMMAND_RIGHT; // check up/down commands if (yValue < UP_THRESHOLD) command = command | COMMAND_UP; else if (yValue > DOWN_THRESHOLD) command = command | COMMAND_DOWN; // NOTE: AT A TIME, THERE MAY BE NO COMMAND, ONE COMMAND OR TWO COMMANDS // print command to serial and process command if (command & COMMAND_LEFT) { Serial.println("COMMAND LEFT"); // TODO: add your task here } if (command & COMMAND_RIGHT) { Serial.println("COMMAND RIGHT"); // TODO: add your task here } if (command & COMMAND_UP) { Serial.println("COMMAND UP"); // TODO: add your task here } if (command & COMMAND_DOWN) { Serial.println("COMMAND DOWN"); // TODO: add your task here } }

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.
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ 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.

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!