Electrical – Arduino/Atmega328: check if PIN is shorted to GND

arduinoatmega328p

In a project with an Atmega328 running at 8 MHz and powered from 3.3 V, I need to check if a 2-pin jumper is permanently inserted.

photo of 2-pin jumper example

So, the schematic is:

schematic diagram

and the board is:

PCB layout

I cannot figure it…

Is it really simple as:

1) Configure the PIN as INPUT_PULLUP?

2) Check if PIN is HIGH then is connected and do stuff?

3) Do I need external resistor (and what value) or is it sufficient the INTERNAL_PULLUP?

4) Can I leave the PIN shorted to GND for a massive amount of time, let's say for 1 year? This ATmega will be on "forever"

pseudocode, to understand:

void setup() {

  pinMode(PIN_CHECK , INPUT_PULLUP);

}

void loop() {

   // at first post, I did put this HIGH, but I was wrong.
   // the check need to be for LOW, when connected
   if ( PIN_CHECK == LOW ) {

     Serial.println("The user has placed the jumper, the pin is shorted to GND!");

   }


}

Thank you to all

Best Answer

Is it really simple as

Almost:

  • you need one code change (as kindly mentioned in comments by Peter Bennett and next-hack while I was writing this)

  • Atmel recommend (see link below) that you add at least one decoupling capacitor between Vcc and Gnd, and an RC combination on the Reset pin, to help prevent spurious resets when relying on the internal pull-up alone in electrically noisy environments (a classic trigger is nearby relays operating). If you decided to change the design so that the Reset pin is driven by anything external, then a diode from the Reset pin to Vcc is also recommended.

1) Configure the PIN as INPUT_PULLUP?

Yes, you can do this whether or not you also add an external pull-up resistor.

2) Check if PIN is HIGH then is connected and do stuff?

Almost. The pin state to check for, is the pin being LOW with the jumper installed, so your code needs to be changed for that.

3) Do I need external resistor (and what value) or is it sufficient the INTERNAL_PULLUP?

From experience, I would consider adding an external pull-up resistor for the jumper pin input (e.g. 10k), especially if anything causing an electric field (e.g. fingers, nearby relays etc.) could come close to it, as the internal pull-up resistors are relatively weak. Other people with different experience may disagree that this is needed.

If you choose not to add an external pull-up resistor on that input pin, then you must enable its internal pull-up resistor.

4) Can I leave the PIN shorted to GND for a massive amount of time, let's say for 1 year? This atmega will be on "forever"

Yes, that is fine. There will be some power consumed through the pull-up resistor (whether internal or external) and that jumper.

There are other techniques that you could use to reduce power consumption e.g. sleeping, then waking every 100ms, checking the pin state, then sleeping again, if power consumption is a concern.

See:

for more information about "minimal" ATmega circuits.