Arduino Code and Video: Dual-Axis Joystick

Arduino Code and Video: Dual-Axis Joystick

In this tutorial, we will explore how to utilize a dual-axis XY joystick with Arduino. This joystick can be employed as a game controller or for controlling robotic arms, among other applications. The primary focus will be on detecting the joystick's position and the state of its push-button switch, which can be reflected on a serial monitor.

XY-Dual access joystick

Throughout the tutorial, we will implement a simple program that reads the X and Y positions of the joystick and displays them in the Arduino Serial Monitor. When the joystick is pushed, the switch state will also be detected. For a detailed explanation of the code, make sure to watch the video at the specified timestamps (in video at 00:00).

Hardware Explained

The main components for this project include the dual-axis joystick module and the Arduino board. The joystick module consists of two variable resistors, one for the X-axis and one for the Y-axis, which provide analog values based on the joystick's position. The push-button switch on the joystick is used to detect when it is pressed.

When you move the joystick, the potentiometers change their resistance, which is translated into values between 0 and 1023. This allows us to determine the joystick's position accurately. The push-button switch is connected to a digital input pin on the Arduino, enabling us to read its state without needing an external resistor by using the internal pull-up resistor feature.

Datasheet Details

ManufacturerGeneric
Part numberDual-axis Joystick Module
Logic/IO voltage5 V
Supply voltage5 V
Output voltage range0 - 5 V
Resolution10-bit (0-1023) analog
Switch typePush-button
PackageStandard module

  • Ensure proper power supply (5 V) to the joystick module.
  • Connect the joystick's X and Y outputs to analog pins A0 and A1, respectively.
  • Use digital pin 6 for the joystick switch.
  • Utilize a pull-up resistor configuration for the switch pin.
  • Monitor the output values to ensure they range from 0 to 1023.

Wiring Instructions

Arduino wiring for XY-Dual access joystick
Arduino wiring for XY-Dual access joystick
Arduino X-Y-Joystick wiring
Arduino X-Y-Joystick wiring

To wire the dual-axis joystick to the Arduino, start by connecting the joystick's ground pin to the ground rail on your breadboard. Next, connect the joystick's 5V pin to the positive rail on the breadboard. The two variable pins on the joystick, labeled as X and Y, should be connected to the analog pins A0 and A1 on the Arduino, respectively.

For the push-button switch on the joystick, connect it to digital pin 6 on the Arduino. This connection utilizes the internal pull-up resistor feature, so you don't need an external resistor. Finally, if you want to monitor the switch state with an LED, connect the longer leg of the LED to digital pin 13 and the shorter leg through a 680-ohm resistor to ground.

Code Examples & Walkthrough

In the Arduino code, we define the pin for the switch and specify the screen dimensions for mapping joystick values. The setup function initializes serial communication and sets the switch pin mode.

void setup() {
  Serial.begin(9600); // initialize serial communication
  pinMode(sw, INPUT_PULLUP); // setting pin sw as input
  pinMode(13, OUTPUT); // LED pin
}

In the loop function, we read the analog values for X and Y positions and map them to the defined screen dimensions. The state of the switch is also read and printed to the Serial Monitor.

void loop() {
  int x = analogRead(A0); // read analog value for X
  int y = analogRead(A1); // read analog value for Y
  int sStat = digitalRead(sw); // read switch state
  // Print values to Serial Monitor
  Serial.print("X: ");
  Serial.print(xPos);
  Serial.print(" Y: ");
  Serial.println(yPos);
}

Finally, the code checks if the switch is pressed, turning the LED on or off based on the switch state. This provides visual feedback when the joystick button is activated.

if(sStat == LOW) {
    Serial.println("Switch pressed");
    digitalWrite(13, HIGH); // Turn LED ON
  } else {
    digitalWrite(13, LOW); // Turn LED OFF
  }

To see how the joystick values change in real time, upload the code to your Arduino and open the Serial Monitor. The values will update every half second, displaying the current X and Y positions along with the switch state (in video at 01:30).

Demonstration / What to Expect

When you move the joystick, the X and Y values should change accordingly, ranging from 0 to 1023. If you press the joystick button, the LED connected to pin 13 will light up, indicating that the switch is active. If the joystick is centered, you should see values around 512 for both axes, depending on the joystick's calibration.

Video Timestamps

  • 00:00 - Introduction to the dual-axis joystick
  • 01:30 - Code explanation and functionality
  • 02:45 - Wiring instructions
  • 03:15 - Expected output and testing

Images

Arduino wiring for XY-Dual access joystick
Arduino wiring for XY-Dual access joystick
XY-Dual access joystick
XY-Dual access joystick
XY-Dual access joystick
XY-Dual access joystick
XY-Dual access joystick
XY-Dual access joystick
XY-Dual access joystick
XY-Dual access joystick
XY-Dual access joystick
XY-Dual access joystick
Arduino X-Y-Joystick wiring
Arduino X-Y-Joystick wiring
58-This is the Arduino code for a dual-axis joystick.
Language: C++
Copied!

Things you might need

Resources & references

No resources yet.

Files📁

No files available.