Why does this always return 1?

always returns 1, even if i dont press it or and even if i remove the button.

void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
   pinMode(6,INPUT);
int ingedrukt = 0;
}
int ingedrukt = 0;
void loop() {
  Serial.println(ingedrukt);
  ingedrukt = digitalRead(6);


}


button arduino

Is it because of hardware or software?

Now it always returns 0?

Welcome to the forum

Could it be that the input is floating at an unknown voltage ?

It is impossible to see how your circuit is wired from the picture. Please post a circuit diagram. A picture of a hand drawn schematic is good enough

Try using INPUT_PULLUP in pinMode() and wiring the circuit to take the input pin LOW when the button is pressed

that did not work, it just always returns 1 now. ill go make the circuit diagram on tinkercad if thats good

My guess would be that you have the switch wired wrongly. Use a jumper wire to take the input LOW instead of the switch

If you use the switch connect to 2 diagonally opposite pins to ensure that the connection is actually across the switch and not 2 shorted terminals


the diagram, there are also some led on other side but not relevant i think

Yes, thats what it looks like in the picture.

@suwn
Connect the button using the pins on opposite corners.

1 Like

thank you, this solved it. i followed the picture on the arduino site, but i switched sides yeah my bad.

AI generated content and direct replies deleted.

now i have another button who also isnt working, but i have 2 that work. do you know what could be wrong?

now i have another button who also isnt working, but i have 2 that work. do you know what could be wrong

Perhaps it is wired wrongly. Are you connecting to diagonally opposite pins on the switch ?

if im correct yes. this is the same way the other switches are also set up

It is impossible to tell from the photo exactly how the switch is connected. Where does the yellow wire connect to and what is the purpose of the resistor that may, or may not, be connected to the switch ?

Write a short sketch that simply reads the state of the input pin associated with the switch and test it


the yellow wire is for the button next to it, that button works.
but this one doesnt.
idk why i need a resistor thats just how i learned it in class. i know i can just do it with input_pullup but it makes no difference for me, then it just always returns 1\

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

void loop() {
  Serial.println(digitalRead(6));
}

@suwn ,
Reply #19 was spam, no idea if the code was any good but as I deleted #19 I also deleted your reply to the spam.

1 Like

Connect the pulldown resistor from GND to the same wire that goes to the input pin.

@PerryBebbington np

i tried a different wiring and it worked thx for the help