Arduino Joystick behaving weirdly

Thread Starter

joaobcosta

Joined Dec 18, 2018
6
So I was trying to connect an Arduino Nano to a joystick module and get the joystick's output. Turns out it is outputting seemingly random values.
So I connected the 5V of the joystick to the 5V of the arduino, the GND to GND and the X and Y to the A0 and A1 pins. I will put the code below. Now when I run the program It seems to output random values that start at 1023, stay that way for a few seconds and then go abruptly down to 0 and stay that way and then suddenly back to 1023 and the cycle goes on and on. It somehow worked for like 20 seconds even though I hadn't changed anything at all. I doubt the problem is in the Arduino, since I tested the same thing with two potentiometers and it works just fine.
Any ideas?
Code:
void setup() {
  Serial.begin(9600);
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
}

void loop() {
  Serial.print("X: ");
  Serial.print(analogRead(A0));
  Serial.print("    Y: ");
  Serial.print(analogRead(A1));
  delay(300);
}
 

danadak

Joined Mar 10, 2018
3,509
Sure sounds like the pin is floating and getting coupling
that is causing it to go from full scale to zero scale. Or the
wiper on joystick needs cleaning.


Regards, Dana.
 

danadak

Joined Mar 10, 2018
3,509
If the pin is not connected to anything then config it as output,
and write a "0" to pin. Or terminate with a pull up or pulldown,
1-10K ohms if it has to be configed as input.

Coupling occurs primarily due to C coupling to a pin that has hi Z.
For example a pin configed as input, with hi Z, and adjacent to a pin
carrying a PWM output or UART output would be a good candidate
for a coupling problem. So layout, un-terminated inputs are primary
causes of this.

https://www.microchip.com/forums/m66582.aspx
www.ti.com/lit/an/scba004d/scba004d.pdf


Regards, Dana.
 
Last edited:

Thread Starter

joaobcosta

Joined Dec 18, 2018
6
Yes it has the potentiometers. Anyways it is already working. I dont exactly know what I did. But thanks a lot for all your help!!
 
Top