The reverse input of the button and LED interaction

int LED_PIN1 = 4;
int BUTTON_PIN = 7;

int buttonState = 0;

void setup() {
  pinMode(LED_PIN1, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);
}

void loop() {
  buttonState = digitalRead(BUTTON_PIN);;
  if(buttonState == LOW) {
    digitalWrite(LED_PIN1, HIGH);
  }else{
    digitalWrite(LED_PIN1, LOW);
  }
}

The above coding is the code I want to modify further to control the stepper motor. As it is very heavy and inconvenient to test it by stepper motor. I try to test by using LED first .when I press the button, The LED turn on and vice versa. However when I upload the program. The LED turns off When I press the button and turn it on when I release it. Can someone help me troubleshoot and reverse the LED output to the correct one?

Hello
Post the schematic to see how we can help.

You mean the wire setup of the arduino?

Yes- show how the button is wired... does it go from the pin to ground or from the pin to 5V, and do you have a pullup or pulldown resistor in the circuit? And show how the led is wired: where do the cathode and anode connect to?

Normal practice is to wire the button from the pin to ground, and use:

pinMode(BUTTON_PIN, INPUT_PULLUP);

That will keep the pin HIGH until it's pressed, at which point it will go low.

It looks as if you're expecting a LOW when pressed, but without either an external pullup or the use of INPUT_PULLUP, that won't be.

ps: Your schematic doesn't need to be anything fancy from a cad type system. Photo of pen and paper drawing is fine, or do some blocks and lines in Paint or Powerpoint even.

Hello giftyjeff

Take a view here to gain the knowledge how to connect buttons and LEDs proper.

Many thanks to LarryD

Have a nice day and enjoy programming in C++ and learning.
Errors and omissions excepted.

Sorry for the long waiting for the schematic. I Finally find back my tinkered account and create the schematic picture :smiley:
For the Pullup point. I have try to do so and the LED does not light up. Might be the Ohm of the resistance is too high or something else. Maybe I try it later on

@giftyjeff, welcome.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.