What is a joystick? How can you use it in your projects?

A joystick is a position sensor consisting of a rod that pivots on a base and transmits the angle or direction to the Arduino board in the form of abscissa and ordinate coordinates.

Joysticks are widely used in our environment, notably in game controllers, to control remote-controlled cars, drones or cameras.

To control them, you’ll have two values: abscissa (up and down) and ordinate (left and right).

The joystick is fitted with two 10K ohm potentiometers, one for each axis. When the joystick is moved, the potentiometer values change accordingly, providing us with a position coordinate. These potentiometers are essential for translating joystick movements into digital data that can be interpreted by the Arduino board.

The joystick is fitted with two potentiometers with equivalent resistors, meaning that the 5V voltage is divided into two equal portions, i.e. 2.5V for each potentiometer. By moving the joystick, the voltage varies from 0V to 5V, thus covering the entire range of possible values for each axis.

When we read the joystick value with the Arduino board, we obtain a value of 512 when the joystick is at rest, and a value that varies from 0 to 1023 when the joystick is moved, depending on its position.

The joystick also has one for your projects. It can be used to take control of your computer mouse, or if you need to click on what you’re pointing at.

he joystick is also equipped with a push-button, which can be used to take control of your computer’s mouse, or to click on desired elements.

The joystick has several pins:

  • GND: ground
  • +5V: supply voltage
  • vRx: analog pin indicating x-axis position
  • vRy: analog pin indicating ordinate position
  • Sw: indicates push-button position

Display joystick coordinates

Now let’s see how to display the joystick’s coordinates on the Arduino IDE’s serial monitor. To do this, we’ll connect the joystick to the analog pin on the Arduino board to retrieve the abscissa and ordinate values:

Here’s the code to display the joystick’s X and Y coordinates:

//Arduino board pin
const int SW_pin = 2; // Digital pin to indicate button position
const int X_pin = 0; // Analog pin for X axis
const int Y_pin = 1; // Analog pin for Y axis

void setup() {
  pinMode(SW_pin, INPUT); // Configure SW as an input
  digitalWrite(SW_pin, HIGH); // Apply voltage to SW to tell joystock it's an input
  Serial.begin(9600); // Set up serial monitor
}

void loop() {
  Serial.print(“X coordinate”);
  Serial.print(analogRead(X_pin)); // X value
  Serial.print(“\n”);
  Serial.print(“Y coordinate”);
  Serial.println(analogRead(Y_pin)); // Y value
  Serial.print(“\n”);
  delay(500); // // A short pause to avoid overloading the serial monitor
}

Control a servomotor

Now we’ll learn how to control a servomotor using a joystick. The servomotor will move from 0° to 180 degrees according to the abscissa coordinates of the joystick. This circuit offers many possibilities for various projects, such as steering a remote-controlled car, opening a water valve, or even controlling the rudder of a boat.

#include <Servo.h> // Using the Servo.h library.
//Pin on Arduino board
#define pinServo 9 // Pin for X servomotor signal.
const int SW_pin = 2; // Digital pin to indicate push-button position.
const int X_pin = 0; // Analog pin for X coordinate
const int Y_pin = 1; // Analog pin for Y coordinate
unsigned short X,Y; // Variables for joystick values.
Servo Sx; // Servo declaration.

void setup() {
  pinMode(SW_pin, INPUT); // Configure SW as an input.
  digitalWrite(SW_pin, HIGH); // Apply voltage to SW to tell joystock it's an input
  pinMode(pinServo, OUTPUT); // Output servo motor pin.
  Sx.attach(pinServo); // Match servo motor pin to pin
  Sx.write(90); // Set servo motor to initial position.
  Serial.begin(9600); // Configure serial monitor.

}

void loop() {
  X = analogRead(X_pin); // Read joystick values.
  Sx.write(rotation_servo(X)); // Send rotation signals.
  delay(500);
}


byte rotation_servo(unsigned short n) // Matching function.
{
  return (byte)(-0.175953 * n + 180);  
}

Control the servomotor with the y-axis of the joystick

In some projects, you may need to control the servomotor using the joystick’s y-axis (up and down movement) rather than the x-axis (left and right movement).

To do this, you need to change line 21 of the code X = analogRead(X_pin); to X = analogRead(Y_pin);