I bought a beginners kit and can get all but one of the examples to work. I have tried different board layouts and varied the code with no success. When I first run the code the LED is off. When I ground pin 9 the LED comes on and stays on. Grounding pin 8 does not turn off the LED once it is lit.

int ledPin = 5;
int buttonApin = 9;
int buttonBpin = 8;

byte leds = 0;

void setup() 
  pinMode(ledPin, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);  
  pinMode(buttonBpin, INPUT_PULLUP);  

void loop() 
  if (digitalRead(buttonApin) == LOW)
    digitalWrite(ledPin, HIGH);
  if (digitalRead(buttonBpin) == LOW)
    digitalWrite(ledPin, LOW);

Circuit diagram

  • Code should read:
    – Bob
    Nov 8 '19 at 11:54
  • Does pin 9 stay grounded after the LED turns on? If you ground both pins with that code you will have the LED at about half intensity, since you are turning it off and on very fast
    – chrisl
    Nov 8 '19 at 12:19
  • Pin 9 is only grounded momentarily but the LED stays on afterwards. After I press the button and release I assume both 8 and 9 are floating, but neither are grounded until I press a button again.
    – Bob
    Nov 8 '19 at 12:34
  • I have tested this with your wiring and your code and it works like a charm. I can turn off the LED with the button at pin 8. You must have done something different, than you stated in your question
    – chrisl
    Nov 8 '19 at 12:43
  • Thanks Chris. I will have another go. I have tried a few times now with no success and different configurations.
    – Bob
    Nov 8 '19 at 12:47

This kind of button has 4 pins. These pins are internally connected in pairs. It's easy to make a mistake when wiring. Please post the real picture of your wiring. Or you can refer to read more about button pin in this post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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