3
\$\begingroup\$

This sounds really very easy, but I'm afraid to try it because I don't want to fry my microcontroller.

I'll use an Arduino for an example, but it should be the same for all microcontrollers.

Anyway, say you have a switch and you want for when you press the switch to bring a digital pin to high(1) and when it's not pressed, to low(0). Would this require resistors? Or would you just straight wire from 3.3v into the input pin?

Or to further simplify what I'm asking. If for some reason you wanted a certain digital input pin to always be high, would you just stick a wire from 3.3v into the digital pin? Or would you need a resistor somewhere in there?

\$\endgroup\$
1
  • \$\begingroup\$ It's not the same for all microcontrollers; many have weak internal pullup or pulldown resistors that can be used for this purpose. \$\endgroup\$ Apr 2, 2011 at 2:16

3 Answers 3

3
\$\begingroup\$

To get at your question about if you can plug power directly to the pin with no resistor and have it always be high... yes, that does work.

When you have your microcontroller set to input mode you can treat the pin like there is a very large resistor inside. So using ohms law, V/R=I with V=3.3 and R=very large, lets say 10megohm gives you a current of 0.33 micro amps. The inputs are designed to act like such a high impedance so that it has a very small effect on anything externally.

\$\endgroup\$
3
  • \$\begingroup\$ What if for some reason during internal initialization of the microcontroller or whatever, that the pin is briefly set to output mode. If it output 0, this would effectively short through the microcontroller and thus break something correct? What protection can I provide against that happening \$\endgroup\$
    – Earlz
    Apr 2, 2011 at 2:52
  • \$\begingroup\$ This may depend on the micro but I believe most default to input on startup. If you want to be careful just add a resistor between the pin and power. Just have to make sure it doesn't have an adverse effect on anything it is connected to. \$\endgroup\$
    – Kellenjb
    Apr 2, 2011 at 3:01
  • \$\begingroup\$ @Earlz - That's why all microcontrollers start with their I/O pins set to input upon reset. \$\endgroup\$
    – stevenvh
    Apr 2, 2012 at 12:33
3
\$\begingroup\$

A microcontroller's pin when in digital input mode is usually many hundreds of kilo-Ohms or even a few megaohms. They are essentially op-amp (CMOS nowadays) high impedance inputs.

The problem when a pin is set to output is that the output drivers are often quite low resistance - tens of ohms or less. They can burn out if shorted to VCC or GND, depending on the state. It seems you are most worried about this situation, with a button/switch interface.

The standard way to do a switch for a digital input is to put the switch on the low side with a high value resistor externally Like this:

schematic

simulate this circuit – Schematic created using CircuitLab

The resistor R1 helps in two ways - if the microcontroller somehow accidentally turns it's pin into an "output" mode, and goes active LOW, this resistor ensures there is no short circuit to GROUND through the pin. When a digital pin goes active low, it's basically a short to ground which is very bad for a pin which is connected to a high power/current capable source.

The second function of R1 is when the button is pressed, it prevents high current flowing through and destroying the switch.

You can't really use the internal pull up resistors in the microcontroller for this set up because if R1 doesn't exist, the switch will fail from short circuit current through it.

The only annoying thing about this whole set up is that it is "active low", as in, the logic value when the button is pressed is "LOW", and when the button is not being pressed, it sits at "HIGH".

Your other option is for the switch to be on the high side, connecting VCC to the digital pin. You can then use internal(very rare) or external pull-down resistors. This is good because the switch can't be externally grounded, but can still have problems if you had the microcontroller pin accidentally set to output and had it set to LOW (which is an almost direct connection to Ground as I mentioned earlier).

schematic

simulate this circuit

To explain the above diagram, Rext is the location of an external resistor if you decided to do this. This ensures when the button is not pressed, the digital input node voltage will decay to zero through this resistor. The Rint is the possible internal resistor if your microcontroller provides one - these are often in the range of 20-40k Ohms. In case of setting the pin to OUTPUT and LOW, the Rprot is there to serve as a short-circuit protection resistor - honestly all general purpose microcontroller pins should be given an external series resistor like this, to make them more rugged.

I myself use the first diagram, with R1 between the pin and the switch, to VCC. This covers all bases and protects against crazy things. I just accommodate the logic level being LOW when active in my software.

\$\endgroup\$
2
\$\begingroup\$

You need to use either a pullup to Vdd or a pulldown to GND. If you don't, when you press your switch, you will effectively short Vdd to GND.

EDIT -- I should have drawn a picture, but instead found a site with an explanation.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Would you require a pullup/down resistor if you took the button out of the equation and just wanted a digital pin to always be high? \$\endgroup\$
    – Earlz
    Apr 2, 2011 at 2:49
  • \$\begingroup\$ Nope, you can pretty safely tie VCC to a pin if it is configured as an input. Most uC's have high input resistance. \$\endgroup\$
    – AngryEE
    Apr 2, 2011 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.