2-Axis Joystick Arduino Programming and Circuit Diagram
Description:
2-Axis Joystick Arduino Programming and Circuit Diagram-In this post, you’ll learn how to use an Arduino Uno or Mega 2 Axis analog joystick and control some LEDs as per the joystick’s motion. I used 4 LEDs to reflect the Forward, Reverse, Left, and Right movement in this beginner’s level venture.
When you search this Joystick module, you will find that this device also has a push-button that can be used for different purposes as per your requirement, or you can leave this push button unconnected. I will use this pushbutton to trigger and deactivate the 2-axis analog joystick in this particular beginner’s level tutorial.
After pressing the push button, an LED will turn on indicating that the joystick is triggered and can be used to control the LEDs. When you click the joystick push-button again, the joystick will be disabled and you will no longer be able to control the LEDs.
In this post, I will cover,
- Circuit diagram explanation
- Joystick interfacing with Arduino
- Arduino Programming and
- Testing
The Amazon Purchase links are given below:
Joystick: https://amzn.to/368dlxa
Arduino Uno: https://amzn.to/39aq6ZT
Mega 2560: https://amzn.to/2SszMsI
lm7805 Voltage Regulator: https://amzn.to/2ERYoTJ
330-ohm resistors pack: https://amzn.to/2Qj1Igg
female DC power jack socket: https://amzn.to/377Pg9M
470uf capacitors: https://amzn.to/2MrN3xR
5×7 cm Vero board: https://amzn.to/37b7KWO
female headers: https://amzn.to/350w6RE
connection wires: https://amzn.to/2MvOJXd
Super Starter kit for Beginners: https://amzn.to/398dliF
Jumper Wires: https://amzn.to/2SrnBwo
Bread Board: https://amzn.to/2MxV5FM
12v Adaptor: https://amzn.to/2MuOlZk
PCB plate: https://amzn.to/2MuwNMB
Variable Supply: https://amzn.to/39d0KdP
Digital Multimeter: https://amzn.to/34WbVoa
Vero Board / stripboard: https://amzn.to/39nL9Zg
Soldering iron kit: “best” You guys should definitely purchase this: https://amzn.to/2PVwexF
Solder wire: https://amzn.to/2QlOvTS
Wire Stripper: https://amzn.to/353tYJa
wirecutter: https://amzn.to/2tV2lFj
PCB small portable drill machine: https://amzn.to/2MvQqnx
DISCLAIMER:
Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Essentially, the 2-axis joystick is a combination of two potentiometers that are used respectively for the X and Y axis. When the joystick is pushed in any direction, the potentiometer value is changed as a result of which we obtain different voltage values, based on these voltage values, then we determine whether the joystick is moving in the direction of forward, reverse, left or right.
The 2-axis Analog joystick module has a total of 5 pins which are labeled with
GND
+5V
VRx
VRy and
SW.
Circuit Diagram:
This is a very simple circuit diagram, as you can see that it only consists of LEDs connected to the resistors in series. The VRx and VRy joystick pins are wired to the analog A1 and A2 pins of the Arduino. The joystick’s SW pin is attached to the Arduino’s virtual pin 4. While the 2-axis joystick’s + 5v and GND pins are wired to the 5v and GND pins of the Arduino.
2-Axis Joystick Interfacing with Arduino:
As per the circuit diagram, the joystick and all the led’s are connected to the Arduino. Now let’s discuss the Arduino programming.
2-Axis Joystick Arduino Programming:
1 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
int button = 4; int vrx = A1; int vry = A2; int xdata = 0; int ydata = 0; int led1 = 8; //FORWARD int led2 = 9; // RIGHT int led3 = 10; //LEFT int led4 = 11; //Reverse int led5 = 13; // indicator int flag = 0; int buttonf = 0; void setup() { Serial.begin(9600); pinMode(vrx, INPUT); pinMode(vry, INPUT); pinMode(button, INPUT); digitalWrite(button , HIGH); pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); pinMode(led4, OUTPUT); pinMode(led5, OUTPUT); //keep all the led's off by default digitalWrite(led1, LOW); digitalWrite(led2, LOW); digitalWrite(led3, LOW); digitalWrite(led4, LOW); digitalWrite(led5, LOW); } void loop() { control(); if( buttonf == 1 ) { joystick(); } if( buttonf == 0 ) { delay(100); } } void control() { if (( digitalRead(button) == LOW ) && (buttonf == 0)) { Serial.println(" Started"); digitalWrite(led5, HIGH); buttonf = 1; delay(1000); } if (( digitalRead(button) == LOW ) && (buttonf == 1)) { digitalWrite(led5, LOW); Serial.println("ended"); buttonf = 0; delay(1000); } digitalWrite(button , HIGH); } void joystick() { xdata = analogRead(vrx); ydata = analogRead(vry); xdata = map(xdata,0,1023,0,10); ydata = map(ydata,0,1023,0,10); /* Serial.println(""); Serial.print("xdata: "); Serial.println(xdata); Serial.print("ydata :"); Serial.print(ydata); delay(1000); */ // stop if ( (xdata == 4) && (ydata ==4) && (flag == 0) ) { digitalWrite(led1, LOW); //FORWARD digitalWrite(led2, LOW); //RIGHT digitalWrite(led3, LOW); //LEFT digitalWrite(led4, LOW); //Reverse Serial.println("Stopped"); flag = 1; delay(100); } //forward if ( (xdata > 4) && (ydata >= 4) && ( flag == 1) ) { digitalWrite(led1, HIGH); //FORWARD digitalWrite(led2, LOW); //RIGHT digitalWrite(led3, LOW); //LEFT digitalWrite(led4, LOW); //Reverse Serial.println("Forward"); flag = 0; delay(100); } //Right if ( (xdata >= 4) && (ydata > 4) && ( flag == 1) ) { digitalWrite(led1, LOW); //FORWARD digitalWrite(led2, HIGH); //RIGHT digitalWrite(led3, LOW); //LEFT digitalWrite(led4, LOW); //Reverse Serial.println("Right"); flag = 0; delay(100); } //Left if ( (xdata >= 4) && (ydata < 4) && ( flag == 1) ) { digitalWrite(led1, LOW); //FORWARD digitalWrite(led2, LOW); //RIGHT digitalWrite(led3, HIGH); //LEFT digitalWrite(led4, LOW); //Reverse Serial.println("Left"); flag = 0; delay(100); } //Backward if ( (xdata < 4) && (ydata >= 4) && ( flag == 1) ) { digitalWrite(led1, LOW); //FORWARD digitalWrite(led2, LOW); //RIGHT digitalWrite(led3, LOW); //LEFT digitalWrite(led4, HIGH); //Reverse Serial.println("Reverse"); flag = 0; delay(100); } } |
Arduino Joystick Program Explanation:
int button = 4; // The SW pin, the joystick shift pin, is attached to the Arduino pin number 4.
int vrx = A1; // The joystick’s VRx pin is attached to the Arduino analog pin A1.
int vry = A2; // The VRy joystick pin is attached to the Arduino’s analog pin A2.
Then I specified two form integer variables to store the values of VRx and VRy.
Then I identified the LEDs with some pins,
int flag = 0; // Use this to avoid unwanted code repeating.
int buttonf = 0; // this is used to control the joystick.
void setup() {
Serial.begin(9600); // Turns on serial contact. The baud rate is 9600. This is used for debugging purposes, we can simply comment on this line once the programming is done.
PinMode is a function which needs two arguments such as input, pin number or pin name, and status that can be input or output. Set the input VRx, VRy and click. Set the key to a high level of logic. Set as output all the led’s, holding all the LEDs off.
Then the work of the void loop starts.
I created two functions to keep the software simple and structured, one for the control of the joystick and one for accessing the values of VRx and VRy to control some leds.
if( buttonf == 1 ) then simply keep executing the joystick function which is a user defined function. which I will explain in a minute.
if( buttonf == 0 ) then simply keep executing this delay function. Which is a very small delay.
Void control()
This is a function specified by the user and its name is command, it has no form of return and it takes no arguments as the output.
This role consists of only two if conditions, the aim of these conditions is to switch the button state from 0 to 1 and from 1 to zero every time the joystick button is pressed, and also switches on and turns off the led, if the LED is on it means the joystick can be used to monitor the other LEDs, and if this led is off it means the joystick is not working. The electronic read feature is used to test whether the button is pressed, so the status of the button is changed every time the button is pressed.
xdata = analogRead(vrx); // reads the VRx pin and store the value in xdata.
ydata = analogRead(vry); // reads the VRy pin of the joystick and store the value in ydata.
Since the VRx and VRy pins have values ranging from 0 to 1023, I used the map function to minimize this range and restrict the maximum value to 10. I’m going to get values from 0 to 10 now.
These are the conditions under which the values stored in xdata and ydata are verified. If both values are equal all LEDs are switched off and the flag state is set to 1.
If xdata is > 4 and ydata is greater than or equal to 4 then it means forward, turn on led1 and reset the flag state to 0. In reality, you might think about the 4… 4 is the quality you get on VRx and VRy pins when the joystick is in normal condition.
Likewise for all other conditions.
Primafacie, a complete document I am looking for.
Thank you.