10
\$\begingroup\$

I am new to "electrical engineering" and I am working on a little arduino project. I want to have a SPST switch be used as a digital input on the board.

I have one of the switch leads connected to posative and the other connected to the digital input. The problem with this is that when the switch it off I can't be sure the input will be grounded. How can I make sure the input is grounded when the switch is off?

Would it work if I connected the digital input to a resistor that is connected to ground so when the switch is off it will be grounded, but when the switch is on it wont short out -- would that work?

\$\endgroup\$

3 Answers 3

9
\$\begingroup\$

The most conventional solution would be to connect one side of the switch to ground. Connect the other to the digital input, and also to a resistor between 1 and 10 K ohms going to the positive supply.

Going the other way, with a pull down resistor as Bruno describes, is possible but less preferred. Many inputs already have a degree of implicit pull-up and will read a '1' if unconnected, though not quite reliably. But if your switch is already connected to the positive rail, then a pull down is an okay solution, though many prefer to use a small resistor when connecting logic inputs to the positive rail.

Many microcontrollers also have internal pullup and/or pull down resistors on GPIO pins which can be enabled by writing to a configuration register. If you are driving an input of such a microcontroller you might not need an external pull up/down resistor at all, though not every microcontroller features these.

\$\endgroup\$
5
  • 4
    \$\begingroup\$ Also, if you are using a uC with built in pull ups or pull downs, it's good practice to enable them on pins that aren't being used to keep them in a known state. \$\endgroup\$
    – Matt Young
    Commented Aug 16, 2012 at 18:55
  • \$\begingroup\$ It's actually easier, for my project to connect the switch to ground. So I will try use a pull up resistor. Thanks! \$\endgroup\$
    – Sponge Bob
    Commented Aug 16, 2012 at 19:05
  • 2
    \$\begingroup\$ Great! Come back when you want to ask us about "switch bounce" :) \$\endgroup\$
    – gbarry
    Commented Aug 16, 2012 at 21:15
  • \$\begingroup\$ Turns out Arduino will do pullup automatically. I'm not sure how, but in the code there is a Digital "INPUT_PULLUP" and I don't even need a resistor. That was easy. Also, about the switch bounce, I noticed it when I was monitoring the serial output. So I just added a 3ms delay in the code. It saved me another trip to the store. :) \$\endgroup\$
    – Sponge Bob
    Commented Aug 17, 2012 at 16:17
  • \$\begingroup\$ If you wade through the ATMEGA data sheet you will see there is a bit that can be written in a configuration register to enable the pullup. The Arduino libraries are just making it more convenient. For the bounce, ignoring (that) input for a while is a usual course, though 3ms might be a bit short. If the delay blocks your whole system you may need to keep it short, if it only ignores that input than you can make it a bit longer since intentional re-activations would be still slower. \$\endgroup\$ Commented Aug 17, 2012 at 16:38
9
\$\begingroup\$

Yes it would work as you described. That is called a pull-down resistor because it assures that when the contact is open the digital input is at the logic state 0 (low). Usually you can use a 10 KΩ resistor for this purpose.

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

That will work, and will give you positive logic: a high level (logic "1") when the switch is closed.

But like Chris says the inverted thing is more common: switch connected to ground, and a pull-up (instead of pull-down) resistor to the power supply. Your logic will be inverted: a logic "1" will correspond with an open switch.

A good reason for the pull-up version is that most microcontrollers have them integrated, and you can enable/disable them depending on your needs. Some microcontrollers also have configurable pull-downs, but these are less common.

If you want an external pull-up 10 kΩ may be a good value. A microcontroller's input can have a leakage current of up to 1 µA, and then 10 kΩ will drop a negligible 10 mV. Lower values are certainly possible, but keep in mind that they will have a larger current to ground when the switch is closed. A 1 kΩ resistor will draw 5 mA at 5 V supply, which is a waste of power really. For the 10 kΩ that's only 500 µA. For very low-power applications you may increase the value to 100 kΩ, but remember the leakage current; 1 µA will give a 100 mV drop!

\$\endgroup\$

Your Answer

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

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