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 About 15 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.
What you'll learn
- Reading two analog axes
- Potentiometers as position sensors
- Internal pull up for a push switch
- Turning numbers into directions with thresholds
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
A thumb joystick holds two potentiometers, one for each direction. X goes to A0 and Y goes to A1. Centered, each reads about 512, and pushed to an end it reads near 0 or 1023.
The stick also has a push switch on pin 2. It is wired to ground, so the sketch turns on the internal pull up and the pin reads LOW when pressed.
Twice a second the sketch prints X, Y and the switch state, plus a word like "left" or "center" from a helper that compares the readings with 200 and 823.
Parts list
3 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Thumb stickIn Claw Machine Kit, Spaceship Panel Kit, Animatronic Creature Kit.
Kits that can build it
These 9 starter kits have every part in the circuit. Each kit's page lists what else it can build.
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 pin | Connects to |
|---|---|
| Thumb stick pin GND | Arduino Uno R3 pin GND |
| Thumb stick pin SW | Arduino Uno R3 pin 2 |
| Thumb stick pin VCC | Arduino Uno R3 pin 5V |
| Thumb stick pin VRX | Arduino Uno R3 pin A0 |
| Thumb stick pin VRY | Arduino 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.
// 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);
}
Code walkthrough
The key functions of the sketch, and what each one does.
direction sketch.ino
Takes the X and Y readings and returns a word. X below 200 is left, above 823 is right, then Y below 200 is up, above 823 is down. Anything else is center. The wide middle band keeps small wobbles from counting.
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";
}setup sketch.ino
Sets the switch pin to INPUT_PULLUP and starts the serial port.
void setup() {
pinMode(SW_PIN, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Mokxi Uno: joystick on A0, A1 and pin 2");
}loop sketch.ino
Reads both axes and the switch, prints them with the direction word, then waits 500 ms.
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);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The switch always reads pressed This project
Make sure SW goes to pin 2. With the pull up on, it should read up when not pressed. - Up and down are swapped This project
Check VRX is on A0 and VRY is on A1. - 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.