INTERFACE 2 AXIS JOYSTICK ANALOG MODULE INTO ARDUINO
In this tutorial today we will learn about joystick. What is joystick? How joystick work? and how to interface joystick with Arduino? We all know joystick that the game is played with joystick. We all have played a lot of games with joystick in our childhood. Whether it is your car racing or your wrestler game, but joysticks is used for many other uses apart from playing games. Such as to control the robot, to control the gimbal, and many such machines whose joystick is used to control the direction or angle. But In this tutorial, we will control the DC motor with joystick.
What is joystick?
Joystick is a two axis base analog input module. Which is used to control the direction or angle of any output device. For example, to control the servo motor, to control the robot and to control RC car, cranes, machines and so on. It has two 10K potentiometers and these two potentiometers are arranged for the X axis and for the Y axis. And it has a cap that is used to move the two 10K potentiometers in the X axis and the Y axis. and there is a switch button. Which is used to activate the joystick by cap or knob.
How to Joystick Works?
The joystick has two 10k potentiometers or pots. In such a way these potentiometers are arranged with the help of a gimbal mechanism. These two pots can be easily moved separately on the x-axis and y-axis. And there is a push button. Which is used for different purposes. When the 1st pot is moved on the x-axis (Left to Right) with the help of a stick, the voltage changes at the VRX pin of the joystick, according to the supply voltage. Similarly, if the 2nd pot is moved on the y-axis (Up to Down) with the help of a stick, then the voltage changes at the VRY pin of the joystick. Read the output voltage of the joystick by connecting the VRX pin and VRY pin of the joystick to any two analog pins of the Arduino.
Pinout of Joystick
5V - To give DC power supply. GND - To give common ground. VRx - To read the voltage from the X-axis. VRy - To read the voltage from the Y-axis. SW - To read the Switch Status.
Component Required
- Arduino Uno
- Joystick
- L298N Motor Driver Module
- 12V DC Gear Motor
- 12V Battery
- Jumper Wires
Schematic Diagram
Circuit Diagram
Code
int x_axis=A0;
int y_axis=A1;
int in1 =2;
int in2=3;
int var1;
int var2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(x_axis,INPUT);
pinMode(y_axis,INPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
var1=analogRead(x_axis);
var2=analogRead(y_axis);
Serial.print("X-Axis=");
Serial.println(var1);
Serial.print("Y-Axis=");
Serial.println(var2);
delay(500);
if(var1>511&var1>var2)
{
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
}
else if (var2>511&var2>var1)
{
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
}
else
{
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
}
}
0 Comments