Power Saving for pins only used during setup

Im currently using INPUT_PULLUP on 4 pins that are statically set with dip switches and only look at their values during the setup function to determine which part of the code they will run. Ive programmed multiple programs into the arduino and the dip switches tell it which to run when it powers on, they are never changed once set. This was so I could have one file and flash 15 different arduinos with the same code but have them do different tasks.
This in on a PCB and is in a case so there is no chance of the pins getting shorted.

Would there be any benefits at the end of setup() setting these pins to OUTPUT and LOW?
After the sampling of the pins and the section of code is selected to run would setting them all to a LOW output remove the current draw of keeping them INPUT_PULLUP shorted to ground or just INPUT_PULLUP depending on which Dip Switch if flipped.

setup()
{
  pinMode(A7, INPUT_PULLUP);    //Note: A7 will not work as in input, see below
  pinMode(A0, INPUT_PULLUP);
  pinMode(A1, INPUT_PULLUP);
  pinMode(A2, INPUT_PULLUP);
  byte Dip1 = digitalRead(A7);  //Note: A7 will not work as in input, see below
  byte Dip2 = digitalRead(A0);
  byte Dip3 = digitalRead(A1);
  byte Dip4 = digitalRead(A2);
  if(!Dip1 && !Dip2 && !Dip3 && !Dip4) {DeviceAddress = Address1;}  //0000
  if(!Dip1 && !Dip2 && !Dip3 &&  Dip4) {DeviceAddress = Address2;}  //0001
  if(!Dip1 &&  Dip2 && !Dip3 && !Dip4) {DeviceAddress = Address3;}  //0100
  if(!Dip1 &&  Dip2 && !Dip3 &&  Dip4) {DeviceAddress = Address4;}  //0101
  if(!Dip1 &&  Dip2 &&  Dip3 && !Dip4) {DeviceAddress = Address5;}  //0110
  if(!Dip1 &&  Dip2 &&  Dip3 &&  Dip4) {DeviceAddress = Address6;}  //0111

  pinMode(A7, OUTPUT);     //Note: A7 will not work as in output, see below
  digitalWrite(A7, LOW);   //Note: A7 will not work as in output, see below
  pinMode(A0, OUTPUT);
  digitalWrite(A0, LOW);
  pinMode(A1, OUTPUT);
  digitalWrite(A1, LOW);
  pinMode(A2, OUTPUT);
  digitalWrite(A2, LOW);
}

Ohm's law... the resistor PULLs UP because it's connected to Vdd. So a LOW consumes power, a HIGH does not.

OK so some (up to 4) of the dip switches may be LOW in which case they will be drawing current from the INPUT_PULLUP.
You could re-configure the inputs as INPUT (ie not _PULLUP) which will make them effectively open circuit - however that may not be desirable as subject to interference.
I wasnt ABSOLUTELY sure about this - so I did the experiment.
A digital output set LOW shows a voltage of less than 1mV and a current to ground of a few microamps on my multimeter.
So yes - set the pins to OUTPUT and LOW

Thanks for the input. Logically made sense to me.

If the pins never change while power is applied and are always tied to either VCC or Ground would INPUT make more sense then? Due to it being open. Does INPUT flow any amperage while sensing?

I would either have to change to solder pads or a two way dip switch due to not having VCC available on the PCB at the moment.

The focus is to remove the resistor that PULLUP uses.

You could avoid external pull ups by attaching a small value cap (guessing 100nF, say) to the pin to be tested and 0V.
Have your DIP switch pull to zero/0V, accross the cap.
Write a 1, high to the output to charge the cap, then quickly read back the same pin. If the switch is closed then the read back will be zero, as the switch will discharge the cap. If the switch is open the read back will be 1 (high) as the capacitor will hold the charge.

The power cost is just the momentary charging of the cap. I would add a low value series resistor, say 220 ohms, to avoid stressing the output of the micro.

void setup()
{
  const byte dipPin[4] = {A7, A0, A1, A2}; //I hope this is not a Nano?
  const ??? Address[16] = {Address1, Address2, ... };
  byte a=0;
  for(byte p=0; p<4; p++) {
    pinMode(dipPin[p], INPUT_PULLUP);
    if (digitalRead(dipPin[p]) == HIGH) bitSet(a, p);
    pinMode(dipPin[p], OUTPUT);
    digitalWrite(dipPin[p], LOW);
    }
  DeviceAddress = Address[a];
}

Which Arduino? This won't work on a Nano where A6,A7 is analogRead only.
Leo..

No its a Pro Mini but the board has nothing to do with the question.

johnerrington: You have confirmed my thoughts as it was so I'm safe to say ill just switch them to outputs and put them low.

stevemj: good thought however the PCB is already made and the extra components don't seem cost effective for a one time startup use.

PaulRB: I like the code, Did not know bitSet was a thing so ill have to look into that. It would actually help me out a lot to condense my data due to sending bytes that are either 1 or 0 and wasting the other 3 bits.

For any others who may find fault in setting them to an output while they could be tied to ground directly. I made a function: SetValue(Name, Value) All the Names are const bytes and the Dip Switch pin names are declared in the setup() and are not global to the code so there is no name for them to be accidentally triggered. The function also allows for me to set the Value to HIGH or a PWM and it looks to see if the pin can PWM if not it just sets it high.

ATmega328, so same story.
A6,A7 has no digital functions and no internal pull up.
Leo..

Thanks Leo, I missed that. No idea why the OP is using analog pins for digital inputs. Nor (from their first post) what board they are using!

Well then this is what I get for prototyping on a Mega then switching to a smaller footprint.
Thanks for the A7 error that I would run into.

I am using A6 as an analog input but that's the only analog signal on the board. I also have all but pin 13 dedicated to doing something productive due to not wanting to use the LED on the board.

I suppose I will have to use Pin 13 now that they can be output to ground on the dip switches and the LED will remain off. It was being used as a Serial Received flash for testing but I can remove it.

Thanks for the help and the corrections

Pin13 on a ProMini is also special.
Big boards (Uno, Mega) have that pin buffered with an opamp,
but small boards have the built-in LED directly connected to the pin (with a 330ohm resistor).
That could mean trouble if you're trying to use that pin as INPUT.
Leo..

Have you considered attaching 4 resistors to your dip switches and connecting them to a single analog input, like A7? That would save you 3 pins and make use of A7 which cannot be used as a digital input (as I hinted at in reply #6).

For example, use 10K, 4K7, 2K2, 1K connected in series. Connect the 4 dip switches to short out each of those 4 resistors. Connect another 4K7 to form a voltage divider with the other 4 resistors and connect the output of the divider to A7. Then use analogRead(A7) and with a little logic you can determine the setting of the dip switches.

Hope that makes sense. If not, will draw you a schematic.

If you can spare an output pin you could tie all the resistors to it, instead of 5V. Then after you take a reading, disable it by setting it HIGH so there is no current.

1 Like

Y'all have phenomenal ideas.

However, I think ill just nix the 4 dip switches and go with 3 at the moment. I was attempting to have the board have up to 16 addresses but I will have to settle with just 8 for now.

Wawa: Again you have put holes in my plan. Thanks for saving the frustration of an error when I would attempt to expand to the extra addresses. (Neither A7 or D13 as an input were part of my code that did anything as of yet)

PaulRB: Looks like an interesting idea, however it appears it would only sense the lowest resistance switch while I need to see if multiple switches are flipped. Possible doing resistors with each switch in parallel then to A7 with another resistor to ground would work. I would have to use the an output HIGH on another pin that would branch to the resistors and do math. (Possible for the future)

aarg: I like the pin as 5V for one pin to shut down however, the others would still need to be switched to output and low. This is why I was trying to sense to ground.

Going to put another hole in your plan.
You can detect multiple switch positions with an R2R resistor ladder.
Leo..

For a R2R network to work correctly, you need SPDT switches since the inputs must be either 0 or1.

Goose (Wawa), This is what I call a target rich environment.
Thank you for opening this idea to help simplify so much of my inputs

Sorry, I tried to explain, but a schematic is the best way. Will post that later when I have time.

Just to follow up and answer the hanging question....

The R2R array is set up and working for my project.
The resistor layout is such:

The analog pin feeds into a series of 1k resistors.
Before the first resistor there is a 1k pullup to 5v.
Between each 1k resistor I have 2k resistors to each pin that I use as input (shorting to Ground).

After some math the voltage per pin grounded and multiple pins grounded translate into a 0-255 analog read off the analog pin.
This supports up to 8 inputs (shorts to ground) to be used on a single analog pin.