Electronic – AVR switch best practice

avrbasicresistorsswitches

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..

Best Answer

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.