Won't print text when pushbutton is being pressed

It won't print the text when I am pressing the button. Code:

#define BUTTON_PIN 7

void setup(){

Serial.begin(9600);

pinMode(BUTTON_PIN, INPUT);

}

void loop(){

delay(1000);

if (digitalRead(BUTTON_PIN) == HIGH){

Serial.println("Pushbutton is being pressed");

}

else{

  Serial.println("Pushbutton is not being pressed");

}

}

The delay is blocking your processor, so for one instant every second the button is checked.

Also, how is your button wired up?

FYI

  • Unplug it from USB
  • Get rid of the resistor
  • Move the line from the 5V pin to the ground pin
  • Switch the pin mode to INPUT_PULLUP
  • Instead of looking for high, look for low
  • Until you learn to not use them lower the delay to 100

Still doesn't work, but I changed something:

Were my directions unclear?

No, I did everything you told, but it still just printed button is being pressed

I guess the were, no big deal

  • Take all the wires and the resistor off
  • You'll only need to 2 wires and the button
  • Run 1 wire from pin 7 to one side of the button
  • Run another wire from ground to the other side of the button

That's all the wires you need

Then in your program make sure you:

  • Switch the pin mode to INPUT_PULLUP
  • Instead of looking for high, look for low
  • Until you learn to not use them lower the delay to 100

If you haven't already

It helps?

1 Like

Wait, it suddenly works!! Thank you. If you don't mind, I got a couple of questions. Why do I have to switch the pinMode to INPUT_PULLUP and what does it do? Is INPUT broken or something? And what does the ground supply do?? Thanks a lot!


It should look like this
Just 2 wires

It worked but can you like explain how it works. And what INPUT_PULLUP is and ground supply?

Yes, happy to. Can you show me a picture of your wiring?

I did the same thing you did

Just wanted to be sure.

Gotta go soon. I just have a little bit of questions. Hope you dont mind. 1: What is the ground supply for. 2: What is the power supply for. 3: Why did i have to use INPUT_PULLUP instead of INPUT, and what is the difference between them? Thanks if you answer

When you're reading a button you should be looking to see if it's connected to ground because that's the safest way to do it.

When the button is pressed the pin is connected to ground, but what is the value when the button is not pressed?

We want the value to be high(5V), not low(ground) so we use a pullup resistor to allow a tiny bit of 5V to show on the pin when it's not grounded (the button is not pressed).

Your Arduino has built in pullup resistors, that's what INPUT_PULLUP does is hook it to the input pin internally.

You should try to avoid relying on the 5V pin to power anything. It's only good for a couple of LEDs. It can be used for more advanced stuff but you'll probably get to that later.

Try the link @chrisknightley provided in #10 above.

Srsly, you gotta learn to try helping yourself a bit.

a7