Button pull down

Hello guys, I have a problem, I’m trying to connect an ordinary button pull down, but I can’t do it at all, I connect, for example, pin D2 via a 10K resistor to the minus and the led lights up, but it should be off, and when I connect the plus to the button, it doesn’t light up, but it should would be the other way around…help

You leave a lot unsaid about what you are connecting. Are you connecting D2 to LED to button to resistor to V+/- ? This sounds like expected behavior If D2 is HIGH. Note that if you move the resistor from V- (GND) to V+, you will need to flip the LED to get it to come on when D2 is LOW.

Generally, a pull-down resistor is used to prevent an input pin from “floating” between on & off so the Arduino is never sure when the button is disconnected. This is different than a current-limiting resistor used to prevent an LED from drawing too much power.

Ignoring the LED for a minute, a pull-down resistor would be used to connect an input pin to the opposite power as the button. If you are using a button to connect D2 to V+, then the resistor would connect D2 to V-. When the button is up, the resistor provides a restricted connection to V-, so the pin will always read LOW. When the button is down, it connects D2 to V+, so it will read HIGH. You still have the restricted connection to V-, which is causing a short; which is why a fairly high resistor is used. You can swap the button and resistor if you want the button to pull the pin LOW instead of HIGH.

If you want to add an LED to indicate when the button is pushed, the safest way is to connect it to a different pin & use code to turn the LED on/off as button is pressed. You might be able to put the LED in-line with the button, but it should have a current-limiting resistor. The pull-down resistor will need to be quite a bit larger than the current-limiting resistor for things to still work.

Thank you, I managed to solve with “flip flop” and with “pulse in change”. That was the only quick solution for me.

And now I have one more problem, which is not related to this, I would like the LED to turn on instantly and after a while to fade out when it goes off…

Odd that you would need the flip-flop. Tying the button directly to the led should do the same thing.

The easiest way to get a fade is to use the ‘fade’ node. Since you only want fade off and immediate on, there are two options:

  1. use output of button if it is on, or output of fade if button is off
    image
    The down-side of this option is that the ‘fade’ node is slowly ramping up to 1 while button is pressed. If button is released before ‘fade’ gets to one, the led will immediately drop down to dimmer value, then continue to fade off.

  2. change rate of fade based on whether button is pushed
    image
    If led turns off immediately on button release, it is because the new target value of 0 changes before the rate value changes. You can avoid this by putting a ‘defer’ node between button & fade-TARG to make sure RATE updates before TARG.

In this case, there is probably not much advantage of one option over the other (unless you want to avoid the “bug” of option 1; or it might be a desirable affect), but depending on the rest of the code, one might sometimes have a clear advantage, so both tricks are good to know.

Great, thank you very much. I think the first option will be good for me, although it is always good to know both options. It’s quite simple, but sometimes the idea just doesn’t come to mind. Thank you very much.

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