In the past, we’ve looked at how a potentiometer can be used to control your Arduino projects. To add a bit more complexity, an Arduino joystick module can be used to get even more input data for your project. The joystick is made up of two potentiometers, at right angles to each other, that let you read coordinates in 2D space. Here’s how to add one to your projects.
A simple Arduino joystick works using potentiometers, like the kind found in dials or sliders, to read values along two perpendicular axes. By combining the two values, you can get x,y coordinates that correspond to the position of the joystick. With that data, you can take action based on where the joystick is pointed.
Like most potentiometers, both axes are measured on a scale of 0 to 1023. Since joysticks start in the center and move left/right and up/down, the default starting position will be around 511 for both axes. There are several types of Arduino joystick controllers out there, and even a joystick shield or two. For our purposes, we’ll be looking at a basic analog Arduino joystick module.
This Arduino joystick comes with five pins: one for power, one for ground, two pins for the X and Y axes, and a pin for a button press. We’ll only be using the first four today, but you can try expanding your project with button presses in the future. You can also find joystick shields that have buttons as well, so you have all the inputs you need to control an entire game.
For now, though, we’ll demonstrate how the joystick works by using it to control four LEDs. Our goal will be that when the joystick points up, down, left, and right, a different LED will light up for each direction. This will be easy to detect, since each of the four directions is at the far end of one of the two potentiometer axes.
For this project, we’re going to use four differently colored LEDs as indicators to show that our project is working, on top of the Arduino joystick module. Here’s the full list of parts you’ll need:
Arduino Uno | ![]() |
Joystick | ![]() |
Four LEDs (preferably differently colored if you have them) |
![]() |
Four 220 Ohm resistors | ![]() |
Wires | ![]() |
Breadboard | ![]() |
USB cable | ![]() |
Arduino IDE | ![]() |
Once you have all your materials together, upload the code from the next section to your Uno, then move on to the wiring section.
To get started, upload the following sketch to your Arduino Uno.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#define joyX A0 #define joyY A1 int blueLED = 7; int redLED = 6; int yellowLED = 5; int whiteLED = 4; void setup() { Serial.begin(9600); pinMode(blueLED,OUTPUT); pinMode(redLED,OUTPUT); pinMode(yellowLED,OUTPUT); pinMode(whiteLED,OUTPUT); } void loop() { int xValue; int yValue; xValue = analogRead(joyX); yValue = analogRead(joyY); Serial.print("X: "); Serial.print(xValue); Serial.print("\t Y: "); Serial.println(yValue); if (yValue > 1020){ digitalWrite(blueLED, HIGH); } else { digitalWrite(blueLED, LOW); } if (yValue < 2){ digitalWrite(redLED, HIGH); } else { digitalWrite(redLED, LOW); } if (xValue > 1020){ digitalWrite(yellowLED, HIGH); } else { digitalWrite(yellowLED, LOW); } if (xValue < 2){ digitalWrite(whiteLED, HIGH); } else { digitalWrite(whiteLED, LOW); } } |
Next, let’s go through the sketch to highlight the important parts you need to understand.
1 2 3 4 5 6 7 |
#define joyX A0 #define joyY A1 int blueLED = 7; int redLED = 6; int yellowLED = 5; int whiteLED = 4; |
In this section, we’ll define the six pin variables. The first two pins, A0 and A1, will store the X and Y variables coming in from the joystick module. The next four lines define four LED pins, each of which will be a different color for this sketch.
1 2 3 4 5 6 7 |
void setup() { Serial.begin(9600); pinMode(blueLED,OUTPUT); pinMode(redLED,OUTPUT); pinMode(yellowLED,OUTPUT); pinMode(whiteLED,OUTPUT); } |
In this section, we’ll start the Serial monitor, which will let us see what values the joystick outputs as you move the joystick module around. The four pinMode() functions will also assign the LED pins as outputs.
1 2 3 4 5 6 7 8 9 |
int xValue; int yValue; xValue = analogRead(joyX); yValue = analogRead(joyY); Serial.print("X: "); Serial.print(xValue); Serial.print("\t Y: "); Serial.println(yValue); |
Inside the loop() section, we’ll first create the variables xValue and yValue to store the values from the joystick module. As you move the joystick around, these values should change in real time.
We’ll also print the X and Y values to the serial monitor. If you want to see which values correspond with different positions, open the serial monitor while the Arduino is plugged in over USB.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
if (yValue > 1020){ digitalWrite(blueLED, HIGH); } else { digitalWrite(blueLED, LOW); } if (yValue < 2){ digitalWrite(redLED, HIGH); } else { digitalWrite(redLED, LOW); } if (xValue > 1020){ digitalWrite(yellowLED, HIGH); } else { digitalWrite(yellowLED, LOW); } if (xValue < 2){ digitalWrite(whiteLED, HIGH); } else { digitalWrite(whiteLED, LOW); } |
Finally, we’ll use four if() statements to control the colored LEDs. Each one is assigned to a value of either “> 1020” or “< 2” (we use these values to reduce flicker). For example, “> 1020” on the y axis corresponds with the joystick pointing up, and so it will turn on the blue LED. Likewise, “< 2” on the y axis corresponds with the joystick pointing down, and so the red LED will turn on, and so on.
The wiring for this project is relatively straightforward. Each LED will need its own 220 Ohm resistor, so make sure you have a few of those handy. To wire up the project, follow these steps. First, on the joystick module:
Next, for the LEDs, we’ll connect one color to the digital pins on the Arduino from 4 through 7. For each of the pins, do the following steps:
Once everything is wired up and you have the software uploaded, you can turn on your Arduino and start moving the joystick around. If you have the serial monitor open, you should see the X and Y values change as you move the joystick around. Try pointing the joystick up, down, left, and right to see which LED lights up. You can expand this project by triggering different actions based on where the joystick is pointing.