1
\$\begingroup\$

After reading a lot of posts (here and on the web), I am a bit confused.
All I want to do is to get the best practice of push button connection to AVR.

I have 2 "sections" to my question. the first one is the HIGH or LOW connection, and the 2nd one is the resistor issue. I will start with the HIGH/LOW options:

  1. +5V --\ ----AVR : The AVR pin is set to LOW by default and checked if the pin is HIGH (so the switch is on).
  2. GND --\ ----AVR : The AVR pin is set to HIGH (Pull-up?), checking it for LOW to see click.

The 2nd section of the question is about the resistor, and if I need to use it.

  • If I am using the pin with HIGH value and by pushing the button it connected to groud,
    I saw some of the blogers put resistor before the button and connecting it to VCC:
    VCC_RES
    Do I need it? if so, what should be R1 value?

  • If I am setting the pin to LOW as default, should I use resistor as well, but connect it to GND?

Thanks guys..

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

It doesn't really matter if you implement it as low or high active switch. However, since you need either a pull-up or pull-down resistor anyway (to have a defined logic level when the button/switch is open) it may be worth noting that the AVR controllers IO pins have a integrated pull-up resistor (of about 10k Ohm) that could be activated/used.

If I am using the pin with HIGH value and by pushing the button it connected to groud, I saw some of the blogers put resistor before the button and connecting it to VCC:

That resistor actually acts a pull-up resistor. It is not necessary to limit the current from/to the IO port through the closed switch to GND, because it has a very high impedance when configured as an input.

what should be R1 value?

Since transmission speed and edge times and not an issue for something "slow" like pushing a button, the main consideration is to limit the current to a sensible value. With something around 50k Ohm you would limit the current to 0.1mA at 5V, which should be fine. You don't want a pull-up that is to "weak". Imagine using 1 Giga Ohm. That would essentially be the same as leaving the pull-up out and would not properly bias the input against the respective level.

If I am setting the pin to LOW as default, should I use resistor as well, but connect it to GND?

Yes, that would be your pull-down resistor.

\$\endgroup\$
4
  • \$\begingroup\$ Thanks for the detailed answer, So just to clarify - If I am using AVR (which has internal pull-up) I can drop the external resistor? \$\endgroup\$
    – gabi
    Commented Jul 27, 2015 at 12:42
  • 2
    \$\begingroup\$ @gabi Yes. You can activate the internal pull-up by setting the pin as INPUT and setting it HIGH. Something like this(with your port/pin values ofcourse): DDRA &= ~(1<<PINA0); PORTA |= (1<<PINA0); \$\endgroup\$
    – Golaž
    Commented Jul 27, 2015 at 12:57
  • \$\begingroup\$ Is it ok (in terms of efficiency) to connect permanent on/off toggle to pulled up INPUT pin ? The issue is that the pin will be HIGH and connected to GND permanently. will it be better to use 10K resistor between the GND and the toggle? Example: GND--10K--/-AVR \$\endgroup\$
    – gabi
    Commented Jul 27, 2015 at 13:50
  • 1
    \$\begingroup\$ @gabi: When the switch is closed, the current will already be limited by the pull-up resistor. There is (nearly) no current sourced from the IO pin when it is configured as an input. Be careful though, that if you would accidentally set the pin as output and close the switch, you could damage the controller. \$\endgroup\$
    – Rev
    Commented Jul 27, 2015 at 14:04

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.