Arduino Pushbutton methods

Hi All,

Quick question, i'm looking at pushbuttons integration with current projects. Ive noticed there are two (2) different methods to people have been using.

  1. https://docs.arduino.cc/built-in-examples/digital/Button
    image

or
2. https://create.arduino.cc/projecthub/baqwas/push-nano-s-button-395872
image

What is the reasons for the different methods, or are they just simply alternative methods for the same outcome?

Cheers,

Nic

Hello Nic,
I think you will find the answer here Buttons and other electro-mechanical inputs (introduction)

the conventional approach is to connect the button between the pin and ground, configure the pin as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and for the button to pull the pin LOW.

awesome, thanks heaps!

Note: Your second diagram is wrong. The pull-up resistor goes between the pin and +5V, not between the pin and button switch. The external pull-up can be replaced with the internal pull-up.

An INPUT pin connected to a switch must have a pull-up or pull-down resistor or the value will float randomly between LOW and HIGH when the switch is open.

but not if you specify INPUT_PULLUP for the PIN, as its pulled high untill button pressed?

Do you still need a 10k or so resistor between to limit AMPs between PIN and GND when button is pressed or does INPUT_PULLUP resistor help there?

Cheers

The input pullup resistor limits the current, you don't need the external resistor at all. You can consider them to be around 50k Ohms, although the value is no accurately specified.

Hello
Take a view to get some ideas how to integrate pushbuttons:

Many thanks to LarryD :wave:

You only need a current-limiting resistor between the pin and a load if the pin is set to OUTPUT. For a pin that is INPUT or INPUT_PULLUP you can connect it to Ground or +5V without a problem. If you fear that a bad sketch will set your pin to OUTPUT you can add a current-limiting resistor but 10k (0.5 mA) is excessively high resistance and may cause input problems. Use more like 240 Ohms. That will limit the current to a safe 21 mA and still allow your INPUT_PULLUP pin to work reliably.

but with the extra 10k resistor, the pin may only be pulled down to a couple volts, not groud

awesome thanks for the info, just checked the PIN guidelines for the ESP32 and PINs 34/35 i'm going to use dont have internal pullup.

Based on what you said, and setting the PIN to INPUT + 240ohm resistor should still do the trick? Below as an example?

cheers,

why such a small resistor? 13.7 ma (3.3V / 240Ω). why not 10k?

ok so a 10k resistor to 'act' as an internal pullup and because PIN 34 and 35 are set as anINPUT when the button is pressed the PINs wont draw excessive mA?

In using a switch or push button to provide a digital input to the Arduino we basically have two options as shown here; to connect one side of the switch or button to the ground 0V rail - or to connect it to the +Vcc rail. The usual (MUCH better) choice is to use a ground connected "normally open" button or switch. This has many advantages - but the "active" (pressed) condition of the button results in a logic "0" and inactive or released condition gives a "1".

It explains why using method 1 is potentially unsafe, and the design choices in choosing input circuits.

You may also want to look at this if switch bounce will be an issue.

1 Like

awesome thanks for that, based on the second link I'm using Bounce2 library and and testing with stable interval or bounce-lockout method of coding.

Seems pretty good so far

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