Arduino - Joystick
This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://arduinogetstarted.com/tutorials/arduino-joystick
This build reads a joystick module with Arduino. The joystick has two potentiometers set at 90 degrees to each other for X and Y position, plus a built-in pushbutton. You get two analog readings (0-1023) for position and one digital reading for the button, useful for direction control, menu navigation, or driving servos in a pan-tilt setup.
Supplies
Everything below is what the build uses. The links go to the parts we make and test.
- Official Arduino Uno
- DIYables STEM V3, Fully Compatible with Arduino Uno R3
- USB 2.0 cable type A/B (for USB-A PC)
- USB 2.0 cable type C/B (for USB-C PC)
- Joystick
- Jumper Wires
- Screw Terminal Block Shield for Arduino Uno
- Sensors/Servo Expansion Shield for Arduino Uno
- Breadboard Shield for Arduino Uno
- Enclosure for Arduino Uno
- Prototyping Base Plate & Breadboard Kit for Arduino UNO
- DIYables STEM V3 Starter Kit (Arduino included)
- DIYables Sensor Kit (18 sensors/displays)
Wiring
Connect the joystick's GND pin to Arduino GND and its VCC pin to Arduino 5V. Connect VRX to analog pin A1 and VRY to analog pin A0. Connect SW, the pushbutton output, to a digital pin. SW is normally open and needs a pull-up so it reads HIGH when not pressed and LOW when pressed. For a permanent build, a screw terminal shield on the Arduino makes the wires easier to secure.
How It Works
Moving the thumbstick left/right changes the voltage on VRX between 0 and 5V, read by Arduino's analog input as a value from 0 to 1023, with 0 at one end and 1023 at the other. Moving it up/down does the same on VRY. Moving diagonally changes both at once, in proportion to the position on each axis. Pressing the thumbstick down closes the internal pushbutton; with a pull-up resistor the SW pin's digital reading goes from HIGH to LOW.
Code
The analog axes are read directly with analogRead() on the VRX and VRY pins. The pushbutton is handled with the ezButton library, which adds debounce and can enable the internal pull-up. Example sketches build on these two reads: one just prints X and Y, one adds the button state, one converts the analog values into MOVE_LEFT, MOVE_RIGHT, MOVE_UP, MOVE_DOWN commands, and another converts the readings into angles to drive two servos, for example in a pan-tilt camera.
arduino-joystick.ino - the sketch from the tutorial, unchanged.
Full Tutorial
The complete tutorial, wiring diagram and code explanation: https://arduinogetstarted.com/tutorials/arduino-joystick