Joystick Module With Arduino

by STEAM-DIY in Circuits > Arduino

88 Views, 0 Favorites, 0 Comments

Joystick Module With Arduino

Screenshot 2024-10-17 145347.png

Welcome back to my blog. We have already described the types of data required for Arduino encoding in the previous post. We create programs using that knowledge. Okay, today we are going to do a small project and learn Arduino. For that, I have used “digital read,” “analog read” and “analog write”. The following are articles describing these.


Supplies

  1. Analog write — Click me
  2. Analog read —Click me
  3. Digital read — Click me

Okay, let’s go to the post today. We have mainly used the joystick module for this project. We have seen this component in game consoles and in remote controls. They cannot be customized. But with the module we are talking about today, we can do whatever we want through the Arduino board. Let’s do this project using simple code. The following is this module.


Using this component we can create remote controls and game consoles. We will talk about these projects in the next articles. This joystick module has five pins. These are GND, + 5v, VRx, VRy, and SW. It uses GND and + 5v pins to power this component. Analog values ​​can be obtained via VRx and VRy pins by moving this module to both the X and Y axes. A digital value can be obtained from the SW pin. To do this, the module must be pushed down. Let’s do this project practically, using a servo motor and an LED bulb. The required components are as follows.

  1. Arduino Uno board.
  2. Joystick module.
  3. Servo motor.
  4. LED bulb.
  5. 220ohm resistor
  6. 10k resistor.
  7. Breadboard.
  8. Jumper wires.

Screenshot 2024-10-17 145938.png

Connect these components to the breadboard.

Joystick module with Arduino.

Connect these components to your Arduino Uno board using the circuit diagram below.

Joystick module with Arduino.



Use only the PMW pins to connect the servo motor. The 220ohm resistor is used to control the current flowing through the LED bulb. The 10k resistor is used for the pull-up circuit.


Let’s Look at the Code Below

void setup() {
pinMode(2, INPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}
void loop() {
int x = analogRead(A0);
bool b = digitalRead(2);
x = map(x, 0, 1024, 0, 180);
analogWrite(3, x);

if (b == 0) {
digitalWrite(4, HIGH);
} else {
digitalWrite(4, LOW);
}
}

Explanation of the code

This code sets the second pin as an input pin, the third pin as an output pin and the fourth pin as an output pin.

void setup() {
pinMode(2, INPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}

Then this code reads analog and digital values and puts them into the variables. The integer data type is named ‘x’ and the boolean data type is named ‘b’. This process is called variable declaring.

int x = analogRead(A0);
bool b = digitalRead(2);

This code converts the analog value from 0 -1024 to 0 -180. The servo motor then rotates.

x = map(x, 0, 1024, 0, 180);
analogWrite(3, x);

The digital value is checked using an IF condition. Then it is checked whether it is equal to 0. The LED bulb turns on when the digital value is 0 and the LED bulb turns off when the digital value is 1.

if (b == 0) {
digitalWrite(4, HIGH);
} else {
digitalWrite(4, LOW);
}


Now Select Board and Port. After, Click the Upload Button.

Joystick module with Arduino.

OK, now you can test this project. The following is a project using the Arduino Nano. It uses a serial monitor to view the joystick module readings. The circuit diagram and code are given below.