2-Axis Joystick Arduino Project, Joystick Button & Joystick Library Arduino

(Last Updated On: October 19, 2019)

Joystick

Description:

2-Axis Joystick Arduino Project- In this Tutorial, you will learn how to use a 2-Axis analog joystick with Arduino and control some led’s as per the movement of the joystick. We have placed 4 LEDs in such a way that it represents the direction of the joystick shaft movement.


Joystick

This joystick also has a push button which can be used for various other purposes or it can be left unconnected. In this tutorial I am using the joystick push button for activating and deactivating the joystick. When the joystick push button is pressed an Led is turned on which means that the joystick is ready and can be used to control the led’s. When the joystick push button is pressed again the Led is turned off which means that the joystick cannot be used to control the led’s, this Tutorial covers each and every detail, this tutorials covers

  1. Complete Circuit diagram explanation
  2. Interfacing
  3. Programming and finally
  4. Testing

For the step by step detailed explanation watch video given at the end of this Article.



The components and tools used in this project can be purchased from Amazon, the components Purchase links are given below:

Arduino Uno: https://amzn.to/2tVz1vu
Mega 2560: https://amzn.to/2ze1kdu
Joystick: https://amzn.to/2E2Q0kI
Potentiometer: https://amzn.to/2JSLbfg
330 ohm resistors pack: https://amzn.to/2NtlAeh
5v 2A adaptor: https://amzn.to/2GsNfcw
2n2222 npn transistors: https://amzn.to/2EmXsIb
10k Resistor: https://amzn.to/2KTd6OW
lm7805 Voltage Regulator: https://amzn.to/2IZEl5x
330-ohm resistors pack: https://amzn.to/2NtlAeh
female DC power jack socket: https://amzn.to/2KEo1gt
470 uf capacitors: https://amzn.to/2xDCOzf
5×7 cm vero board: https://amzn.to/2OHGbvn
female headers: https://amzn.to/2zqmtiJ
connection wires: https://amzn.to/2DpBuW7
Super Starter kit for Beginners: https://amzn.to/2KJvmKG
Jumper Wires: https://amzn.to/2KMoVXs
Bread Board: https://amzn.to/2MS4q8X
12v Adaptor: https://amzn.to/2Ntr6h1
PCB plate: https://amzn.to/2IUwpCt
Variable Supply: https://amzn.to/2MT4Qfj
Digital Multimeter: https://amzn.to/2Nvft9i
Vero Board / strip board: https://amzn.to/2MTf9jD
Soldering iron kit: “best” You guys should definitely purchase this: https://amzn.to/2zfoNuJ
Solder wire: https://amzn.to/2ufUMWf
Wire Stripper: https://amzn.to/2KOqxfU
wire cutter: https://amzn.to/2ucIq14
PCB small portable drill machine: https://amzn.to/2Nu62XF

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!

 

Joystick

This joystick is nothing but a combination of two potentiometers for X and Y plane respectively. It reads the voltage through the potentiometer and gives analog value to the Arduino, and the analog value changes as we move the joystick shaft (which is simply the potentiometer pointer.


Joystick

Joystick

As you can see this joystick has total of 5 pins clearly labeled with GND, +5V, VRx, VRy, and SW. This joystick also has a push button.


Circuit Diagram:

Joystick

This is the complete circuit diagram designed in cadsoft eagle 9.1.0 version. If you want to learn how to make a schematic and PCB then watch my tutorial, the link is given below.

Learn how to make a Schematic & PCB:

The joystick ground pin is connected with the Arduino’s ground. +5v pin is connected with the Arduino’s 5 volt. The VRx and VRy pins are connected with the analog pins A1 and A2 while the SW pin of the joystick is connected with the digital pin 4 of the Arduino. 330 ohm resistors are connected with all the LEDs. These are the current limiting resistors. These four LEDs are used to represent the Forward, Right, Left, and Reverse movement as per the joystick shaft movement, while the 5th led is used as the indicator.


2-Axis Joystick Interfacing with Arduino:

Joystick

The joystick and all the led’s are connected with the Arduino as per the circuit diagram. Now let’s discuss the Arduino programming.

Arduino Joystick Library:

The use of the 2-Axis Analog Joystick with the Arduino is really easy, and you don’t really need a Library. You can use the X-Axis and Y-Axis without any problem. So in this Tutorial, we are not going to use any Joystick Library.


2-Axis Joystick Arduino Programming:


Program Explanation:

int button = 4; // the SW pin which is the switch pin of the j-stick is connected with pin number 4 of the Arduino.

int vrx = A1;  // the VRx pin of the J-stick is connected with the analog pin A1 of the Arduino

int vry = A2; // the VRy pin of the L-stick is connected with the analog pin A2 of the Arduino.

Then I defined two variables of the type integer for storing the VRx and VRy values.

Then I defined some pins for the LEDs,

int flag = 0; // this is used to stop the unnecessary repetition of code.

int buttonf = 0; // this is used to control the J-stick.

void setup() {

 Serial.begin(9600); // activates the serial communication. 9600 is the baud rate. This is used for the debugging purposes, once the programming is completed then we can simply comment this line.

pinMode is a function and it takes two arguments as the input, the pin number or pin name and the status which can be input or output. Set the VRx ,VRy and button as input. Set the button at logic level high. Set all the led’s as output, keep all the LEDs in off state.

Then starts the void loop function.

To keep the code simple and organized I created two functions, one for the J-stick control and another one for accessing the VRx and VRy values to control some led’s.

if( buttonf == 1 ) then simply keep executing the J-stick 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 user defined function and its name is control, it has no return type and it doesn’t take any arguments as the input. This function consists of only two if conditions, the purpose of these conditions is to change the buttonf state from 0 to 1 and from 1 to zero each type the joystick button is pressed, and also turns on and turns off the led, if the LED is On it means the joystick can be used to control the other LEDs, and if this led is off it means the joystick is not active. The digitalread function is used to check if the button is pressed, so each time the button is pressed the state of the buttonf is changed.Joystick is a user defined function; it has no return type and doesn’t take any arguments as the input.

  xdata = analogRead(vrx); // reads the VRx pin and store the value in xdata.

  ydata = analogRead(vry); // reads the VRy pin of the J-stick and store the value in ydata.

As the VRx and VRy pins gives values that ranges from 0 to 1023, to reduce this range I used the map function and limit the maximum value to 10. So now I will get values from 0 to 10.These are the conditions which are used to check the values stored in xdata and ydata. If both the values are equal then turn off all the LEDs and change the flag state to 1.If the xdata is > 4 and ydata is greater than or equal to 4 then it means forward, turn on led1 and change the flag state back to 0. You might be thinking about the 4… 4 is actually the value that you get on pins VRx and VRy when the j-stick is in normal state.

Similarly for all the remaining conditions.

For the practical demonstration and complete explanation watch the following video.


Joystick Arduino Projects:

Some of my advanced level projects in which the same Joystick module has been used.

Wireless Joystick controlled Robot Car using Arduino, 433Mhz RF and L298N Motor Driver

https://www.electroniclinic.com/wireless-joystick-controlled-robot-car-using-arduino-433mhz-rf-and-l298n-motor-driver/

Arduino IOT Project: Nodemcu ESP8266 wifi Robot Car “L298N motor driver + Blynk + Joystick”

https://www.electroniclinic.com/arduino-iot-project-nodemcu-esp8266-wifi-robot-car-l298n-motor-driver-blynk-joystick/

Wireless Hand gesture controlled Robot with Flex Sensor using Arduino

https://www.electroniclinic.com/wireless-hand-gesture-controlled-robot-with-flex-sensor-using-arduino/

CD ROM stepper motor Arduino l298n + Joystick controlled speed and Direction Control

https://www.electroniclinic.com/cd-rom-stepper-motor-arduino-l298n-joystick-controlled-speed-and-direction-control/


Watch Video Tutorial:

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Close