Understanding the need for two resistors with a switch

I'm following a tutorial that accompanied a starter kit by a company called Freenove. In this tutorial, we're attempting to read the state of a momentary button on a given Arduino pin. I understand the notion of a pull-up or push-down resistor, but it's not clear to me why they are using two resistors in this circuit.

The explanation given is as follows, but, as a beginner, I don't understand what they mean:

In the above circuit diagram, when the button is not pressed, 5V (high level) will be detected by UNO port; and 0V (low level) when the button is pressed. The role of Resistor R2 here is to prevent the port from being set to output high level by accident, which could be connected directly to the cathode and cause a short circuit when the button is pressed.

Can anyone help me interpret this? Thank you.

Sure. Imagine a scenario where that resistor isn’t there. Thing work fine. And then one day by accident you type the wrong pin number somewhere and that pin accidentally gets set to OUTPUT and HIGH in your code. Nobody notices at first but then you press your button and now that HIGH pin is connected directly to ground, a short circuit. Current flows freely, way over the rating of the pin because nothing to resist the flow. You suddenly notice something is wrong when smoke starts boiling out of your Arduino.

I’ll admit I’ve never got that resistor there. But it’s a good safety measure.

Makes perfect sense. Thank you for the clarification!

One PCB designer has made an Arduino that protects the PCB from the user.

Each I/O pin has these components:

https://andrew-sterian.squarespace.com/10-ways-to-destroy-an-arduino

I often just add a 220R series resistor.

Thanks for this link -- very informative.

1. When the given R1-R2-S1 network is connected with DPin-2 (input mode), we have the following scenario (Fig-1).
IOPin2.png
Figure-1:

(1) By accident/error, we may execute this code: pinMode(2, OUTPUT) in our sketch; DPin-2 becomes OUTPUT and is capable to source safe 20 mA current. We close S1, DPin-2 will supply a current of 5/10k = 0.5 mA current only; everything is safe.

2. When R2 is removed from the R1-R2-S1 network of DPin-2 (input mode), we have the following scenario (Fig-2).


Figure-2:

(1) By accident/error, we may execute this code: pinMode(2, OUTPUT) in our sketch; DPin-2 becomes OUTPUT. We close S1, DPin-2 is shorted to GND; as a result, huge current will flow which is enough to burn the internal driver circuit.

IOPin2.png

In theory the above is true but in reality

  1. Even direct short to GND is unlikely to damage the pin.
  2. If that happens you will at least know something bad is happening.
    I don’t think the resistor here is essential and I doubt many people use it.

Smajdalf:
In theory the above is true but in reality

  1. Even direct short to GND is unlikely to damage the pin.

Can you calculate short circuit power and current? After that check datasheets to see for how long the pin contact will tolerate this power dissipation. Will there be not a thermal runaway event to burn out the pin?

What happens when there occurs a short circuit in the power line -- we see tremendous fire? In electronic circuit, we come up with non-functional circuit/system due to accidental excessive overloading.

The short circuit current is said to be ~90mA so dissipation is about 450mW. The datasheet does not say how long the pin may survive this however thermal runaway should not happen in MOSFET driver (of course it can still be damaged).

The Arduino (ATMega) pins are surprisingly rugged.

Anyway I believe most people do not use the R2. The probability of turning the pin as output by mistake is low. If it happens there are other worse things - the button won't work which may range from minor inconvenience to serious damage (i.e. if it is a limit switch).

Let us be clear,

The protective resistor is only appropriate for experimentation - such as in a “starter kit”! It would clearly not be used for an actual application where the code is completed and there will be no “accidental” setting of the pin to OUTPUT.

It is also not appropriate for use where you use INPUT_PULLUP instead of an external pull-up resistor. However a value of 220 Ohms would limit the current to 22 mA or 1k would be just fine - just in case. :grinning: