# Electronic components: Analog Joystick

> Learn how the analog joystick works with Arduino: read its VRx and VRy axes with analogRead from 0 to 1023, and detect clicks through the SW switch pin.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2021-03-15 | Topics: [Arduino](https://flaviocopes.com/tags/electronics/) | Canonical: https://flaviocopes.com/electronics-components-analog-joystick/

The analog joystick is one of those electronic components you have certainly used while playing video games:

![Two black analog joystick modules side by side on a white breadboard and black foam pad](https://flaviocopes.com/images/electronics-components-analog-joystick/IMG_3645.jpg)

![Side view of analog joystick modules showing the internal components and circuit board details](https://flaviocopes.com/images/electronics-components-analog-joystick/IMG_3646.jpg)

![Top view of analog joystick modules with thumb sticks visible, showing circular range of motion](https://flaviocopes.com/images/electronics-components-analog-joystick/IMG_3647.jpg)

![Bottom view of analog joystick module showing the PCB with labeled pins and electronic components](https://flaviocopes.com/images/electronics-components-analog-joystick/IMG_3648.jpg)

You can move the joystick around, and you can also click it from top to bottom:

![Finger pressing down on analog joystick thumb stick to demonstrate the clickable switch function](https://flaviocopes.com/images/electronics-components-analog-joystick/IMG_3650.jpg)

![Close-up of finger pressing analog joystick showing the switch mechanism being activated](https://flaviocopes.com/images/electronics-components-analog-joystick/IMG_3649.jpg)

Any action performed will send the appropriate electronic signals to the circuit it's connected to.

The 5 pins of the joystick are:

- `GND`, the input LOW signal
- `+5V`, the input HIGH signal, can also be 3.3V when using a 3.3V based device
- `VRx`, the analog signal that represents the position of the joystick on the `x` axis
- `VRy`, the analog signal that represents the position of the joystick on the `y` axis
- `SW`, short for _switch_, is the digital value `LOW` when pressed, otherwise `HIGH`

![Close-up of joystick pin labels showing GND, +5V, VRx, VRy, and SW pins with connection wires](https://flaviocopes.com/images/electronics-components-analog-joystick/IMG_3653.jpg)

You connect `VRx` and `VRy` to an analog input pin to get their value.

Analog inputs range from `0` to `1023` since they use a `10` bits resolution.

When watching the joystick with the pins on the left, the `X` axis values assumes values from `0` (full left) to `1023` (full right) and `498` in the middle. The `Y` axis values assumes values from `0` (top) to `1023` (bottom) and `498` in the middle.

![Diagram showing joystick X and Y axis value ranges from 0 to 1023 with center position at 498](https://flaviocopes.com/images/electronics-components-analog-joystick/Immagine_PNG-8C0721DA8F3C-1.png)

Assuming `VRx` is connected to `A0` and `VRy` to `A1`:

```c
int x = analogRead(A0);
int y = analogRead(A1);
```

A simple program that prints the values is this:

```c
void setup() {
  Serial.begin(9600);
}

void loop() {
  int x = analogRead(A0);
  int y = analogRead(A1);
  Serial.print("X = ");
  Serial.print(x);
  Serial.print("\tY = ");
  Serial.println(y);
  delay(100);
}
```

You can also think of them as voltage values. Assuming a `5V` positive voltage, you can multiply the value you get by `5.0`, and then divide it by `1023` to get a `0` to `5` range:

```c
x = analogRead(A0);
y = analogRead(A1);

float x_val = (x * 5.0) / 1023;
```

![Diagram showing joystick voltage values from 0V to 5V with center position at 2.5V for both axes](https://flaviocopes.com/images/electronics-components-analog-joystick/Immagine_PNG-F95A8659C5B1-1.png)

You can perform a similar calculation to get the values relative to a `3.3V` `Vcc`.
