Ultimate Guide to Using a Joystick with Arduino

Looking to add a joystick to your Arduino project but not sure where to start? Look no further! In this comprehensive tutorial, we’ll walk you through everything you need to know to get your joystick up and running with Arduino.

We’ll start with an overview of joysticks and how they work, then move on to connecting the joystick to your Arduino board. You’ll learn how to read the joystick’s input and use it in your project. We will also cover important topics like debouncing, calibration, and how to use libraries to simplify your code. By the end of this tutorial, you’ll have a solid understanding of how to use a joystick with Arduino, as well as the skills you need to apply it to your own unique projects.

Whether you’re a beginner or an experienced Arduino user, this tutorial has something for everyone. So join us as we dive into the exciting world of Arduino joysticks!

What is a Joystick?

A Joystick module typically consists of two potentiometers, one for the X-axis and one for the Y-axis, as well as a push-button switch. These potentiometers are used to measure the position of the joystick along the X and Y axes, while the push-button switch is used to detect when the joystick is pressed.

What is a potentiometer?

A potentiometer is an electronic component that is commonly used to measure or control the level of voltage in a circuit. It is essentially a variable resistor with three terminals: two fixed outer terminals and a movable middle terminal.

The potentiometer works by varying the resistance between its two outer terminals as the movable middle terminal is rotated. This variation in resistance allows the user to control the voltage level in the circuit. Potentiometers are commonly used in audio equipment, such as volume controls and tone controls, as well as in various other electronic devices where a variable voltage needs to be controlled.

In our case, we are using it to measure user input and translate into an action (moving a device, playing a videogame, etc).

How are potentiometers used in a Joystick

A Joystick will have 2 potentiometers (one for each axis along a plane). The pictures below show one of the potentiometers. As you can see, movement of the joystick leads to a rotation of the potentiometer, which as we will see later, will result in a change of the output voltage of that potentiometer, which can be used as a signal for our project.

Arduino joystick

How is the pushbutton used in a Joystick

The joystick’s body is connected to a pushbutton, so when you press down on the joystick, the pushbutton is engaged, and there is a change in voltage signal at the output pin, which we can use for our projects.

Arduino joystick

Joystick device and pins

Output Pins

VRX (X-axis output pin): This pin is connected to the wiper of the potentiometer that measures the position of the joystick along the X-axis. The voltage on this pin varies based on the position of the joystick, with the voltage ranging from 0V to the maximum voltage (5V).

Arduino joystick pins

VRY (Y-axis output pin): This pin is connected to the wiper of the potentiometer that measures the position of the joystick along the Y-axis. The voltage on this pin varies based on the position of the joystick, with the voltage ranging from 0V to the maximum voltage (5V).

SW (Push-button switch output pin): This pin is connected to the push-button switch on the Joystick module. When the button is not pressed, the voltage on this pin is equal to the maximum voltage (5V). When the button is pressed, the voltage on this pin is equal to 0V.

Other Pins

The GND and +5V pins are connected to GND and 5V on your Micro Controller (i.e. Arduino UNO).

Connecting Joystick to Arduino UNO

GND – Connect to Arduino GND
VCC – Connect to Arduino 5V
VRX – Connect to Arduino A0
VRY – Connect to Arduino A1
SW – Connect to Arduino IO Pin #2

Arduino joystick connections

First Test of Joystick with Arduino UNO

Use the code below to complete the first test of the Joystick. This code reads the X and Y values from the joystick and the state of the button and then prints these values to the serial monitor.

After running the code, open the Serial Monitor, and the output should behave as follows:

Below is an example from me moving the Joystick straight up, and another from doing nothing. It looks like some calibration might be needed. You can also see that the button also showed a change in value even though I didn’t push it. Some work needed to improve that as well.

Joystick at Center (Doing Nothing)

Joystick pushed Up

Calibrating a Joystick

Calibrating an Arduino joystick is an important step to ensure accurate and consistent readings from the joystick. Calibration involves determining the minimum and maximum values of the joystick input and mapping those values to a desired range.

From the previous example, make sure to take note of the output values for both axis when the joystick is placed left, center, right, and up, center, down. Below are my uncalibrated values:

Then copy and paste the code below and replace the first 6 constants with your values (you can see how I entered them)?

Fixing the Push Button Variability

In order to address the issue of the pushbutton reading flickering, we will need to change the pinmode setup on our Arduino to be INPUT_PULLUP.

Pullup Pins

A pullup resistor is a resistor connected between the input pin and the positive power supply voltage (Vcc), and it serves to ensure that the input pin is in a known state when no external signal is present. When a switch or sensor is connected to an input pin configured with a pullup resistor, the input pin is pulled up to the voltage level of Vcc when the switch or sensor is open (not activated).

With the Arduino UNO, the digital pins can be configured as input with a pullup resistor using the pinMode() function. For example, to configure digital pin 2 as input with a pullup resistor, you would write:

When the input pin is configured with a pullup resistor, the internal resistance between the pin and Vcc is typically between 20kΩ and 50kΩ, depending on the specific model of Arduino board. This means that the input pin will read HIGH when it is not connected to anything or when the switch or sensor is open, and it will read LOW when the switch or sensor is closed.

Using this to solve our issue

In our example, we will go ahead and define the mode of Pin 2 the same way as the example above (INPUT_PULLUP). See the code below for full example and then read the results of the test via Serial Monitor. As you can see, the variability / flickering problem of the push button is now solved.

Example – Joystick with NeoRing

In this example, we will use our joystick to control individual LED lights on a NeoPixel LED Ring (by AdaFruit) with 16 LEDs. Depending on the position of the joystick, a specific area of the LED ring will light up. The main one lighting up at max intensity, and the adjacent ones with lighter intensity. If the joystick is near the center, the whole ring will light up at low intensity, and if the push button is pressed, the color that the LED lights up will change, cycling between several colors with each press of the button.

Connecting our NeoRing LED to Arduino UNO

Install Required Arduino Library

Before we can run this example, we need to install the Adafruit NeoPixel Library. See steps in the images below:

Arduino Code

2 Replies to “Ultimate Guide to Using a Joystick with Arduino”

  1. I truly admired the work you’ve put in here. The design is refined, your authored material stylish, however, you seem to have acquired some trepidation about what you intend to present next. Undoubtedly, I’ll revisit more regularly, similar to I have nearly all the time, in the event you sustain this rise.

  2. What a fantastic resource! The articles are meticulously crafted, offering a perfect balance of depth and accessibility. I always walk away having gained new understanding. My sincere appreciation to the team behind this outstanding website.

Leave a Reply

Your email address will not be published. Required fields are marked *