Project 8: Joystick Tester

Read Joystick Movements with Arduino

11/1/20265 min read

Introduction

Have you ever played a video game using a joystick or game controller? A joystick allows you to control movement in different directions by simply moving it with your hand.

In this project, you will connect a joystick module to your Arduino Uno and create a simple joystick tester. The Arduino will read the joystick's horizontal and vertical movements and detect when its built-in button is pressed. You will be able to see all these values in the Serial Monitor.

This project introduces you to analog inputs, digital inputs, and how controllers communicate with electronic systems.

What You Will Learn:

  • How a joystick module works.

  • How to read horizontal and vertical joystick movements.

  • How to use analogRead() to measure joystick positions.

  • How to read a joystick button using digitalRead().

  • How to use INPUT_PULLUP.

  • How to display joystick information in the Serial Monitor.

Joystick Module

1. Components Required

You will need the following components from your Arduino robotics kit:

  • 1 Arduino Uno

  • 1 joystick module

  • Jumper wires

  • 1 USB cable to connect and power the Arduino

2. Understanding the Joystick Module

A joystick is an input device that allows you to control movement in two directions.

Most joystick modules used with Arduino have two potentiometers and one push button.

The two potentiometers measure movement along two axes:

  • X-axis: Detects horizontal movement, such as left and right.

  • Y-axis: Detects vertical movement, such as up and down.

The module also has a push button that activates when you press the joystick down.

A typical Arduino joystick module has five connections:

  • VCC: Supplies power to the module.

  • GND: Connects to the Arduino's ground.

  • VRx: Sends the horizontal position signal.

  • VRy: Sends the vertical position signal.

  • SW: Sends the button signal.

The analog outputs usually provide readings between 0 and 1023 on an Arduino Uno.

When the joystick is approximately in the center, both axes usually read around 512. Moving the joystick changes these values.

3. Connecting the Joystick to Arduino

Let's connect the joystick module to your Arduino Uno.

Step 1: Identify the Joystick Pins

Look at your joystick module and identify its five main connections:

  • VCC

  • GND

  • VRx

  • VRy

  • SW

The labels may be printed directly on the module.

Step 2: Connect the Horizontal Axis

Connect the VRx pin to Arduino analog pin A0.

This allows the Arduino to read horizontal joystick movements.

Step 3: Connect the Vertical Axis

Connect the VRy pin to Arduino analog pin A1.

This allows the Arduino to read vertical joystick movements.

Step 4: Connect the Button

Connect the SW pin to Arduino digital pin 2.

The Arduino will use this pin to detect when the joystick is pressed.

Step 5: Connect Power

Connect the VCC pin to Arduino 5V and the GND pin to Arduino GND.

Your connections should be:

  • Joystick VCC → Arduino 5V

  • Joystick GND → Arduino GND

  • Joystick VRx → Arduino A0

  • Joystick VRy → Arduino A1

  • Joystick SW → Arduino digital pin 2

4. Writing the Arduino Code

Now let's program the Arduino to read the joystick's movements and button presses.

Open the Arduino IDE, Copy and paste the following code into your sketch:

Upload the code to your Arduino Uno.

Once the program starts running, open the Serial Monitor and set the baud rate to 9600.

Move the joystick in different directions and observe the changing X and Y values.

You can also press the joystick down to see the button status change.

5. Understanding the Code

Let's break the program into simple parts so you can understand how it works.

A. Defining the Joystick Pins

These lines assign names to the Arduino pins connected to the joystick.

  • joystickXPin represents analog pin A0.

  • joystickYPin represents analog pin A1.

  • joystickButtonPin represents digital pin 2.

Using descriptive names makes the program easier to understand.

B. Setting Up the Button and Serial Communication

The setup() function runs once when the Arduino starts or resets.

Inside this function:

  • pinMode(joystickButtonPin, INPUT_PULLUP) configures digital pin 2 as an input and enables the Arduino's internal pull-up resistor.

  • Serial.begin(9600) starts communication between the Arduino and your computer.

  • Serial.println() displays the project title in the Serial Monitor.

Because we are using INPUT_PULLUP, the button behaves like this:

  • HIGH means the button is released.

  • LOW means the button is pressed.

C. Reading the X-Axis

The analogRead() function reads the signal from the joystick's horizontal axis.

The reading is stored in the variable xValue.

On an Arduino Uno, the value normally ranges from 0 to 1023.

Moving the joystick left or right changes this value.

D. Reading the Y-Axis

This line reads the signal from the joystick's vertical axis.

The result is stored in yValue.

Moving the joystick up or down changes this reading.

Together, the X and Y values tell the Arduino the joystick's current position.

E. Detecting the Button Press

This line checks whether the joystick's button is pressed.

The digitalRead() function reads the digital state of pin 2.

Because we are using INPUT_PULLUP, a pressed button produces a LOW reading.

The result is stored in a Boolean variable called buttonPressed.

A Boolean variable can have only two possible values:

  • true

  • false

If the button is pressed, buttonPressed becomes true. Otherwise, it becomes false.

F. Displaying the Joystick Values

These instructions display the joystick information in the Serial Monitor.

The program displays:

  • The current X-axis reading.

  • The current Y-axis reading.

  • Whether the button is pressed or released.

The expression:

is called a conditional operator.

It displays PRESSED when buttonPressed is true and RELEASED when it is false.

G. Adding a Short Delay

This pauses the program for 150 milliseconds before taking the next reading.

The Arduino then repeats the process and displays the latest joystick position.

6. Understanding the Serial Monitor

The Serial Monitor allows you to observe the information sent by your Arduino while the program is running.

In this project, it displays the X-axis value, Y-axis value, and button status.

Example output:

BRIXBOTICS JOYSTICK TESTER
--------------------------
X: 512 | Y: 510 | Button: RELEASED
X: 120 | Y: 515 | Button: RELEASED
X: 890 | Y: 510 | Button: RELEASED
X: 515 | Y: 130 | Button: RELEASED
X: 510 | Y: 900 | Button: RELEASED
X: 512 | Y: 512 | Button: PRESSED

These are example readings. Your actual values may be different depending on your joystick module.

Here is how to understand them:

  • Values around 512 generally indicate that the joystick is near the center.

  • Values moving toward 0 indicate movement toward one end of an axis.

  • Values moving toward 1023 indicate movement toward the opposite end.

  • PRESSED means the joystick's push button is being pressed.

  • RELEASED means the button is not being pressed.

The direction associated with higher and lower readings can vary depending on how the joystick module is oriented.

7. Real-World Applications

Joysticks are used in many electronic devices because they provide a simple way to control movement and direction.

Video Game Controllers: Joysticks allow players to control characters, vehicles, and other objects in video games.

Robotics: A joystick can be used to manually control a robot's movement, direction, and actions.

Remote-Controlled Vehicles: Joysticks can control the movement of RC cars, boats, and other remote-controlled machines.

Industrial Equipment: Some machines use joystick controllers to operate mechanical arms, cranes, and other equipment.

Camera Control Systems: Joysticks can be used to move cameras and adjust their position in compatible camera systems.

Congratulations! You have successfully connected a joystick module to your Arduino Uno and built a joystick tester.

Through this project, you learned how to:

  • Understand the X-axis and Y-axis of a joystick.

  • Connect analog joystick outputs to an Arduino.

  • Read analog values using analogRead().

  • Detect button presses using digitalRead().

  • Understand how INPUT_PULLUP works.

  • Use Boolean variables to store button states.

  • Display live sensor information in the Serial Monitor.

You have now learned how to use a joystick as an input device. These concepts will help you build more interactive projects, including manually controlled robots and remote-control systems.