Setting Up a Simple Joystick on Arduino for Beginner!
by BobWithTech in Circuits > Arduino
464 Views, 2 Favorites, 0 Comments
Setting Up a Simple Joystick on Arduino for Beginner!
This Tutorial Will Cover On HOW We Could Implement A Joystick Control System Into an Arduino From The Joystick Input.
Supplies
Gather The Required Material:
From Amazon:
- Joytick Module
- Best Affordable Arduino (Elegoo Brand Distributions) Available High Quality
- Jumper Wire
From Ebay Discount 1%-4% OFF:
Hardware Connection Diagram
Connection Instructions:
- Connect the gnd of the joystick module to gnd pin of Arduino.
- Connect the Vin or V+ of the joystick module to 5V pin of Arduino.
- Connect the VRx of the joystick module to A0 pin of Arduino.
- Connect the VRy of the joystick module to A1 pin of Arduino.
- Connect the SW or Press of the joystick module to pin 2 of Arduino.
Program Code With Explanations
Program Code:
- Overall Code:
int VRx = A0;
int VRy = A1;
int SW = 2;
int xPosition = 0;
int yPosition = 0;
int SW_state = 0;
int mapX = 0;
int mapY = 0;
void setup() {
Serial.begin(9600);
pinMode(VRx, INPUT);
pinMode(VRy, INPUT);
pinMode(SW, INPUT_PULLUP);
}
void loop() {
xPosition = analogRead(VRx);
yPosition = analogRead(VRy);
SW_state = digitalRead(SW);
mapX = map(xPosition, 0, 1023, -100, 100);
mapY = map(yPosition, 0, 1023, -100 100);
Serial.print("X: " + String(mapX));
Serial.print("\tY: " + String(mapY));
Serial.println("\tButton: " + String(SW_state));
delay(100);
}
- Explanation Code:
1. Initialize Variable For Arduino Pin:
int VRx = A0; #Analog Input Variable For VRx
int VRy = A1; #Analog Input Variable For VRy
int SW = 2; #Digital Input Variable For Press Button
2. Initialize Variable For Storing Arduino Internal Data:
int xPosition = 0; #Initialising Stored Variable For X-Movement
int yPosition = 0; #Initialising Stored Variable For Y-Movement
int SW_state = 0; #Initialising Stored Variable For Press Reaction
int mapX = 0; #Initialising Stored Variable Of Output Value Of X After Mapping
int mapY = 0; #Initialising Stored Variable Of Ouptput Vale Of Y After Mapping
3. Startup Setup For Arduino With Pin Modes Initialized:
void setup() {
Serial.begin(9600); #Begin Arduino Serial Data
pinMode(VRx, INPUT); #Set VRx Pin as an Input
pinMode(VRy, INPUT); #Set VRy Pin as an Input
pinMode(SW, INPUT_PULLUP); #Set SW Pin as Input
}
4. Loop Code That Would Run In Arduino, With The Given Serial Print Of Output From The Mapping:
void loop() {
xPosition = analogRead(VRx); #Stored The Analog Input Data Into The Variable
yPosition = analogRead(VRy); #----
SW_state = digitalRead(SW); #Stored The Digital Input Data Into The Variable
mapX = map(xPosition, 0, 1023, -100, 100); #Start Mapping The X-Value From a range of 0-1023 into range
mapY = map(yPosition, 0, 1023, -100 100); #of -100-100 (-100 as backward and 100 as frontward)
Serial.print("X: " + String(mapX)); #Print The Stored Variable In Arduino Serial Data
Serial.print("\tY: " + String(mapY)); #-----
Serial.println("\tButton: " + String(SW_state)); #------
delay(100); #Slow Down The Serial Print Speed By Adding Delay of 100 ms
}