Built-in project · Arduino Uno R3

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

A thumb joystick module with X on A0, Y on A1 and its press switch on pin 2: the sketch prints both axes and the switch, and names the direction. 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 Runs in your browser. Free, and no account needed.

Joystick on A0, A1 and pin 2 · 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.

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
// Joystick module: X on A0, Y on A1, the press switch on pin 2.
//
//   VCC -> 5V   GND -> GND   VRx -> A0   VRy -> A1   SW -> pin 2
//
// The stick holds two potentiometers, one per axis. Centered, each sits
// half way and reads about 512; pushed to an end it reads near 0 or near
// 1023. Pressing the stick down closes a switch to ground, so pin 2 has the
// pull-up turned on and reads LOW while the stick is pressed.
//
// Drag the stick on the canvas, or click it to press it.

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

const char *direction(int x, int y) {
  if (x < 200) return "left";
  if (x > 823) return "right";
  if (y < 200) return "up";
  if (y > 823) return "down";
  return "center";
}

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

void loop() {
  int x = analogRead(X_PIN);
  int y = analogRead(Y_PIN);
  bool pressed = digitalRead(SW_PIN) == LOW;
  Serial.print("X: ");
  Serial.print(x);
  Serial.print("  Y: ");
  Serial.print(y);
  Serial.print("  switch: ");
  Serial.print(pressed ? "pressed" : "up");
  Serial.print("  ");
  Serial.println(direction(x, y));
  delay(500);
}

Parts list

3 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; Thumb stick pin GND
  • Arduino Uno R3 pin 2; Thumb stick pin SW
  • Arduino Uno R3 pin 5V; Thumb stick pin VCC
  • Arduino Uno R3 pin A0; Thumb stick pin VRX
  • Arduino Uno R3 pin A1; Thumb stick pin VRY

Change it and keep it

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