Arduino analog joystick module
Connecting an analog joystick module to Arduino is very simple. The analog joystick module for Arduino has five pins: Ground, Vcc, X-axis, Y-axis and Key or SW.
Connect Ground and Vcc pins to Gnd and Vcc pins of Arduino, the X pin to A0, the Y pin to A1 and SW pin to Digital pin 2 of Arduino.
The analog joysticks are made from potentiometers, so the X and Y pins will output analog values. The SW/Key pin is Digital.
Arduino has an analogRead range from 0 to 1023. When using analogRead() function, the X and Y pins should show values closed to 512, if the joystick is in standby/ middle position.
Use the following code to test the setup. Open Serial Monitor and set the Baud Rate to 115200 baud.
// joystick pins const int SWITCH = 2; // digital pin 2 connected to SW output of JoyStick const int X_AX = A0; // analog pin 0 connected to X output of JoyStick const int Y_AX = A1; // analog pin 1 connected to Y output of JoyStick int rows = 0; void setup() { pinMode(SWITCH, INPUT); digitalWrite(SWITCH, HIGH); Serial.begin(115200); showHeading(); } void loop() { rows++; if (rows > 20) { rows = 0; showHeading(); } Serial.print(digitalRead(SWITCH)); Serial.print(" "); Serial.print(analogRead(X_AX)); Serial.print(" "); Serial.println(analogRead(Y_AX)); delay(100); // delay in between reads for stability } void showHeading() { Serial.println("Switch X axis Y axis"); }
Upload the sketch to Arduino and wait for output in Serial Monitor. You should see values closed to 512 when the joystick is in the standby position.
Different joysticks will output different values, depending on the quality and the brand of the joystick.
mine shows ⸮ ⸮⸮⸮⸮ and more stuff just continues to come up
Hello Matt,
Did you set up the Baud Rate correctly ?
Change the “baud” to 115200 it should appear
Mine is totally crazy! It increases the value to maximum then go back to zero then go back to maximum continuously without me touching anything 🙁 I’ve tried so many basic tutorials for this :(( maybe something with my notebook source? When I unplug it from the socket the numbers change and become more static T………T
Hello,
Try with another analog joystick module. The one you use now may be broken.
mine keeps showing 0 for x and 0 for y. this is the second joystick i’ve tested showing this. do you know the cause?
I tried this sketch and the x-axis worked fine but the y-axis shows 512 constantly and only slightly changes (to about 495) when I push it in the negative direction. I thought maybe it was faulty so I tested it with a potentiometer and, sure enough, the x-axis has a variable resistance while the y axis only reads a 0.
Is there anything I could try to fix it or to make sure that it’s in fact broken? Or should I just eat my pride and buy a new one?