Arduino Joystick Module Tutorial
by Utsource in Circuits > Arduino
955 Views, 0 Favorites, 0 Comments
Arduino Joystick Module Tutorial
Hi guys in this instructables we will learn how to use joystick module with Arduino and how we can read the data from joystick module using arduino uno on the serial monitor.
Things You Need
For this instructables we will need following things :
Arduino uno :
Joystick Module :
Jumper wires :
Breadboard (optional) :
Schmatics
Connect everything According to the shown schmatics in the image and everything will be good.
Code
Please copy the following code and upload it to the arduino Board :
int xPin = A1;
int yPin = A0;
int buttonPin = 2;
int xPosition = 0;
int yPosition = 0;
int buttonState = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
//activate pull-up resistor on the push-button pin
pinMode(buttonPin, INPUT_PULLUP);
// For versions prior to Arduino 1.0.1
// pinMode(buttonPin, INPUT);
// digitalWrite(buttonPin, HIGH);
}
void loop() {
xPosition = analogRead(xPin);
yPosition = analogRead(yPin);
buttonState = digitalRead(buttonPin);
Serial.print("X: ");
Serial.print(xPosition);
Serial.print(" | Y: ");
Serial.print(yPosition);
Serial.print(" | Button: ");
Serial.println(buttonState);
delay(100); // add some delay between reads
}
int xPin = A1;
int yPin = A0;
int buttonPin = 2;
int xPosition = 0;
int yPosition = 0;
int buttonState = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
//activate pull-up resistor on the push-button pin
pinMode(buttonPin, INPUT_PULLUP);
// For versions prior to Arduino 1.0.1
// pinMode(buttonPin, INPUT);
// digitalWrite(buttonPin, HIGH);
}
void loop() {
xPosition = analogRead(xPin);
yPosition = analogRead(yPin);
buttonState = digitalRead(buttonPin);
Serial.print("X: ");
Serial.print(xPosition);
Serial.print(" | Y: ");
Serial.print(yPosition);
Serial.print(" | Button: ");
Serial.println(buttonState);
delay(100); // add some delay between reads
}
Final Step
After connecting everything together and uploading the code to arduino, you can connect the cable to pc and open the serial monitor and on serial monitor whenever you will move the joystick you can see the changed joystick's Potentiometer X & potentiometer Y values on your serial monitor.