Joystick is only showing values between 1020 and 1023 instead of 0 and 1023

Hi, i´m using this Joystick Module Joystick Module with Push Button for Arduino

I wrote a code wich is similar to everyone i have found on the internet, but for some reason i only get values between 1018 and 1023 and 1022 when the potentiometers are in the middle.
my code:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.print("X axis:");
  Serial.print(analogRead(0)); //print the analog value of the X axis
  delay(100);
  Serial.print("   Y axis:");
  Serial.println(analogRead(1)); //print the analog value of the Y axis
  delay(100);
}

This are some of the values that i get straight from the serial monitor.
X axis:1022 Y axis:1022
X axis:1022 Y axis:1022
X axis:1022 Y axis:1022
X axis:1022 Y axis:1022
X axis:1022 Y axis:1021
X axis:1022 Y axis:1022
X axis:1022 Y axis:1022
X axis:1022 Y axis:1022
X axis:1021 Y axis:1022
X axis:1022 Y axis:1022
X axis:1022 Y axis:1022
X axis:1021 Y axis:1022
X axis:1022 Y axis:1022
X axis:1022 Y axis:1022
X axis:1022 Y axis:1022
X axis:1022 Y axis:1022
X axis:1022 Y axis:1022
X axis:1022 Y axis:1022
X axis:1022 Y axis:1022
X axis:1022 Y axis:1022
X axis:1022 Y axis:1022
X axis:1021 Y axis:1022
X axis:1021 Y axis:1021
X axis:1021 Y axis:1022
X axis:1022 Y axis:1021
X axis:1021 Y axis:1021
X axis:1018 Y axis:1021
X axis:1018 Y axis:1021
X axis:1018 Y axis:1020
X axis:1018 Y axis:1021
X axis:1018 Y axis:1019
X axis:1018 Y axis:1017
X axis:1020 Y axis:1017
X axis:1020 Y axis:1017
X axis:1020 Y axis:1018
X axis:1020 Y axis:1017
X axis:1022 Y axis:1017
X axis:1023 Y axis:1018
X axis:1023 Y axis:1020
X axis:1023 Y axis:1020
X axis:1023 Y axis:1021
X axis:1023 Y axis:1023
X axis:1018 Y axis:1023
X axis:1017 Y axis:1023
X axis:1010 Y axis:1020
X axis:1003 Y axis:1020
X axis:997 Y axis:1022
X axis:999 Y axis:1023
X axis:999 Y axis:1023
X axis:1007 Y axis:1023
X axis:1006 Y axis:1023
X axis:1015 Y axis:1023
X axis:1015 Y axis:1023
X axis:1014 Y axis:1020
X axis:1013 Y axis:1016
X axis:1014 Y axis:1015
X axis:1016 Y axis:1020
X axis:1007 Y axis:1020
X axis:1002 Y axis:1020
X axis:1000 Y axis:1023
X axis:999 Y axis:1023
X axis:997 Y axis:1023
X axis:996 Y axis:1023
X axis:995 Y axis:1023
X axis:996 Y axis:1023
X axis:997 Y axis:1020
X axis:994 Y axis:1015
X axis:994 Y axis:1015
X axis:993 Y axis:1015
X axis:1012 Y axis:1015
X axis:1011 Y axis:1017
X axis:1012 Y axis:1021
X axis:1007 Y axis:1023

As you can see the Y axis goes between 1015 and 1023, with the centre in 1022.
The X axis on the other hand goes completly mad. It starts with 1022 being in the centre and going between 1018 and 1023, but after some rotations it ends up going down to 995. After some time it seems to get back to (1020-1023).
I´ve checked the conections by changing the pins and the wires, but it doesn´t seem to work.
I have also tried to assign bigger values with the 0 in the centre by changing the value to ((analog read(0)-1022)*100). but that just gives me the values -200, -100, 0 and 100. Nothing in the middle, so i guess the potentiometers aren´t that precise.

Thank you for reading and to anyone who can help.

The joystick is defective, or the ground connection is bad or missing. Post a clear wiring diagram

1 Like

this is the method i´m using to conect it. Sorry for not posting it directly.

GND with GND, 5V to 5V, X axis potentiomenter to A0 and Y axis potentiomenter to A1.

Some parts of your code seem to be missing!

Paul

Check the GND wire. It may be broken (possibly inside the insulation) or perhaps not making a good connection.

Or else as jremington says your joystick is bad. If you have a multimeter check the voltage on the joystick output pins. If you don't have a multimeter now would be a good time to get one.

Steve

Ok, I rechecked the wires asking a friend some of his and it works fine now. I guess i´ll have to change my wires, because none of them worked.

Sorry everybody, in the end it was a dumb problem. But thanks for replying.

Your code tries to read data from pins 0 and 1, which are TX and RX.

Try changing that to A0 and A1.

I'm pretty sure that analogRead() interprets "0" and "1" as A0 and A1.

Source code to analogRead() is in this thread. Comment specifically states "allow channel or pin numbers".

jremington:
I'm pretty sure that analogRead() interprets "0" and "1" as A0 and A1.

Mmm... Can be, I've heard about that issue before. It absolutely has to rate as one of the worst design decisions by the Arduino IDE developers.

If in doubt look at the source code... Yes analogRead interprets small numbers as analog channels, large ones
as full Arduino pin codes (these happen to not overlap). To quote the source for analogRead():

	if (pin >= 14) pin -= 14; // allow for channel or pin numbers

Originally analogRead() only took small numbers, 0 for analog pin 0, etc etc. The generalization to allow
using A0 and A1 etc was added later to allow consistent behaviour with pinMode/digitalWrite. Historical
reasons basically.

What worries me is that the code uses hard-coded 14 instead of the A0 #define.... There's a bunch of
conditionally compiled tests that could all be simplified to

	if (pin >= A0) pin -= A0; // allow for channel or pin numbers