0

I have a joystick and am using the following code to show the output to the serial monitor:

// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output

void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(115200);
}

void loop() {
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  delay(500);
}`

Even when I am not moving the joystick, the X and Y axis outputs change continuously. Anywhere from around 50 to 600 (it seems.)

Every read has a new number.

What could be causing this?

I used the guide on https://www.brainy-bits.com/arduino-joystick-tutorial/

enter image description here

  • 1
    Can you observe the voltage at X and Y via a multimeter or an oscilloscope? Your code looks right but the hardware might have a problem. Relaxed position should read 512 (~2.5V) – Maximilian Gerhardt Mar 30 '18 at 13:27
  • const int X_pin = 0; should technically be const int X_pin = A0;, but if I recall shouldn't matter. – Gerben Mar 30 '18 at 14:06
  • 1
    This is a fairly simple project. I suspect bad parts and suggest you swap out the joystick then, if that doesn't work, the Arduino. If that is not an option: consider if only 1 of the 2 analog input has noise on it that wiring for that input from the joystick is wrong or broken. If both analog inputs have noise on them inspect both the ground and 5 volt wires. However I doubt that is the problem as if either worked then all the analog readings would always be zero (only ground is connected) or its maximum (only 5 volts is connected) which I believe is 255. – st2000 Mar 30 '18 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.