Built-in project · Arduino Uno R3

Joystick module: A0, A1 and pin 3, on an Arduino Uno

A joystick with X on A0, Y on A1 and its push switch on pin 3: the sketch prints all three five times a second. 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 $29. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Two analog inputs from one module
  • Reading analog values with analogRead
  • A push switch with INPUT_PULLUP
  • Printing several values on one line
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 2

How it works

The joystick module holds two potentiometers, one for each direction, and a push switch. VRX goes to analog pin A0, VRY goes to A1, and SW goes to pin 3. Each axis gives a number around 512 in the middle.

Pin 3 uses the internal pull-up, so it reads 1 normally and 0 while the stick is pressed down. Five times a second the sketch prints the X value, the Y value and the switch reading in one line.

Parts list

2 parts, plus the jumper wires. They retail for about $29; 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 SWArduino Uno R3 pin 3
Thumb stick pin VCCArduino Uno R3 pin 5V
Thumb stick pin GNDArduino Uno R3 pin GND
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 module: two potentiometers and a switch.
//
//   VRx -> A0   VRy -> A1   SW -> pin 3 (internal pull-up)   + -> 5V   GND -> GND
//
// Each axis reads about 512 in the middle. The switch joins SW to ground when
// the stick is pressed down.
//
// Drag the joystick, or click it to press.

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

void loop() {
  Serial.print(analogRead(A0));
  Serial.print(", ");
  Serial.print(analogRead(A1));
  Serial.print(", ");
  Serial.println(digitalRead(3));
  delay(200);
}

Code walkthrough

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

setup sketch.ino

Sets pin 3 to INPUT_PULLUP so the switch needs no outside resistor. Then it starts the serial port at 9600 baud and prints a hello message. The analog pins need no setup.

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

loop sketch.ino

Prints analogRead(A0), a comma, analogRead(A1), another comma, and digitalRead(3). Then delay(200) pauses for a fifth of a second. Drag the joystick to change the numbers, or click to press.

void loop() {
  Serial.print(analogRead(A0));
  Serial.print(", ");
  Serial.print(analogRead(A1));
  Serial.print(", ");
  Serial.println(digitalRead(3));
  delay(200);
}

Troubleshooting

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

  • The two axes seem swapped. This project
    Check that VRX goes to A0 and VRY goes to A1. The first number printed is A0.
  • The switch value is 1 when pressed. This project
    It reads 0 when pressed, because the pull-up holds it at 1 otherwise.
  • 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.