Arduino Uno Joystick Simulator: Wiring, Code and a Live Circuit
This is a two-axis joystick module wired to an Arduino Uno, read live: the serial monitor prints both axes, the direction the stick points and the word pressed while the button is down.
- 5 V logic
- No library: analogRead()
- X on pin A0, Y on pin A1
- Button on pin 2
Wiring the Joystick to the Arduino Uno
Five wires: 5V, GND, VRX to pin A0, VRY to pin A1 and SW to pin 2. In the editor, drag the stick on the drawing and press it.
Every connection in the circuit above, with the board’s own pin names.
| Joystick pin | Arduino Uno pin | Why |
|---|---|---|
| VCC | 5V | Powers both tracks. |
| GND | GND | The other end of both tracks. |
| VRX | pin A0 | The X wiper, an analog input. |
| VRY | pin A1 | The Y wiper, an analog input. |
| SW | pin 2 | Shorts to GND when the stick is pressed; the sketch turns on the internal pull-up. |
Power
The module is two potentiometers and a switch, so it runs from the Arduino Uno’s 5V and each axis gives 0 to 1023. The Uno’s ADC is ten bits against its 5 V rail, so analogRead() gives 0 to 1023.
Pins to leave alone on the Arduino Uno
Pins 0 and 1 are the USB serial port on a real Uno, so anything wired there fights the upload and the serial monitor. Pin 13 carries the onboard LED.
On a real bench
On a real bench the Uno stands beside the breadboard: the Joystick goes into the breadboard and male-to-male jumpers run from the Uno’s female headers to its rows. Power the Uno from USB while you build, and unplug it before you move a wire.
The code, and what it does.
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
const int X_PIN = A0; // VRX, pin A0
const int Y_PIN = A1; // VRY, pin A1
const int SW_PIN = 2; // the button, pin 2
void setup() {
Serial.begin(9600);
pinMode(SW_PIN, INPUT_PULLUP); // LOW while the stick is pressed
}
void loop() {
int xr = analogRead(X_PIN); // 0 to 1023
int yr = analogRead(Y_PIN);
bool pressed = digitalRead(SW_PIN) == LOW;
const char* dir = "Center";
if (xr < 1023 / 4) dir = "Left";
else if (xr > 1023 / 4 * 3) dir = "Right";
else if (yr < 1023 / 4) dir = "Up";
else if (yr > 1023 / 4 * 3) dir = "Down";
Serial.print("X: ");
Serial.print(xr);
Serial.print(" Y: ");
Serial.print(yr);
Serial.print(" ");
Serial.print(dir);
Serial.println(pressed ? " pressed" : "");
delay(250);
}
Each axis is a potentiometer with its ends on VCC and GND, so analogRead() gives about half of 1023 with the stick centered and nears 0 or 1023 at the ends of travel. Real sticks rest a few counts off center, so the sketch uses wide bands (a quarter and three quarters of the range) rather than an exact middle.
The button is a plain switch to ground, so INPUT_PULLUP makes it read HIGH at rest and LOW while the stick is pressed down.
Serial.begin(9600) opens the serial monitor. Serial goes over the USB cable, and the serial monitor in the editor reads it at whatever baud rate the sketch sets.
Common mistakes with the Joystick on the Arduino Uno
No pull-up on the button
Without INPUT_PULLUP the SW pin floats and reports presses nobody made. Most modules have no resistor of their own.
Expecting an exact center
The rest position is a few counts either side of the middle and differs per axis. Use a dead zone, or read the rest position at startup and measure from it.
An axis that runs backwards
Modules are mounted in different directions. Swap the two pins in the code, or flip a reading with 1023 - reading.
Questions and answers
Can I simulate a joystick with an Arduino Uno?
Yes. The circuit at the top of this page is an Arduino Uno with a joystick module, running in your browser on a simulated ATmega328P. Drag the stick on the drawing and both axes change; press it and the button line goes low. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
What values does the joystick give on the Arduino Uno?
The Uno’s ADC is ten bits against its 5 V rail, so analogRead() gives 0 to 1023. The button reads LOW when pressed.
Is it a real analog stick?
Yes, in the sense that matters: two variable voltages and a switch. Under the cap is a pair of 10 k potentiometers, so any code that reads two analog pins reads it.
How do I move a servo with the stick?
Read an axis and call servo.write(map(reading, 0, 1023, 0, 180)). The Servo page on this site has the wiring.
Build your own Joystick project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.