Interfacing Joystick Module with Arduino

537 views

Introduction

Are you interested in creating your own interactive projects and games? One of the most popular input devices for such projects is the joystick module. The joystick module is a device that allows users to control the movement of an object or a game character in multiple directions.

In this article, we will explore how to interface the “Joystick module with the Arduino” board, a popular microcontroller platform used in many DIY electronics projects.

What is Joystick Module?

A joystick module is an input device that allows users to control the movement of an object or a game character in multiple directions. It typically consists of two potentiometers and a button, which are used to measure the position of the joystick in two dimensions (X and Y axis) and the state of the button. The potentiometers convert analog signals from the joystick into digital signals that can be read by a microcontroller or a computer.

Hardware Components

To interface Joystick Module with Arduino, you’ll need the following hardware components to get started:

ComponentsValueQty
Arduino UNO1
USB Cable Type A to B1
DC Power for Arduino1
Joystick Module1
Jumper Wires1

Joystick Module Pinout

Joystick Module Pinout
Sl. NoPin labelPin Description
1GNDGround Connection
25VSupply a pin for the joystick. The 5 V provides biasing voltage for the potentiometers
3VRxAnalog voltage output for the X-axis potentiometer
4VRyAnalog voltage output for the Y-axis potentiometer
5SWSwitch output. The pin goes low when the switch is pressed

Joystick Module Arduino Circuit

Make connections according to the circuit diagram given below.

Wiring / Connections

ArduinoJoystick Interface
5VVCC
GNDGND
A0VER
A1HOR
D6SEL

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Installing Libraries

Before you start uploading a code, download and unzip the following libraries at /Program Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

#include "Mouse.h"
 
// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 2;      // switch to turn on and off mouse control
const int mouseButton = 3;    // input pin for the mouse pushButton
const int xAxis = A0;         // joystick X axis
const int yAxis = A1;         // joystick Y axis
const int ledPin = 5;         // Mouse control LED
 
// parameters for reading the joystick:
int range = 12;               // output range of X or Y movement
int responseDelay = 5;        // response delay of the mouse, in ms
int threshold = range / 4;    // resting threshold
int center = range / 2;       // resting position value
 
bool mouseIsActive = false;    // whether or not to control the mouse
int lastSwitchState = LOW;        // previous switch state
 
void setup() {
  pinMode(switchPin, INPUT);       // the switch pin
  pinMode(ledPin, OUTPUT);         // the LED pin
  // take control of the mouse:
  Mouse.begin();
}
 
void loop() {
  // read the switch:
  int switchState = digitalRead(switchPin);
  // if it's changed and it's high, toggle the mouse state:
  if (switchState != lastSwitchState) {
    if (switchState == HIGH) {
      mouseIsActive = !mouseIsActive;
      // turn on LED to indicate mouse state:
      digitalWrite(ledPin, mouseIsActive);
    }
  }
  // save switch state for next comparison:
  lastSwitchState = switchState;
 
  // read and scale the two axes:
  int xReading = readAxis(A0);
  int yReading = readAxis(A1);
 
  // if the mouse control state is active, move the mouse:
  if (mouseIsActive) {
    Mouse.move(xReading, yReading, 0);
  }
 
  // read the mouse button and click or not click:
  // if the mouse button is pressed:
  if (digitalRead(mouseButton) == HIGH) {
    // if the mouse is not pressed, press it:
    if (!Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.press(MOUSE_LEFT);
    }
  }
  // else the mouse button is not pressed:
  else {
    // if the mouse is pressed, release it:
    if (Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.release(MOUSE_LEFT);
    }
  }
 
  delay(responseDelay);
}
 
/*
  reads an axis (0 or 1 for x or y) and scales the analog input range to a range
  from 0 to <range>
*/
 
int readAxis(int thisAxis) {
  // read the analog input:
  int reading = analogRead(thisAxis);
 
  // map the reading from the analog input range to the output range:
  reading = map(reading, 0, 1023, 0, range);
 
  // if the output reading is outside from the rest position threshold, use it:
  int distance = reading - center;
 
  if (abs(distance) < threshold) {
    distance = 0;
  }
 
  // return the distance for this axis:
  return distance;
}

Code Explanation

This Arduino code demonstrates how to use a joystick module to control the mouse pointer on a computer. The code first sets up the pin numbers for the joystick axes, the switch, the LED, and the mouse button. It also defines some parameters for reading the joystick, such as the output range of X or Y movement, the response delay of the mouse, the resting threshold, and the resting position value.

In the setup function, the code initializes the switch and LED pins and takes control of the mouse using the Mouse.begin() function. In the loop function, the code reads the switch state and toggles the mouse control state if the switch is pressed. If the mouse control state is active, the code moves the mouse based on the readings from the joystick module. Additionally, the code checks the state of the mouse button and clicks or releases the left mouse button accordingly.

The readAxis function is a helper function that reads an analog input from a specified axis (X or Y) of the joystick module, scales the input range to the output range, and applies a resting threshold. The function returns the distance for the specified axis.

Overall, this code demonstrates how to interface a joystick module with Arduino and use it to control the mouse pointer on a computer.

Applications

Here are some applications of the Joystick Module:

  • Gaming controllers
  • Robotics and automation
  • Remote control systems
  • Virtual reality and simulators
  • Industrial control systems
  • Medical devices and rehabilitation equipment
  • Surveillance and security systems
  • Aerial and ground vehicles
  • Camera pan/tilt control
  • Human-machine interfaces for disabled persons.

Conclusion

Controlling a joystick module with Arduino is a fun and exciting project that can lead to the creation of many interactive projects and games. By following the simple steps outlined in this article, you can easily build your own joystick-controlled project and take your DIY electronics skills to the next level. So go ahead and give it a try, and let your creativity soar!