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 might have seen a joystick used as a game controller, a toy controller, or even in large machines like excavator controllers.
The joystick has two potentiometers arranged in a square formation and one push button. It gives the following outputs:
An analog value (from 0 to 1023) for the horizontal position (X-coordinate)
An analog value (from 0 to 1023) for the vertical position (Y-coordinate)
A digital state of a button (HIGH or LOW)
When you combine two analog values, they form 2-D coordinates. These coordinates are centered when the joystick is not being moved. You can easily find out the actual direction of these coordinates by running a test code, which we will cover in the next section.
Some applications might use all three outputs, while others might use only some of them.
Pinout
A joystick uses five pins.
GND pin: connect to GND (0V)
VCC pin: connect to VCC (5V)
VRX pin: gives an analog value for the horizontal position (X-coordinate).
VRY pin: gives an analog value for the vertical position (Y-coordinate).
SW pin: comes from the joystick's pushbutton. It is usually open. Using a pull-up resistor makes this pin HIGH when not pressed and LOW when pressed.
How It Works
Left/Right Movement
When you move the joystick left or right, it changes the electrical signal (voltage) at the VRX pin.
Moving to the left lowers the voltage to 0 volts.
Moving to the right increases the voltage to 5 volts.
The Arduino reads this voltage and converts it to a number between 0 and 1023. So, 0V corresponds to 0, and 5V corresponds to 1023.
Up/Down Movement:
Similarly, moving the joystick up or down changes the voltage at the VRY pin.
Moving up lowers the voltage to 0 volts.
Moving down increases the voltage to 5 volts.
The Arduino reads this voltage too, converting it to a number between 0 and 1023, just like with the VRX pin.
Combined Movements:
When you move the joystick in any direction (not just purely left/right or up/down), it changes the voltage at both VRX and VRY pins based on its position along each axis.
Pressing the Joystick:
When you push the joystick down (like pressing a button), it activates an internal button inside the joystick.
If you connect a pull-up resistor to the SW pin, the voltage at this pin will change from 5V to 0V when pressed.
The Arduino reads this change as a digital signal, showing HIGH (5V) when not pressed and LOW (0V) when pressed.
In summary, moving the joystick changes voltages at VRX and VRY, which the Arduino reads as numbers between 0 and 1023. Pressing the joystick changes the voltage at the SW pin, which the Arduino reads as either HIGH or LOW.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
The joystick consists of two components: an analog part (X and Y axis) and a digital part (a pushbutton).
To read the values for the X and Y axes, simply use the function analogRead() on the analog input pin.
int xValue = analogRead(A1);int yValue = analogRead(A0);
For the digital part, which is a pushbutton: it's just a button. An easy and effective method is to use the ezButton library. This library helps manage button stability and also activates an internal system to keep the button stable. You can learn more in the Arduino UNO R4 - Button tutorial. We will show you how to use the code in the next part of this tutorial.
After getting the readings from analog pins, we might need to change them into manageable values. The following section will give example codes for this.
Arduino UNO R4 Code
Here are example codes for Arduino UNO R4:
Example code: gets analog values from joystick
Example code: gets analog values and checks button state on joystick
Example code: changes analog value to commands: MOVE_LEFT, MOVE_RIGHT, MOVE_UP, MOVE_DOWN
Example code: turns analog values into angles to manage two servo motors (like in a pan-tilt camera)
Reads analog values from joystick
/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-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 pinint xValue = 0; // To store value of the X axisint yValue = 0; // To store value of the Y axisvoidsetup() {Serial.begin(9600) ;}voidloop() {// read analog X and Y analog values xValue = analogRead(VRX_PIN); yValue = analogRead(VRY_PIN);// print data to Serial Monitor on Arduino IDESerial.print("x = ");Serial.print(xValue);Serial.print(", y = ");Serial.println(yValue);delay(200);}
Connect the joystick to Arduino UNO R4 according to the provided diagram.
Connect the Arduino Uno R4 board to your computer using a USB cable.
Launch the Arduino IDE on your computer.
Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
Copy the code above and open it in Arduino IDE
Click the Upload button in Arduino IDE to upload the code to your Arduino UNO R4
Push the joystick's thumb all the way to the edge, then turn it around in a circle (either clockwise or anti-clockwise)
Check the results on the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
When turning the joystick, keep an eye on the Serial Monitor. If the X value shows 0, remember this position as left; the opposite is right. If the Y value is 0, note this position as up; the opposite is down.
Reads analog values and reads the button state from a joystick
/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-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 pinezButtonbutton(SW_PIN);int xValue = 0; // To store value of the X axisint yValue = 0; // To store value of the Y axisint bValue = 0; // To store value of the buttonvoidsetup() {Serial.begin(9600) ;button.setDebounceTime(50); // set debounce time to 50 milliseconds}voidloop() {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 IDESerial.print("x = ");Serial.print(xValue);Serial.print(", y = ");Serial.print(yValue);Serial.print(" : button = ");Serial.println(bValue);}
Detailed Instructions
Go to the Libraries icon on the left side of the Arduino IDE.
Look for “ezButton” and locate the button library by ArduinoGetStarted.com
Press the Install button to add the ezButton library.
Copy the code and open it in Arduino IDE.
Click the Upload button in Arduino IDE to upload the code to the Arduino UNO R4.
Move the joystick thumb left, right, up, or down.
Press down on the top of the joystick.
Check the results on the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
Converts analog value to MOVE LEFT/RIGHT/UP/DOWN commands
/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-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 0x08int xValue = 0 ; // To store value of the X axisint yValue = 0 ; // To store value of the Y axisintcommand = COMMAND_NO;voidsetup() {Serial.begin(9600) ;}voidloop() {// read analog X and Y analog values xValue = analogRead(VRX_PIN); yValue = analogRead(VRY_PIN);// converts the analog value to commands// reset commandscommand = COMMAND_NO;// check left/right commandsif (xValue < LEFT_THRESHOLD)command = command | COMMAND_LEFT;elseif (xValue > RIGHT_THRESHOLD)command = command | COMMAND_RIGHT;// check up/down commandsif (yValue < UP_THRESHOLD)command = command | COMMAND_UP;elseif (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 commandif (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 with Arduino IDE.
Click the Upload button in Arduino IDE to upload the code to Arduino UNO R4.
Move the joystick thumb to the left, right, up, down, or any other direction.
Check the results on the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
※ NOTE THAT:
Sometimes, there might 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
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!