Arduino Tutorial - Joystick

About Joystick Sensor

You probably see the Joystick somewhere such as a game controller, toy controller, or even a big real machine such as an excavator controller.

The joystick is composed of two potentiometers square with each other, and one push button. Therefore, it provides the following outputs:

  • An analog value (from 0 to 1023) corresponding to the horizontal position (called X-coordinate)
  • An analog value (from 0 to 1023) corresponding to the vertical position (called Y-coordinate)
  • A digital value of a pushbutton (HIGH or LOW)

The combination of two analog values can create 2-D coordinates with the center are values when the joystick is in the rest position. The real direction of the coordinates can be identified simply when you run a test code (in the next part).

Some applications may use all three outputs, some applications may use some of three outputs.

Pinout

A Joystick has 5 pins:

  • GND pin: needs to be connected to GND (0V)
  • VCC pin: needs to be connected to VCC (5V)
  • VRX pin: outputs an analog value corresponding to the horizontal position (called X-coordinate).
  • VRY pin: outputs an analog value corresponding to the vertical position (called Y-coordinate).
  • SW pin: is the output from the pushbutton inside the joystick. It’s normally open. If we use a pull-up resistor in this pin, the SW pin will be HIGH when it is not pressed, and LOW when it is pressed.
Joystick Pinout

How It Works

  • When you push the joystick's thump to left/right, the voltage in the VRX pin is changed, The voltage range is from 0 to 5V (0 at left and 5v at right). The voltage value is in proportion to the position of the thump ⇒ The reading value on Arduino's analog pin is from 0 to 1023
  • When you push the joystick's thump to up/down, the voltage in the VRY pin is changed, The voltage range is from 0 to 5V (0 at up and 5v at down). The voltage value is in proportion to the position of the thump ⇒ The reading value on Arduino's analog pin is from 0 to 1023
  • When you push the joystick's thump to any direction, the voltage in both VRX and VRY pins is changed in proportion to the projection of position on each axis
  • When you push the joystick's thump from top to bottom, the pushbutton inside the joystick is closed, If we use a pull-up resistor in the SW pin, the output from SW pin will change from 5V to 0V ⇒ The reading value on Arduino's digital pin is changed from HIGH to LOW

Tag » Arduino Joystick X Y Code