Joystick Button/Switch Issue

So I'm working on a small project with the UNO that requires me to use the analog joystick's button fonctionality. However I just can't seem to get it to work. The joystick only outputs 0's no matter what I do, only rarely but somewhat randomly spitting out a 1.

Here's the code:

void setup() { pinMode(10, INPUT); Serial.begin(9600); }

void loop() { Serial.print(digitalRead(10)); delay(100); }

The joystick is just attached to 5v, GND, and SW to pin 10.

The joystick is just attached to 5v, GND, and SW to pin 10

Are they the only connections to the joystick ? Where did you get the joystick from ?

Have you any idea how the switch is wired in the joystick ? Does it go to 5V or GND when pressed, for instance ?

The joystick it just one of these [attached] it should be outputting a HIGH digital on the SW pin when pressed.


Try this instead

void setup()
  pinMode(10, INPUT_PULLUP);

void loop()

Change the baud rate to suit yourself