How to use Joystick Module with Arduino Board

Share on Social Media

The JoyStick is an analog sensor that can be used to control your Arduino. Analog Joy Sticks are basically potentiometers, so they return values which can be made visible using the provided code in this step.

By clicking the serial monitor you will see the values. Once you move the JoyStick the values will change. This code is also handy if you want to control your Arduino using a JoyStick.

DataSheet:- Joystick_module_Datsheet

Connection:-

Code:- Analog joysticks are basically potentiometers so they return analog values. When the joystick is in the resting position or middle, it should return a value of about 512. The range of values goes from 0 to 1024.

// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output

void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(115200);
}

void loop() {
Serial.print(“Switch: “);
Serial.print(digitalRead(SW_pin));
Serial.print(“\n”);
Serial.print(“X-axis: “);
Serial.print(analogRead(X_pin));
Serial.print(“\n”);
Serial.print(“Y-axis: “);
Serial.println(analogRead(Y_pin));
Serial.print(“\n\n”);
delay(500);
}


Share on Social Media

Leave a Reply

Your email address will not be published. Required fields are marked *