5
\$\begingroup\$

I'm using an Arduino Uno to control a small robot and I'm starting to add micro-switches as bump-sensors. I know that when I'm creating a circuit with LEDs I need to use resistors to limit the current so I don't mess up the Arduino, but do I need to do the same with the micro-switches? If so, how do I calculate the necessary resistance?

If it makes a difference, my micro-switch is Pololu #1405, Snap-Action Switch with 15.6mm Bump Lever 3-pin, SPDT, 5A. The Arduino outputs 5V and has a maximum of 40mA.

EDIT: I've done some more research and I found a page that seems to describe exactly what I want to do (http://arduino.cc/en/tutorial/button), don't know how I missed it before. So if I'm understanding the situation correctly, the only resistor I need is the pull-up/down one between the switch and ground, but none between the switch and the pin that's reading the switch.

\$\endgroup\$
1
9
\$\begingroup\$

You do not need a resistor, as long as you set up the pin to be input. The current protection resistors are necessary only in cases where the Arduino pin is used as a current source, i.e. output. If the pin is used as an output pin, you may need a current-limiting resistor, depending on what is connected to the pin. Usually we don't connect switches to outputs though (except for unusual cases when the switches need power, switches are actually circuits, etc).

For an input pin you may want a pull-up or a pull-down resistor. However, all AVR chips have built-in pull-ups. After you configured the pin as an input pin, you activate the pull-up resistor by digitalWrite(HIGH) to that pin, or, as @mpflaga points out, on the newer Arduino toolchain, you can set the input mode and pullup using pinMode(in_pin, INPUT_PULLUP); line. See here for more information on pull-up/pull-down resistors.

If you want a discrete pull-up resistor (i.e. not use the built-in one, though I don't know why you would do that) the circuit would look as follows:

schematic

simulate this circuit – Schematic created using CircuitLab

The pin will read high when the switch isn't pressed, and low when it is.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Note latest Arduino IDE has a pinmode command to make it more clear. pinMode(in_pin, INPUT_PULLUP); arduino.cc/en/Reference/PinMode \$\endgroup\$
    – mpflaga
    Mar 12 '13 at 19:48
  • 1
    \$\begingroup\$ Ah, cool. That's much clearer than the digitalWrite(..) version of that same. Thanks! \$\endgroup\$ Mar 12 '13 at 19:54
6
\$\begingroup\$

A closed switch is a dead short. Analyze your circuit by replacing all the switches with shorts. If all the currents are still within spec, then you're fine. If not, you're not.

Without a schematic or any hint of what exactly you are doing, this is about all that can be said.

\$\endgroup\$

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.