I have been using https://wokwi.com/ to learn Arduino and I am having a problem that seems to be happening no matter what I do. I am trying to set up a push button and an LED. The end goal here is to set it up so that when I tap the push button down and release it that the LED blinks like a turn signal in a car. When I push it down and release it again, the LED should stop blinking. I have tried tons of different code, even using code copied and pasted directly from Arduino's website. What ends up happening is that the button is unreliable. Sometimes the button will click, other times it won't register. It is very bizarre behavior. I read about the bouncing effect but any code I put in to ignore bouncing doesn't seem to have any effect on the system. Why are push buttons so unreliable? I know in real life they aren't because I am able to type this using presumably push buttons on my keyboard. What am I doing wrong??

Here is an example. The push button is totally unpredictable. Why is it behaving like this?

enter image description here

int state = 0;

void setup()
  pinMode(9, OUTPUT);
  pinMode(6, INPUT);

void loop()
  if (digitalRead(6)) {
    state = !state;
  digitalWrite(9, state);
  • the problem is on line 20
    – jsotola
    Jul 19 at 15:15
  • I am counting 15 lines
    – wgm
    Jul 19 at 15:33
  • 2
    no other guess was possible without seeing the code
    – jsotola
    Jul 19 at 15:53
  • you have a logic error in your code ... the code is similar to if it's dark outside, then flip the light switch ... you'd be turning the lights on and off all night long, and the light stands a chance to be left on all day
    – jsotola
    Jul 19 at 16:47
  • if (!digitalRead(6)) { state = !state; } Is that correct?
    – wgm
    Jul 19 at 18:53

1 Answer 1


You've got the button wired wrong. Your resistor is inline with the signal going to the pin. It should be going from the pin to 5V. The button should be wired directly to the pin and ground. Or you can use INPUT_PULLUP in pinMode instead of INPUT and eliminate the resistor entirely.

Your code should test for the button pin being LOW, not HIGH. When you press the button it will read LOW. When unpressed it will read HIGH. This is the normal way that buttons are wired up.

Also note that your loop() function will repeat thousands of times per second. As long as you still have your finger on the button it will keep cycling the state. That means that every time you press the button, the state gets cycled a few tens of thousands of times and there's no real way of knowing which state it will be on when you finally get your finger off the button.

You should look at the "State Change Example" in the Arduino IDE. It shows how to keep track of the previous state of the button so you can react once to each press.

  • Do you know what I can Google to find that State Change Example? Having trouble finding it. Thanks for the explanation
    – wgm
    Jul 19 at 17:59
  • I think Delta_G means this one: docs.arduino.cc/built-in-examples/digital/StateChangeDetection
    – chrisl
    Jul 19 at 19:14
  • @wgm, open the Arduino IDE. Go to the File menu. File->Examples->Digital. State Change Detection will be towards the middle. If you google "Arduino State Change Example" you will be flooded with information and tutorials. Surely one of them is your speed.
    – Delta_G
    Jul 19 at 20:19
  • Do you know what I can Google to find that State Change Example - see my website: gammon.com.au/switches - it also talks about transitions
    – Nick Gammon
    Jul 20 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.