Arduino Joystick behaving weirdly

Discussion in 'The Projects Forum' started by joaobcosta, Mar 31, 2019.

  1. joaobcosta

    Thread Starter New Member

    Dec 18, 2018
    6
    0
    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 (Text):
    1. void setup() {
    2.   Serial.begin(9600);
    3.   pinMode(A0,INPUT);
    4.   pinMode(A1,INPUT);
    5. }
    6.  
    7. void loop() {
    8.   Serial.print("X: ");
    9.   Serial.print(analogRead(A0));
    10.   Serial.print("    Y: ");
    11.   Serial.print(analogRead(A1));
    12.   delay(300);
    13. }
     
  2. danadak

    Distinguished Member

    Mar 10, 2018
    2,789
    601
    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.
     
  3. joaobcosta

    Thread Starter New Member

    Dec 18, 2018
    6
    0
    How can I solve the floating and coupling problem?
     
  4. danadak

    Distinguished Member

    Mar 10, 2018
    2,789
    601
    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: Mar 31, 2019
  5. DNA Robotics

    Member

    Jun 13, 2014
    453
    171
    Some joysticks are not analog. Are you sure it has potentiometers?
     
  6. joaobcosta

    Thread Starter New Member

    Dec 18, 2018
    6
    0
    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!!
     
Loading...