Analog Joystick Reference
A 2-axis analog joystick (two potentiometers) with an integrated push button, selected by pressing straight down on the stick.
Pin names
| Name | Description |
|---|---|
| VCC | Power (5V) |
| VERT | Y-axis analog output |
| HORZ | X-axis analog output |
| SEL | Push-button — momentary switch to GND when pressed |
| GND | Ground |
Attributes
This component has no configurable attributes.
Usage
Connect VCC to 5V, GND to GND, VERT/HORZ to analog input pins, and SEL to
a GPIO configured with INPUT_PULLUP (it reads LOW when pressed).
const int PIN_VERT = A0;
const int PIN_HORZ = A1;
const int PIN_SEL = 2;
void setup() {
pinMode(PIN_SEL, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int x = analogRead(PIN_HORZ);
int y = analogRead(PIN_VERT);
bool pressed = digitalRead(PIN_SEL) == LOW;
Serial.print("X: "); Serial.print(x);
Serial.print(" Y: "); Serial.print(y);
Serial.print(" Btn: "); Serial.println(pressed ? "pressed" : "released");
delay(200);
}
The centre rest position typically reads around 512 on each axis (out of 0–1023) — read both axes at startup to calibrate for hardware variance.
Examples
| Description | Attrs |
|---|---|
| Default joystick | {} |
See also
- Potentiometer — single-axis equivalent of each joystick axis
- Pushbutton — same digital press behaviour as the joystick's SEL pin