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.
This
picture shows the physical structure of the Arduino joystick - two
potentiometers to top and right with the control stick in the middle.
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);
}
[File:joystick.ino]
The values show that the joystick returns to center values are the
same 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
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.
/*
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()) + " |");
}
[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.
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.