I want to use Arduino Uno or a Nano which uses ATMEGA328P as micro-controller.

D2 is now set as INPUT. During the start up I want the MCU to detect whether pin D2 is HIGH or LOW depending on the setting set by the user. The setting will be done by an SPDT switch as shown below:

enter image description here

So if the switch pole is coupled to the +5V the state of D2 will be HIGH and if coupled to the GND the state will be LOW. Depending on this state in void main(), the program will perform a specific task.

I'm just not 100% sure whether it is very fine to connect pins that way. Is there any need for a pull-down resistor between the switch and the GND or between the switch and the +5V pin?

(What if the worst case D2 is accidentally set as OUTPUT pin?)


3 Answers 3


Assuming the MCU pin will be damaged/destroyed if configured as Output, when externally forced to some fixed level, yes, you should insert 10Kohm resistor in the pin's PCB trace.

Do not configure the pin with any on-chip pullup or pulldown current.


As a quick-and-cheerful experiment there isn’t anything inherently wrong with this really, but it could be improved.

Out of reset the MCU will set the pin as an input. The worst that can happen is if you make a programming error and change the port function or direction such that it's an output. The resulting current is limited by the Rds(on) of the port pin driver so nothing bad will happen, no cratered IC, no white smoke. If you’re paranoid, add a series resistor.

It is questionable practice to have a floating signal. With no pull-up/pull down, the signal will float while the switch moves from one position to another. It’s vulnerable to noise pickup during that time, for example, from your finger as it touches the switch. So you should program the MCU to have a pull-up or pull down.

Hacktastical idea: Add a small capacitor on it (100pF, say) to the input also. Then you get a debounce action too as well as reducing the impedance of the input to improve noise immunity.

Finally, in a real product there is one other issue: ESD immunity. Switches are an entry point for static discharges. A TVS diode on the line next to the switch would be prudent. If the switch has a housing, ground that too.

  • \$\begingroup\$ Capacitor without resistance will not debounce, just add LC ringing every switch period and make it harder on the switch terminals due to inrush current. \$\endgroup\$
    – MadHatter
    Jul 19, 2019 at 0:59
  • \$\begingroup\$ Without pullup/pulldown, it will hold the voltage during the break-to-make time, then get charged or discharged on the first 'bounce' and so ignore successive bounces. We can get away with this because of how the switch is wired. As for inrush, we're talking 100pf here - not so much of an issue. With pullup/pulldown it will act as part of an R-C for a regular cheesy debounce. \$\endgroup\$ Jul 19, 2019 at 1:06

Does your switch break-before-make or make-before-break? If the latter, a resistor is needed to prevent a short from 5V to ground. The resistor should be between the switch and 5V, or between the switch and ground.

Setting D2 as an output can cause that output to source/sink very large currents. This may cause the processor to behave incorrectly, or in extreme cases, to be permanently damaged. In general, you should avoid this possibility either in software or by adding a resistor in series with D2 (between the switch and D2).

  • \$\begingroup\$ The type of switch can be seen here: docs-emea.rs-online.com/webdocs/1587/0900766b8158761d.pdf At the last page bottom. Is that break-before-break? \$\endgroup\$
    – user1245
    Jul 18, 2019 at 12:49
  • \$\begingroup\$ This is actually a 3-position switch, with the center position specified as having no connection. So you are safe, this is a break-before-make and there is no risk of shorting power to ground. However, when the switch is at its center position the voltage at the input pin is undefined, so you might want a resistor to ground or 5V (whatever is the default situation) anyway. \$\endgroup\$ Jul 18, 2019 at 13:03

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.