Arduino Analog Joystick: Wiring, Code and Simulator

To build this Arduino Analog Joystick project you need an Arduino Uno and an analog joystick. An analog joystick: HORZ on A0, VERT on A1, and its push-switch on pin 2. Make the 5 connections in the wiring table, upload the Arduino C++ code, and move the X and Y sliders, or click the stick to press it. The serial monitor shows the readings.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1analog joystickHORZ and VERT to analog pins (about 512 at rest; left and up read higher), SEL is a button to GND. VCC to 5V.

Wiring

#FromTo
1Arduino Uno 5Vanalog joystick VCC
2Arduino Uno GNDanalog joystick GND
3Arduino Uno A0analog joystick HORZ
4Arduino Uno A1analog joystick VERT
5Arduino Uno pin 2analog joystick SEL

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add an analog joystick from the parts palette.
  3. Wire 1. Connect Arduino Uno 5V to analog joystick VCC.
  4. Wire 2. Connect Arduino Uno GND to analog joystick GND.
  5. Wire 3. Connect Arduino Uno A0 to analog joystick HORZ.
  6. Wire 4. Connect Arduino Uno A1 to analog joystick VERT.
  7. Wire 5. Connect Arduino Uno pin 2 to analog joystick SEL.
  8. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  9. Run it. Press Run. Move the X and Y sliders, or click the stick to press it. The serial monitor shows the readings.

Arduino C++ code

// Joystick: HORZ on A0, VERT on A1, the push-switch SEL on pin 2.
void setup() {
  pinMode(2, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  int x = analogRead(A0);
  int y = analogRead(A1);
  bool pressed = digitalRead(2) == LOW;
  Serial.print("x=");
  Serial.print(x);
  Serial.print(" y=");
  Serial.print(y);
  Serial.println(pressed ? " pressed" : "");
  delay(200);
}

What to try

Move the X and Y sliders, or click the stick to press it. The serial monitor shows the readings.

Frequently asked questions

How do I wire the Analog Joystick project on an Arduino Uno?

Make 5 connections: Arduino Uno 5V to analog joystick VCC; Arduino Uno GND to analog joystick GND; Arduino Uno A0 to analog joystick HORZ; Arduino Uno A1 to analog joystick VERT; Arduino Uno pin 2 to analog joystick SEL.

What parts do I need for this Arduino project?

An Arduino Uno and an analog joystick.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

An analog joystick: HORZ on A0, VERT on A1, and its push-switch on pin 2.

More Arduino Uno projects