Automatically making unused pins INPUT_PULLUP?

Is there a command to put in a sketch to automatically make all undefined pins INPUT_PULLUP to protect them? Or do you have to manually enter in each and every unused pin?

1) Why would you want to do that? It doesn't matter if you don't use them. 2) Simplest would be to loop over all pins first and make them INPUT_PULLUP and after that set the used pins to what you want?

seanspotatobusiness: Is there a command to put in a sketch to automatically make all undefined pins INPUT_PULLUP to protect them? Or do you have to manually enter in each and every unused pin?

Unused pins are INPUT by default.

Why do you think, that INPUT_PULLUP might "protect" them in any way?

jurs: Unused pins are INPUT by default.

Why do you think, that INPUT_PULLUP might "protect" them in any way?

I read it somewhere.

Now you know it's bull ;)

septillion: Now you know it's bull ;)

According to what I read it's because floating pins cause extra power consumption. So for battery-powered projects it's best to set the unused digital pins to INPUT_PULLUP.

Is this true or it's another myth?

According to what I read

Where did you read this ?

ATmega328/328P datasheet:

18.2.6. Unconnected Pins

If some pins are unused, it is recommended to ensure that these pins have a defined level. Even though most of the digital inputs are disabled in the deep sleep modes as described above, floating inputs should be avoided to reduce current consumption in all other modes where the digital inputs are enabled (Reset, Active mode and Idle mode).

The simplest method to ensure a defined level of an unused pin, is to enable the internal pull-up. In this case, the pull-up will be disabled during reset. If low power consumption during reset is important, it is recommended to use an external pull-up or pull-down. Connecting unused pins directly to VCC or GND is not recommended, since this may cause excessive currents if the pin is accidentally configured as an output.

It’s not bull. Open inputs float and hence cause transitions from high to low and vice versa. Totempoles will be shorts for a very short duration during these transitions due to different propagation delays causing increase in current consumption.

The original statement still is bull ;) Aka, it does not protect but it does save energy.

seanspotatobusiness: Is there a command to put in a sketch to automatically make all undefined pins INPUT_PULLUP to protect them?

AVR specific for processors with 1 to 4 ports. Must be the very first thing in setup. Sets ALL pins to pullup enabled. You must use pinMode( X, INPUT ) for pins that should not have the pullup enabled. pinMode( X, OUTPUT ) will set the output HIGH. Untested.

You are better served just using pinMode.

#if defined( PORTA )
  PORTA = 0xFF
#endif
#if defined( PORTB )
  PORTB = 0xFF
#endif
#if defined( PORTC )
  PORTC = 0xFF
#endif
#if defined( PORTD )
  PORTD = 0xFF
#endif

septillion: 1) Why would you want to do that? It doesn't matter if you don't use them.

If you want micro-power operation (a few µA in sleep or low clock speeds) you have to do this to prevent many mA of dissipation with floating inputs oscillating or sitting mid-rail.

2) Simplest would be to loop over all pins first and make them INPUT_PULLUP and after that set the used pins to what you want?

For the UNO the quickest/dirtiest is direct port manipulation:

void setup()
{
  PORTB = 0x3F ; // pins 8..13
  PORTC = 0x3F ; // pins A0..A5
  PORTD = 0xFC ; // pins 2..7
  ...

Note this avoids the serial pins, D0/D1, since they may be being used, but if they aren't simply change 0xFC to 0xFF.

If you did this:

void setup()
{
  for(byte q = 2;q <= 19;q++)
    pinMode(q,INPUT_PULLUP);
  pinMode(7,OUTPUT);
}

Would pin 7 be set HIGH?

Yes, pinMode(pin, OUTPUT) only sets direction, not state.

septillion: The original statement still is bull ;) Aka, it does not protect but it does save energy.

It also prevents unused pins from capacitively coupling the state of used inputs in close proximity.

Perehama: It also prevents unused pins from capacitively coupling the state of used inputs in close proximity.

I'd not necessarily assume that, given the internal pull-ups are weak, 30k or so. Even 0.1pF of capacitance to a logic signal changing 5V in 5ns will induce 0.1mA of crosstalk...

MarkT: I'd not necessarily assume that, given the internal pull-ups are weak, 30k or so. Even 0.1pF of capacitance to a logic signal changing 5V in 5ns will induce 0.1mA of crosstalk...

Let me correct myself. Pulling the unused pins into one state, ideally with external pull up or down resistors, may prevent those pins from capacitively coupling the state of used inputs.