Analog Joystick Inaccurate Values Problem

Hi everyone. I am new to Arduino programming and I am configuring an analog joystick (3.3V, overall 4 pinout) with Arduino mega 2560. Even with basic code to read analog values (working on online simulator like Wokwi), the values occur linearly no matter the position of the joystick. At 0 position without movement, the values surf from 0 to 1023 and back at both x and y position. I am having 3 similar joysticks with many other Arduino boards, and the problem still persists. Any idea why the readings could be such?

Welcome to the forum

Please post your sketch, using code tags when you do, and a schematic of your project. A photo of a hand drawn circuit is good enough as long as it shows how the components are connected and powered and the type of Arduino board that you are using

const int xAxisPin = A1;
const int yAxisPin = A2;
int lastX = 0;
int lastY = 0;
void setup() {
  Serial.begin(9600);
}

void loop() {
  int currentX = analogRead(xAxisPin);
  int currentY = analogRead(yAxisPin);

  Serial.print("x= ");
  Serial.print(currentX);
  Serial.print("y= ");
  Serial.print(currentY);
  delay(5000);
}

Do you have the joystick ground terminal connected to Arduino's GND?

I await the schematic with interest

1 Like

only when nothing connected to analog input it reads sinusoidal in range 0 to 1023.

We need:
links to the joystick data sheet

A schematic showing the connections

Yes

As a new user, I cannot upload attachments.

To post images etc. you need trust level 1, you can get there by:

  • Entering at least 5 topics
  • Reading at least 30 posts
  • Spend a total of 10 minutes reading posts

Users at trust level 1 can...

  • Use all core Discourse functions; all new user restrictions are removed
  • Send PMs
  • Upload images and attachments

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.