Arduino joystick: Easy setup and use on the Arduino (2024)

  1. Home
  2. Arduino Interfaces
  3. Arduino Joystick

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

All that happens is that you put 5V at one end of the potentiometer and 0V at theother end of the potentiometer, and the wiper adopts a value in between thesevoltages. 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 havethe power pins and two analogue outputs, but the also have a pushbutton 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: Easy setup and use on the Arduino (1)

Thispicture shows the physical structure of the Arduino joystick - twopotentiometers to top and right with the control stick in the middle.

Arduino joystick: Easy setup and use on the Arduino (2)

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 onthe orientation of the module. That won't really matter so change it insoftware as needed.

The button input pin is connected to groundwhen pushed, so you need a pullup on that connection - that is easilydone using the internal pullup in an Arduino (see the sketch below forhow to do that).


Arduino Joystick Sketch

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

const int VRyPin = A1;const int SWPin = 5;int VRx = 0; // value read from the horizontal potint VRy = 0; // value read from the vertical potint SW = 0; // value read from the switchvoid 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);}

[File:joystick.ino]

joystick in the middle - output

Notice how the value is not exactly the center of the ADC vange (511)and that both are different values even though they represent themiddle 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

Joystick moved to limits - output

Horizontal

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

Vertical


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 center values are thesame i.e. they are consistent but the values can be very different for each potentiometer.

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 Libraryfolder (On your libraries folder inside Sketchbooks or Arduinosoftware).
  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.

Sketch - 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 100Joystick* joystic;// the setup function runs once when you press reset or power the boardvoid 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 forevervoid 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()) + " |");}

[File:example from github: ]

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.

Note: See the 'arduino map' page to properly use the map function - there are some subtle problems there.

×

Top of page

Arduino Joystick

Connections to Arduino Uno

Software
Arduino Joystick Sketch
joystick in the middle - output
Joystick moved to limits - output

Arduino Joystick Library
Sketch - calibrating the joystick

Range Mapping ADC values

Comments

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

Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.

Privacy Policy| Contact| About Me

Site Map| Terms of Use

Arduino joystick: Easy setup and use on the Arduino (2024)
Top Articles
Village of Barrington - A Look at Yesterday, Today, and Tomorrow
Best & Fun Things To Do + Places To Visit In Barrington, Illinois
Spasa Parish
Rentals for rent in Maastricht
159R Bus Schedule Pdf
Sallisaw Bin Store
Black Adam Showtimes Near Maya Cinemas Delano
Espn Transfer Portal Basketball
Pollen Levels Richmond
11 Best Sites Like The Chive For Funny Pictures and Memes
Things to do in Wichita Falls on weekends 12-15 September
Craigslist Pets Huntsville Alabama
Paulette Goddard | American Actress, Modern Times, Charlie Chaplin
What's the Difference Between Halal and Haram Meat & Food?
R/Skinwalker
Rugged Gentleman Barber Shop Martinsburg Wv
Jennifer Lenzini Leaving Ktiv
Justified - Streams, Episodenguide und News zur Serie
Epay. Medstarhealth.org
Olde Kegg Bar & Grill Portage Menu
Cubilabras
Half Inning In Which The Home Team Bats Crossword
Amazing Lash Bay Colony
Juego Friv Poki
Dirt Devil Ud70181 Parts Diagram
Truist Bank Open Saturday
Water Leaks in Your Car When It Rains? Common Causes & Fixes
What’s Closing at Disney World? A Complete Guide
New from Simply So Good - Cherry Apricot Slab Pie
Drys Pharmacy
Ohio State Football Wiki
FirstLight Power to Acquire Leading Canadian Renewable Operator and Developer Hydromega Services Inc. - FirstLight
Webmail.unt.edu
2024-25 ITH Season Preview: USC Trojans
Restored Republic December 1 2022
12 30 Pacific Time
Jami Lafay Gofundme
Litter-Robot 3 Pinch Contact & Dfi Kit
Greenbrier Bunker Tour Coupon
No Compromise in Maneuverability and Effectiveness
Black Adam Showtimes Near Cinemark Texarkana 14
Ice Hockey Dboard
Über 60 Prozent Rabatt auf E-Bikes: Aldi reduziert sämtliche Pedelecs stark im Preis - nur noch für kurze Zeit
Wie blocke ich einen Bot aus Boardman/USA - sellerforum.de
Craigslist Pets Inland Empire
Infinity Pool Showtimes Near Maya Cinemas Bakersfield
Hooda Math—Games, Features, and Benefits — Mashup Math
Dermpathdiagnostics Com Pay Invoice
How To Use Price Chopper Points At Quiktrip
Maria Butina Bikini
Busted Newspaper Zapata Tx
Latest Posts
Article information

Author: Roderick King

Last Updated:

Views: 5647

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.