2

I built this circuit to learn button control.

My setup

But my code (or button) is working in reverse. I want to initialize led flash effect when the button is pressed with this code

buttonState = digitalRead(8);
if (buttonState == HIGH)
  doFlash();

Instead the effect runs continuously and pauses if I push and hold the button. If change the condition as if (buttonState == LOW) code is working as expected. (Effect starts when I push the button.)

This is just the opposite of what I read in tutorials. What is wrong in my circuit or code?

Full code:

 void setup(){
  for(int i=2;i<=6;i++) 
  pinMode(i,OUTPUT);
  pinMode(8,INPUT_PULLUP);
 }

 void loop(){
  int buttonState;

  buttonState = digitalRead(8);
  if (buttonState == LOW)
    doFlash();
 }

void doFlash() {
    int i;
    for (i=2;i<=6;i++) {
        digitalWrite(i,HIGH);
        delay(100);
        digitalWrite(i,LOW); 
    }   
    for (i=6;i>=2;i--) {
        digitalWrite(i,HIGH);
        delay(100);
        digitalWrite(i,LOW); 
    }
}
0

Most tutorials will make the code more "intuitive" by adding additional components to the circuit. E.g.

enter image description here

Here you can see one end of the button is connected to 5 volt, will the other end is connected to a digital input, and via a (pull-down) resistor to ground.

If the button isn't pressed the pull-down resistor will make the input read LOW. If the button is pressed the input will go to 5v (overpowering the pull-down resistor) and the input will read HIGH.

Like you I found the pull-up version a bit weird. But once I figured out how both versions worked, I now prefer the pull-up one. For once, because you don't need an addition resistor. (And secondly because I don't like to use unrestricted 5v signals. If in the above example you accidentally set the input to an output, and press the button, you create a short, and could blow up the micro controller.)

4

This behavior is expected: INPUT_PULLUP means the pin is pulled up via the internal pull up, i.e. it will read as high if not connected externally.

With your push button you connect it to ground (0 volts), thus bringing it to low level and it will read as low. Therefore, in your code, you need to check for low level (LOW) to check if the button is pressed (as you found out by changing the code).

2

There are two kinds of logic: "active high" and "active low". (At least that's one way to characterize it.) Your circuit is "active low", and there's nothing wrong with it. If fact, when you use the internal pull-up resistors of the microcontroller you're bound to use active low logic. (More modern microcontrollers have configurable pull-up or pull-down resistors.)

If this confuses you you can use a #define in your code:

#define ACTIVE LOW

if (buttonState == ACTIVE)
  doFlash();
0

i had the same problem and no body solved it for me.but i got the solution and it works.enter image description here

  • 1
    This is a basic pulldown circuit, same as described in Geben's answer two years ago. Given the duplication, and that you provide no explanation of how this works or changes the sense of the input, it's not a very helpful contribution to the site at this point. – Chris Stratton Jul 11 '17 at 22:19
  • Welcome to Arduino SE. Please take the tour to get the most out of this site at arduino.stackexchange.com/Tour. Meanwhile, note that the input to an Arduino input pin needs to be between 0-5 Volts and have about 10K or a bit more across it. 1K isn't enough. Search on "Arduino input pull-down" and I think you will find what you are looking for. – SDsolar Jul 12 '17 at 2:58

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.