1
\$\begingroup\$

I want to connect a simple push button to a microcontroller and record the time I press the button.enter image description here Would this work or do I need to put a resistor in between?
If yes why?

\$\endgroup\$
  • \$\begingroup\$ The documentation for the part covers all of this. As an input you can certainly strap it up or down without a resistor. But to make the pushbutton work you want to have a pull up or pull down. The documentation for the part describes the I/O pins the pull up or down features if present (usually present) how to configure the pull up or pull down, etc. Also if the pin is a 5V or 3.3 and if 3.3 then is it 5v tolerant. the avrs for arduinos have historically been 5V parts not 3.3, but in general the 5V parts are being replaced by 3.3v (or lower) in new designs. \$\endgroup\$ – old_timer Nov 27 '19 at 19:40
5
\$\begingroup\$

schematic

simulate this circuit – Schematic created using CircuitLab

The above setup reads '1' when switch = off and '0' when switch = on

\$R\$ is an external pull-up resistor that avoids the floating of input in case the digital IO pin of the controller has no internal pull-up. In this case, it will pull the pin to 5V when switch = off.

Alternative setup:

schematic

simulate this circuit

The above setup reads '1' when switch = on and '0' when switch = off

\$R\$ is an external pull-down resistor here.

\$\endgroup\$
  • 1
    \$\begingroup\$ This would be a good answer if only you've changed 5V to 3.3V. Using 5V pin tolerance works, but is not a good design choice in general. Also, 10k for resistors works just as well but significantly reduces power consumption. \$\endgroup\$ – Maple Nov 27 '19 at 18:35
  • \$\begingroup\$ Mentioned 5V because Op is using Arduino Uno whose digital pins are tolerant up to 5.5V. \$\endgroup\$ – Meenie Leis Nov 27 '19 at 18:42
  • 1
    \$\begingroup\$ Yes, that is exactly why I said: "Using 5V pin tolerance works, but is not a good design choice" \$\endgroup\$ – Maple Nov 27 '19 at 18:43
  • 2
    \$\begingroup\$ @Maple The Arduino Uno microcontroller is powered from 5V and the input high voltage range for I/O is 3.0V - 5.5V according to the datasheet. Nominally, the input high voltage would be equal to the supply voltage which is 5V. Note that not all of the controller's pins have 3.3V within their input high range (even though all of the I/O pins do), so I don't think it's a good idea to assume that 3.3V inputs are generally valid for a chip powered at 5V. \$\endgroup\$ – user253751 Nov 28 '19 at 11:46
  • \$\begingroup\$ @user253751 You are correct. Since Uno uses ATmega328P which operates at full speed at 3V and above I assumed that's what was used for VCC. However after checking the schematics I realized that the designers opted for 5V supply. In which case this answer is perfectly fine. \$\endgroup\$ – Maple Nov 28 '19 at 19:47
1
\$\begingroup\$

With a little bit of googling, you find myriad of tutorial on this topic, like this "official" one: https://www.arduino.cc/en/tutorial/button

EDIT


The resistor is used to "park" the input reading to a default level when the button is released. If implemented as pull-down, it parks the input low (read = 0) and high (read = 1) when in a pull-up configuration. Without the resistor, the input is left floating which can lead to undesired behaviors if noise couples into the input (technically won't happen to you if you're just implementing a push-button circuit but on very crowded circuit application, noise sources won't come missing).


To go further and if your application need to accurately measure the time of "press", I would recommend searching for button "debounce" which can be implemented either in hardware or software. You could look here to start with: http://dduino.blogspot.com/2012/03/arduino-button-debouncing.html

\$\endgroup\$
  • 2
    \$\begingroup\$ "The reason you need the series resistor is to avoid huge inrush current into the GPIO pin when the button is pressed." From where? There's nothing drawing current. The only purpose a series resistor would have is to prevent ESD damage. ESD = very high voltage, very low current. \$\endgroup\$ – Lundin Nov 28 '19 at 7:27
  • 1
    \$\begingroup\$ Yes, that part is BS. The resistor isn't even in series with the GPIO pin and the button. \$\endgroup\$ – user253751 Nov 28 '19 at 11:47
  • 1
    \$\begingroup\$ Ok technically you guys are right, I was actually thinking of a case where the GPIO is mis-configured to output and set to, for example, low and button is used to pull-up the GPIO pin to VDD. In this case a series resistor would prevent against a straight short to ground. I will correct my answer, thanks for the feedback :) \$\endgroup\$ – Cisco25 Nov 28 '19 at 12:01
  • 2
    \$\begingroup\$ @Lundin Actually, if implemented as pull-up or pull-down, the resistor won't act a an ESD suppressor at all. For that you need to use an ESD diode. If in series, it will help with reducing inrush current which can damage internal protection diodes, but you still need to suppress the high-voltage part, an RC with a 10 or 100nF cap could do (and will help with the bouncing part) or an ESD diode is still fine too. \$\endgroup\$ – Cisco25 Nov 28 '19 at 12:18

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.