2-Axis Joystick Arduino Programming and Circuit Diagram

2-Axix Joystick

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.

2-Axis Joystick

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,

  1. Circuit diagram explanation
  2. Joystick interfacing with Arduino
  3. Arduino Programming and
  4. 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!

2-Axis Joystick

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.

2-Axis Joystick

2-Axis Joystick

The 2-axis Analog joystick module has a total of 5 pins which are labeled with

GND

+5V

VRx

VRy and

SW.


Circuit Diagram:

2-Axis Joystick

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:

2-Axis Joystick

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:

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 specified two form integer variables to store the values of VRx and VRy. 

Then 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.

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 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 to 1023, used the map function to minimize this range and restrict the maximum value to 10. I’m going to get values from 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.

Watch Video Tutorial

Recommended For You

Fawad

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: