Button is not working

Hey everyone, I have just gotten into arduino and instantly wasted about 30 mins trying to get a button to work. Please help, even after pressing the button and internally pulling up the button, the button still logs 1 when pressed.
This is my code:

void setup() {
  // put your setup code here, to run once:

  pinMode(2, INPUT_PULLUP);


  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int btn1 = digitalRead(2);


Serial.println(String(btn1));

}

Please advice me on what I should do, thank you in advance.

Welcome to the forum

Start by connecting to 2 diagonal pins of the switch to ensure that you are connected to 2 sides of the switch

I suspect your button is wired wrong. Use diagonally opposite pins. Test buy shorthing the blue and red wires together.

Woah! Thanks guys! That worked like magic!
And can I ask, why does it even matter to connect them diagonally?

The pair of pins on 2 edges of the button are connected together and the switch contacts are between each pair. If you connect to 2 adjacent pins there is a 50/50 chance that they are already connected together but a diagonal pair will always have the contacts between them

Have you got a multimeter ?

Ohh understood! I never new that lol.
Nope I dont have a multimeter

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