COMPONENT :
Arduino UNO Board
Joystick Module
LEDs
Resistors
Jumper wires
Breadboard
THEORY :
The Joystick module provides Analog Outputs and the output voltages provided by this module keep changing according to the direction in which we move it. And we can get the direction of movement by interpreting these voltage changes using some micro-controller (we are using Arduino UNO).
CONNECTION :
The module has 5 pins: Vcc, Ground, X, Y and Key .
![](https://static.wixstatic.com/media/a27d24_e7efa873e48147cb842f117d3cb3e7cb~mv2.png/v1/fill/w_980,h_481,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/a27d24_e7efa873e48147cb842f117d3cb3e7cb~mv2.png)
CODE :
int button=2; int buttonState0 = 0; int buttonState1 = 0;
void setup()
{ pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT); pinMode(button,INPUT); digitalWrite(button, HIGH); Serial.begin(9600); }
void loop()
{ int x = analogRead(A0); int y = analogRead(A1);
Serial.print(x); Serial.println(y);
buttonState0 = digitalRead(button); Serial.println(buttonState0); if (x>=0 && y<=10) { digitalWrite(10, HIGH); } else
{
digitalWrite(10, LOW);
}
if (x<=10 && y>=500) { digitalWrite(11, HIGH); } else
{
digitalWrite(11, LOW);
}
if (x>=1020 && y>=500) { digitalWrite(9, HIGH); } else
{
digitalWrite(9, LOW);
}
if (x>=500 && y>=1020) { digitalWrite(8, HIGH); } else
{
digitalWrite(8, LOW);
}
if (x>=1020 && y>=1020) { digitalWrite(9, LOW); digitalWrite(8, LOW); }
if (buttonState0 == LOW) { Serial.println("Switch = High"); digitalWrite(7, HIGH); } else
{
digitalWrite(7, LOW);
} buttonState1 = digitalRead(button); Serial.println(buttonState1); delay(50); }
//Feel free to ask your doubts.