Button not working

TL;DR: am having a problem with buttons in Arduino because the LED wouldn't light up whenever I press the button. Code seems to be working fine, the board I set up, however does not.

Hello everyone, I am learning inputs and buttons with Arduino at the moment and I'm struggling to get my code working as intended which is why I am making this post.

The code:

#include <Arduino.h>

// Global Variables

/**The pin that the button is connected to*/
const int kPinButton = 2;

/**The pin that is connected to our LED*/
const int kPinLed = 13;

// Function definitions

/** Run once after the Arduino is connected*/
void setup()
  pinMode(kPinButton, INPUT_PULLUP);// Turn on pull-up resistor

  pinMode(kPinLed, OUTPUT);

/** Run repeatedly after the Arduino is connected*/
void loop()
    if(digitalRead(kPinButton) == LOW)
        digitalWrite(kPinLed, HIGH);
        Serial.println("LED On");
        digitalWrite(kPinLed, LOW);
        Serial.println("LED Off");

This code makes it so that whenever I press the button, the LED lights up. I have also made it so that whenever I pressed/unpressed the button, the serial prints out whether the LED is on or not. The log I get, however, are messages of"LED Off" getting continuously spammed to the monitor. I suspect that the issue is in the board I set up itself, so here is the board I set up:

What I have tried so far:

  • Going online to find multiple solutions to the problem
  • Tried rotating the button 90 degrees
  • Changing up my wires
  • Changing pins
  • Getting help from different forums
  • Changing the baud rate

Any more suggestions is appreciated. Thanks in advance! :slight_smile:

EDIT: Just realized that I had to put an tag for the image to show up.

Here's the OP's picture:

It all looks OK to me.

I had some of those pushbuttons in my original Arduino starter kit and I found them really awkward. They would pop into the breadboard in a way that felt about right, but often they didn't make electrical contact with anything.

To test for this, remove the switch and use the tip of the jumper wire to connect/disconnect instead.

To test for this, remove the switch and use the tip of the jumper wire to connect/disconnect instead.

I'm really new to this, but after I have removed my switch where do I place my jumper wire exactly? Apology if it was already obvious. :slight_smile:

From your photo it looks like you've got a yellow wire and a dark-blue wire connected to either side of the switch.

Then you removed the switch. Unplug the yellow wire from the breadboard and plug it in the same row as the dark blue wire, so the yellow and blue wires are directly connected.

Does the LED come on?
What do you see on the serial monitor?

Hey Gypsum, here is the result along with what I did.

And yes, the LED did turn on.

Great news.

And now the serial monitor is spamming you with "LED On", yeah?

Looks like the crappy little switch is the problem.

This is a little late, but the log spams "LED On" and "LED Off" continuously. Even with this, do you suspect that the switch is still a problem and how do I fix it? Thanks in advance!

Still trying to fix the program and nothing is working. As GypsumFantastic stated, I think this is happening because of the button itself. Still, I don't know what to do next after this, I'm really new to this and I'd love anybody who can point me into the right direction. Do I buy new buttons or is there a way to fix it? As I have asked previously:

Even with this, do you suspect that the switch is still a problem and how do I fix it?

I'm really in need of help right now so any help is always appreciated. Thanks!

That's just how your program is written.

loop() repeats really really really fast. Dozens of times a second. Every time the loop repeats, the Arduino checks the state of the input pin, and every time it checks the pin it does a serial print. So you see dozens and dozens of LED On and LED Off messages in serial monitor. That's just what your sketch says.

Another approach is to make your sketch remember the state of the pin, and only print a message whenever it changes. There's a tutorial built into the IDE called 02.Digital - State Change Detection that will teach you how to do it.

I'm guessing you're working through these tutorials because what you've showed us in this thread is very similar to 2.Digital - Button. So my advice is to just keep going. :slight_smile:

Hey Gypsum, thanks for the short and clear explanation! And as for the button itself that you said was the problem in your previous reply, I'd like to know whether to buy new buttons or is there a fix for it? If I have to buy a new one which one should I buy? (I don't know when I bought it, it will be the right one or not)

It is not a problem with the button.

The problem is that the breadboard has such deep holes that the short pins on the button do not properly penetrate.

The button - and almost all of its ilk - is designed to be soldered into a PCB, not mounted on a breadboard. So you will simply not find buttons that will fit.

Sooner or later, you will need to learn to solder. If you do not already, you need to find a mentor who is already set up to solder things, who will teach you to solder wire off-cuts to the switch pins so that they fit properly into the breadboard.

Yeah like Paul says, you could solder longer legs onto the pushbutton.

Personally I just learned to live without them - when a tutorial said to use a switch, I just used a bit of wire that I could plug & unplug in the breadboard easily to make/break the connection - it's how the switch works inside anyway.

I have actually used those buttons on breadboards without any problems, though I was a bit skeptical at first. You can actually get a tiny bit more length on the legs if you press each side hard against a flat surface to get the bends right against the button package instead of sticking out a bit as they are when you get the buttons.

I have a friend who has been using breadboarded circuits with these constantly for years now with no problems. I tried to explain to him that the breadboard is only for temporary use and he should let me solder up a permanent version but he won't have it.

I have actually used those buttons on breadboards without any problems, though I was a bit skeptical at first. ...

In my limited experience, the buttons work fine the first time you put them into a solderless breadboard. However, with repeated repositioning, the legs become deformed and will just pop out of the holes spontaneously.

@Fester-sama: when you hook up a 4 pin button, make your connections across a diagonal, rather than on the same side. That way it will always be correct, and you won't have to worry if the switch is the right way around.

Like this:

I'll believe that. They have to be only getting contact on the very end of the leg so if you lose even a little of that length you're in trouble. These were also brand new breadboards, not ones that have had their spring contacts loosened by use and abuse.

Alright, thank you everyone for helping me! My knowledge of electronics is limited right now, but I hope I can get better soon! :slight_smile: