Built-in project · Arduino Uno R3

Joystick on A0, A1 and pin 8, on an Arduino Uno

A joystick module with X on A0, Y on A1 and its switch on pin 8 (internal pull-up): the sketch prints both axes and whether the stick is pressed. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $32. On Mokxi they're free.

Joystick on A0, A1 and pin 8 · Arduino Uno R3live0.000 s 0.00x
Drag the joystick
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Reading two analog axes
  • The center and range of an analog stick
  • Internal pull-up for a switch
  • Printing several values on one line
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The joystick is two potentiometers and a button in one. When it rests in the middle, each axis reads about 512. Pushed to the ends, the readings go toward 0 or 1023. X goes to A0 and Y goes to A1.

Pressing the stick down closes the switch SW, which connects pin 8 to ground. The sketch turns on the pin's pull-up, so pin 8 reads LOW while pressed. Twice a second the sketch prints X, Y and "pressed" or "up" to the serial monitor.

Parts list

3 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Thumb stick pin GNDArduino Uno R3 pin GND
Thumb stick pin SWArduino Uno R3 pin 8
Thumb stick pin VCCArduino Uno R3 pin 5V
Thumb stick pin VRXArduino Uno R3 pin A0
Thumb stick pin VRYArduino Uno R3 pin A1

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A joystick: X on A0, Y on A1, the press switch on pin 8.
//
//   VCC -> 5V   GND -> GND   VRx -> A0   VRy -> A1   SW -> pin 8
//
// Each axis is a potentiometer: centered it reads about 512, pushed to the
// ends near 0 or 1023. The switch joins SW to ground when the stick is
// pressed, so pin 8 uses the internal pull-up and reads LOW while pressed.
//
// Drag the stick, or click it.

const int X_PIN = A0;
const int Y_PIN = A1;
const int SW_PIN = 8;

void setup() {
  pinMode(SW_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: joystick on A0, A1 and pin 8");
}

void loop() {
  Serial.print("X: ");
  Serial.print(analogRead(X_PIN));
  Serial.print("  Y: ");
  Serial.print(analogRead(Y_PIN));
  Serial.print("  Z: ");
  Serial.println(digitalRead(SW_PIN) == LOW ? "pressed" : "up");
  delay(500);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Sets SW_PIN (pin 8) to INPUT_PULLUP so the pin stays HIGH until the stick is pressed. Then it starts serial at 9600 baud and prints a hello line.

void setup() {
  pinMode(SW_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: joystick on A0, A1 and pin 8");
}

loop sketch.ino

Prints the analogRead value of A0 and of A1, then the switch state. LOW on SW_PIN means "pressed", otherwise "up". It waits 500 ms before the next line.

void loop() {
  Serial.print("X: ");
  Serial.print(analogRead(X_PIN));
  Serial.print("  Y: ");
  Serial.print(analogRead(Y_PIN));
  Serial.print("  Z: ");
  Serial.println(digitalRead(SW_PIN) == LOW ? "pressed" : "up");
  delay(500);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • X and Y seem swapped This project
    Check VRX goes to A0 and VRY goes to A1.
  • The switch always reads pressed This project
    Check that SW goes to pin 8 and that the sketch still uses INPUT_PULLUP.
  • The axis reads about half scale at rest instead of exactly zero, and the values drift a little, so the cursor or motor creeps. Thumb stick
    Center is about half of the ADC range (about 512 on a 10 bit Arduino). Read it at startup and subtract that, then add a dead zone of a few counts.
  • The push button reads random values or always looks pressed. The switch pin is a plain contact to ground and has no pull-up on the stick. Thumb stick
    Set the switch pin to INPUT_PULLUP and treat LOW as pressed. Debounce it in code. Power the stick from the same voltage as the ADC reference.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.