Arduino Joystick Module Example
Today we will fire the LEDs in a fun way, using the arduino joystick.
Joystick Connection
The joystick module has 5 pins for connection.
From these pins, VRx signals on the horizontal axis, VRy signals on the vertical axis, and SW pin allows reading the pin value of the joystick click button. The joystick module generates analog values between 0 and 1023 in the horizontal and vertical axes.
Joystick module to connect arduino;
- Gnd pin ->Aarduino Gnd pin,
- Vcc pin -> Arduino 5V pin ,
- The VRx pin arduino is one of the A0-A5 analog pins,
- The VRy pin arduino is one of the A0-A5 analog pins,
- The SW pin must be connected to one of the arduino digital pins.
For example, we will connect pin VRx pin A0, pin VRy pin A1, and pin SW pin pin 2 to the arduino pin.
Arduino Joystick Code
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
int xPin = A0; // It can be connected to any of the A0-A5 analog pins. int yPin = A1; // It can be connected to any of the A0-A5 analog pins. int btnpin = 2; // Joystick button arduino connection (Joystick SW output) int x_position = 0; int y_position = 0; int buton_state = 0; void setup() { Serial.begin(9600); pinMode(xPin, INPUT); pinMode(yPin, INPUT); pinMode(btnpin, INPUT_PULLUP); } void loop() { x_position = analogRead(xPin); y_position = analogRead(yPin); buton_state= digitalRead(btnpin); Serial.print("X Position: "); Serial.print(x_position); Serial.print(" | Y Position: "); Serial.print(y_position); Serial.print(" | Button State: "); Serial.println(buton_state); delay(150); } |
- We read in this code we printed the Serial screen
- Joystick position is fixed, the screen output is x position :500.. y position:500..
- Joystick position is in the right side, the screen output x position:500.. y position:0
- When the joystick position is in the left side, the screen output x position:500.. y position:1023
- Joystick position is in the bottom side, the screen output
You install the above codes on the arduino board where you make the joystick connections, you can follow values ranging from 0 to 1023 on the serial monitor screen (Ctrl + Shift + M).
Joystick Led Control
Joystick Led Code
Here we will do the operation, according to the joystick coordinates to burn the leds.For example when the joystick is on the right, the right led will light up.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
int xPin = A0; // It can be connected to any of the A0-A5 analog pins. int yPin = A1; // It can be connected to any of the A0-A5 analog pins. int btnpin = 2; // Joystick button arduino connection (Joystick SW output) int led1=7; int led2=8; int led3=9; int led4=10; int x_position = 0; int y_position = 0; int buton_state = 0; void setup() { Serial.begin(9600); pinMode(xPin, INPUT); pinMode(yPin, INPUT); pinMode(led3,OUTPUT); pinMode(led1,OUTPUT); pinMode(led4,OUTPUT); pinMode(led2,OUTPUT); pinMode(btnpin, INPUT_PULLUP); } void loop() { x_position = analogRead(xPin); y_position = analogRead(yPin); buton_state= digitalRead(btnpin); Serial.print("X Position: "); Serial.print(x_position); Serial.print(" | Y Position: "); Serial.print(y_position); Serial.print(" | Button State: "); Serial.println(buton_state); if(y_position==0) { digitalWrite(led3,HIGH); }else{ digitalWrite(led3,LOW); } if(y_position==1023) { digitalWrite(led4,HIGH); }else{ digitalWrite(led4,LOW); } if(x_position==1023) { digitalWrite(led1,HIGH); }else{ digitalWrite(led1,LOW); } if(x_position==0) { digitalWrite(led2,HIGH); }else{ digitalWrite(led2,LOW); } delay(100); } |
Run Code
if you have question do not forget to write from the chat button next to it or from the comment
Recent Comments