Become a subscriber (Free)

Join 29,000 other subscribers to receive subscriber sale discounts and other free resources.
:
:
Don't worry -- youre-mail address is totally secure. I promise to use it only to send you MicroZine.

Arduino Joystick Tutorial

This Arduino Joystick tutorial will show you how to connect an 2 axis joystick to using any two Arduino analogue inputs. The joystick has two potentiometers one for vertical movement and one for horizontal movement.


All that happens is that you put 5V at one end of the potentiometer and 0V at the other end of the potentiometer, and the wiper adopts a value in between these voltages. Then all you do is read the analogue values using an Arduino using the Arduino adc.

You can buy an Arduino joystick module as shown below - these have the power pins and two analogue outputs, but the also have  a push button pin that activates when you push the joystick down.

For this Arduino joystick the potentiometer values are 4.4kOhm, others are usually 10k Ohms.

Arduino joystick capped with a control knob

This picture shows the physical structure of the Arduino joystick - two potentiometers to top and right with the control stick in the middle.

arduino joystick with no cap (internals)
The connections are power, ground, VRx and VRy (Variable Resistance)  and SW is the push button switch.

Note: Of course whether VRx is horizontal and VRy is vertical depends on the orientation of the module. That won't really matter so change it in software as needed.

The button input pin is connected to ground when pushed, so you need a pullup on that connection - that is easily done using the internal pullup in an Arduino (see the sketch below for how to do that).

Connections to Arduino Uno


Arduino Pin
Joystick pin
5V
+5V
GND
GND
A0
VRx
A1
VRy
5
SW

Software

Arduino Joystick Sketch

The following sketch does not require a library and just reads and displays the values from analogue input A0 and A1. It also displays the button push output.

const int VRyPin = A1;
const int SWPin  = 5;

int VRx = 0;        // value read from the horizontal pot
int VRy = 0;        // value read from the vertical pot
int SW = 0;         // value read from the switch

void setup() {
  Serial.begin(9600);
  pinMode(SWPin,INPUT_PULLUP);
}

void loop() {

  VRx = analogRead(VRxPin);
  VRy = analogRead(VRyPin);
  SW = digitalRead(SWPin);

  // print the results to the Serial Monitor:
  Serial.print("VRrx = ");
  Serial.print(VRx);
  Serial.print("\tVRry = ");
  Serial.print(VRy);
  Serial.print("\tSW = ");
  Serial.println(SW);

  delay(200);
}

Sketch Output with joystick in the middle


Notice how the value is not exactly the center of the ADC vange (511) and that both are different values even though they represent the middle point.

VRrx = 524    VRry = 506    SW = 1
VRrx = 523    VRry = 506    SW = 1
VRrx = 524    VRry = 506    SW = 0
VRrx = 523    VRry = 506    SW = 0
VRrx = 523    VRry = 506    SW = 0
VRrx = 522    VRry = 505    SW = 1
VRrx = 523    VRry = 505    SW = 1
VRrx = 523    VRry = 505    SW = 1
VRrx = 524    VRry = 506    SW = 1

Sketch Output with joystick moved to limits

Horizontal Vertical
VRrx = 523    VRry = 506    SW = 1
VRrx = 512    VRry = 506    SW = 1
VRrx = 420    VRry = 506    SW = 1
VRrx = 218    VRry = 506    SW = 1
VRrx = 60      VRry = 506    SW = 1
VRrx = 0        VRry = 506    SW = 1
VRrx = 0        VRry = 506    SW = 1
VRrx = 0        VRry = 506    SW = 1
VRrx = 0        VRry = 506    SW = 1
VRrx = 0        VRry = 506    SW = 1
VRrx = 0        VRry = 506    SW = 1
VRrx = 0        VRry = 506    SW = 1
VRrx = 0        VRry = 506    SW = 1
VRrx = 253     VRry = 506    SW = 1
VRrx = 523     VRry = 506    SW = 1
VRrx = 524     VRry = 506    SW = 1
VRrx = 523     VRry = 506    SW = 1
VRrx = 524     VRry = 506    SW = 1
VRrx = 524     VRry = 506    SW = 1
VRrx = 675     VRry = 506    SW = 1
VRrx = 911     VRry = 506    SW = 1
VRrx = 1023   VRry = 506    SW = 1
VRrx = 1023   VRry = 507    SW = 1
VRrx = 1022   VRry = 506    SW = 1
VRrx = 1023   VRry = 507    SW = 1
VRrx = 1023   VRry = 506    SW = 1
VRrx = 1023   VRry = 506    SW = 1
VRrx = 1022   VRry = 506    SW = 1
VRrx = 523     VRry = 506    SW = 1
VRrx = 524    VRry = 505    SW = 1
VRrx = 523    VRry = 506    SW = 1
VRrx = 524    VRry = 505    SW = 1
VRrx = 523    VRry = 506    SW = 1
VRrx = 524    VRry = 373    SW = 1
VRrx = 523    VRry = 176    SW = 1
VRrx = 523    VRry = 0    SW = 1
VRrx = 523    VRry = 0    SW = 1
VRrx = 524    VRry = 0    SW = 1
VRrx = 524    VRry = 0    SW = 1
VRrx = 524    VRry = 0    SW = 1
VRrx = 523    VRry = 0    SW = 1
VRrx = 524    VRry = 0    SW = 1
VRrx = 524    VRry = 181    SW = 1
VRrx = 524    VRry = 320    SW = 1
VRrx = 523    VRry = 506    SW = 1
VRrx = 524    VRry = 506    SW = 1
VRrx = 523    VRry = 505    SW = 1
VRrx = 523    VRry = 505    SW = 1
VRrx = 523    VRry = 506    SW = 1
VRrx = 523    VRry = 522    SW = 1
VRrx = 523    VRry = 887    SW = 1
VRrx = 523    VRry = 1023    SW = 1
VRrx = 523    VRry = 1022    SW = 1
VRrx = 524    VRry = 1022    SW = 1
VRrx = 524    VRry = 1023    SW = 1
VRrx = 524    VRry = 1023    SW = 1
VRrx = 524    VRry = 1023    SW = 1
VRrx = 524    VRry = 506    SW = 1


The values show that the joystick returns to centre values are the same i.e. they are consistent but the values can be very different for each potentiometer.

Arduino Joystick Library

This library allows you to calibrate the Arduino Joystick and makes it easy to detect UP, DOWN,LEFT and RIGHT movements.

The parameters LOW HIGH and DIVITION determine how sensitive the code is to the values from the Arduino Joystick.

https://github.com/YuriiSalimov/AxisJoystick/blob/master/examples/SerialJoystick/SerialJoystick.ino

Download it from : Here

  1.     Unzip the download.
  2.     Change the folder name AxisJoystick-version to just AxisJoystick.
  3.     Paste the modified folder on your Library folder (On your libraries folder inside Sketchbooks or Arduino software).
  4. Restart the IDE.

Commands in the library are:

singleRead()
multipleRead()
isPress()
isUp()
isDown()
isRight()
isLeft()
readVRx()
readVRy()
readSW()
calibrate(LOW, HIGH, DIVITION);

Instead of fiddling around with pins you can swap X and Y using:

XYReplacer(original) where original is the orignial object. Note include XYReplace.h - see example here.

Library Sketch example calibrating the joystick



/*
  Joystick axes calibration
  Reads a press of the calibrated joystick and displays information
  in the default Serial.
  https://github.com/YuriiSalimov/AxisJoystick
  Created by Yurii Salimov, January, 2019.
  Released into the public domain.
*/
 #define VRX_PIN A1
#define VRY_PIN A2
#define LOW_RANGE 0
#define HIGH_RANGE 1023
#define RANGE_DIVITION 100

Joystick* joystic;

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  joystic = new AxisJoystick(SW_PIN, VRX_PIN, VRY_PIN);
  joystic->calibrate(LOW_RANGE, HIGH_RANGE, RANGE_DIVITION);
}

// the loop function runs over and over again forever
void loop() {
  Serial.print("| SingleRead: " + String(joystic->singleRead()));
  Serial.print(" | MultipleRead: " + String(joystic->multipleRead()));
  Serial.print(" | Press: " + String(joystic->isPress()));
  Serial.print(" | Up: " + String(joystic->isUp()));
  Serial.print(" | Down: " + String(joystic->isDown()));
  Serial.print(" | Right: " + String(joystic->isRight()));
  Serial.print(" | Left: " + String(joystic->isLeft()));
  Serial.print(" | VRx: " + String(joystic->readVRx()));
  Serial.print(" | VRy: " + String(joystic->readVRy()));
  Serial.println(" | SW: " + String(joystic->readSW()) + " |");
}


Mapping ADC values to a different range

Note If you want to map the output of the ADC to a different range of values then use the map function as follows:

outputValue = map(sensorValue, 0, 1023, 0, 255);

This will linearly map values with the minimum and maximum output:

0 maps to 0
and
1023 becomes 255.

Zero to 1023 becomes Zero to 255.


New! Comments

Have your say about what you just read! Leave me a comment in the box below.




Privacy Policy | Contact | About Me

Site Map | Terms of Use



Visit our Facebook Page:
To Visit Click Here


Recent Articles

  1. Arduino Interrupt : There are Some You May Never Have Heard About!

    Arduino Interrupt Tutorial: Find out how many external there are on an Arduino Uno - The answer is more than two!

    Read more

  2. Easy Switch Debounce

    Switch debounce: Three different ways to debounce input push switches with one amazing method that you can't miss.

    Read more

  3. How to use the ADS1115

    A tutorial on using the ADS1115 precision 16 bit ADC for low power use.

    Read more

  4. How to use the ADXL345 for movement sensing and more.

    With the ADXL345 acellerometer you can detect up to 16g! You can also find out how to use it for tap detection and more.

    Read more

  5. ESP8266 Webserver in Lua

    ESP8266 Webserver: This code shows you how to use lua to create a webserver using html button inputs to contrtol an LED on the ESP module.

    Read more

  6. ESP8266 NodeMCU Firmware: ESP8266 flash

    How to flash nodemcu firmware into a ESP8266 so you can use the LUA scripting language.

    Read more

Readers Comments

"I wanted to thank
you so so so much
for all the information
you have provided in
your site it's

SUPERB and FANTASTIC."

- Ranish Pottath

"This site really is
the best and my favorite.
I find here many useful
projects and tips."

- Milan

bursach<at>gmail.com<

"Awesome site,
very, very easy and nice
to navigate!"


- Matt
matt_tr<at>
wolf359.cjb.net


Learn Microcontrollers

"Interested in
Microcontrollers?"

Sign up for The
Free 7 day guide:

FREE GUIDE : CLICK HERE


"I am a newbie to PIC
and I wanted to say
 how great your
site has been for me."


- Dave

de_scott<at>bellsouth.net

"Your site is a great
and perfect work.
congratulations."


- Suresh

integratredinfosys<at>
yahoo.com

"I couldn't find the correct
words to define
yourweb site.

Very useful, uncovered,
honest and clear.

Thanks so much for
your time and works.
Regards."


- Anon

Back to Top