Arduino Nano Joystick Simulator: Wiring, Code and a Live Circuit
This is a two-axis joystick module wired to an Arduino Nano, 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 Nano
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 Nano 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 Nano’s 5V and each axis gives 0 to 1023. Ten bits against 5 V, 0 to 1023, the same as an Uno. A6 and A7 are extra analog inputs that cannot be digital pins at all.
Pins to leave alone on the Arduino Nano
A6 and A7 are analog only: pinMode(A6, OUTPUT) does nothing and digitalRead(A7) is not there to call. D0 and D1 are the USB serial port.
On a real bench
The Nano plugs into the breadboard itself, straddling the center channel, so the Joystick goes in a few columns along and every connection is a short jumper. The silkscreen labels its pins D2 to D13 and A0 to A7; in code D2 is simply 2. A Nano clone with a CH340 USB chip needs that driver on a real computer.
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 is the USB port, as on the Uno; a clone with a CH340 chip needs that driver on a real computer, which the simulator does not.
Common mistakes with the Joystick on the Arduino Nano
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 Nano?
Yes. The circuit at the top of this page is an Arduino Nano 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 Nano?
Ten bits against 5 V, 0 to 1023, the same as an Uno. A6 and A7 are extra analog inputs that cannot be digital pins at all. 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.