Specifying pin as INPUT not necessary

For those interested in saving a little bit of precious memory, you can forgo the step of coding pinMode( pin, INPUT) if you are initializing the pin for the first time, you do not need this step as ATmel has set all pins to input by default. It doesn't save much memory only 8 bytes but if your pushing the limits with your code, this is one way to get some of that space back.

drewp63:
For those interested in saving a little bit of precious memory, you can forgo the step of coding pinMode( pin, INPUT) if you are initializing the pin for the first time, you do not need this step as ATmel has set all pins to input by default. It doesn't save much memory only 8 bytes but if your pushing the limits with your code, this is one way to get some of that space back.

Yes, Atmel guarantees that all I/O pins default to input pins upon reset or power up condition. However the bootloader and the arduino sketch initialization code do manipulate some pins so I'm not sure if you can assume that ALL I/O pins are in input mode as you sketch start-up function is called (pins 0,1,13 come to mind). Perhaps one of our software gurus can verify what assumptions one can make for ALL I/O pin modes when the set-up function is called?

However I would recommend that even if you don't explicitly have to change or use the mode command to use a pin for your sketch, that you still explicitly state what pins and mode you are using as comments in the start of your sketch, makes it much easier to understand later and troubleshoot initially.

Lefty

"what assumptions one can make "
I assume nothing, and define them all either as input with internal pullup enabled, or as output & driven either high or low.
Required for battery operation so the pins do not float & drift between levels consuming power.

It is hard to believe removing maximum 13 times pinMode( pin, INPUT) will make a noticeably difference to your program size.

Greeting from Belgium
Jantje

Not 13, but 20.
Do it as a for loop in setup, for example:

void setup(){
for (int x =2; x<20; x=x+1){ // declare pins 2 thru 19 as input
pinMode (x, INPUT);
digitalWrite (x, HIGH);
}
Serial.begin (9600); // takes care of pin 0, 1
}

Not 13 not 20 but 18 :stuck_out_tongue:
Or 1 and a loop like crossroad does

Best regards
Janje

Interesting...

Using 1.0, board set to Uno.

1: BareMinimum (blank setup and loop): 466 bytes.
2: Manually Setting Up 2-13 As INPUT: 728 bytes.
3: Loop to set pins 2-13 as INPUT: 652 bytes.
4: A single pinMode(2, INTPUT): 640 bytes.

Manual code:

void setup() {
  // put your setup code here, to run once:
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(9, INPUT);
  pinMode(10, INPUT);
  pinMode(11, INPUT);
  pinMode(12, INPUT);
  pinMode(13, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly: 
}

Loop code:

void setup() {
  // put your setup code here, to run once:
  for (int x=2; x<14; x++){ // declare pins 2 thru 13 as input
    pinMode (x, INPUT);
  }
}

void loop() {
  // put your main code here, to run repeatedly: 

}

@James
You did not test with the direct port manipulations - DDRB DDRD - you can set 8 pins at once ? - Arduino Reference - Arduino Reference

DDRB = 0;
DDRD = 0;

might be the smallest one :slight_smile:

In the spirit of the drewp63's desire to save memory, we should note that the numbers don't change if configuring all for inputs or outputs.

It's been my experience nearly every project has at least one output in which case, robtillaart is right. Direct port manipulation is ultimate in saving space:

robtillaart:
@James
You did not test with the direct port manipulations - DDRB DDRD - you can set 8 pins at once ? -
DDRB = 0;
DDRD = 0;

Rob is right. Direct port manipulation is the smallest - 474 bytes.

void setup() {
  // put your setup code here, to run once:
DDRB=0xEF;
DDRD=0xFE;
}

The code won't be as portable but you can configure all bits for input or output in only 8 bytes of overhead.

If declared as inputs (or left as defaulted), what's the direct port manipulation to turn on the internal pullups so the pins don't float?

Same as digitalWrite. Write a 1.

e.g.
PORTB = 1;
PORTD = 1;

what's the direct port manipulation to turn on the internal pullups so the pins don't float?

DDRD = B01111100; // set pin 0,1,7 as input, pin 2,3,4,5,6 as output
PORTD = B10000011; // set pullups high for the pins 0,1,7

or PORTD = ~DDRD; // maybe even simpler :wink:

Yes, but a fair test is not an empty sketch. Otherwise who cares how much memory it takes? Or what the pins are?

This sketch:

void setup() 
{
}

void loop() 
  {
  }

For me was 450 bytes.


And adding the pinModes certainly pushed it up:

void setup() 
 {
  for (int x=0; x< A5; x++)
    pinMode (x, INPUT);
 }

void loop() 
  {
  }

Now: 636 bytes.


But let's assume that we use pinMode somewhere in our sketch:

void setup() 
 {
 }

void loop() 
  {
  pinMode (5, OUTPUT);
  }

Now it's 624 bytes. So we only really "lost" 12 bytes.


We confirm that here:

void setup() 
 {
  for (int x=0; x< A5; x++)
    pinMode (x, INPUT);
 }

void loop() 
  {
  pinMode (5, OUTPUT);
  }

That was 644 bytes. Only 20 bytes more than not doing it.


So, you can set them to inputs if you want. That is, if you don't believe the datasheet. But if you don't believe the datasheet you are going to have trouble programming it anyway.

The only thing I have found inconsistent with the datasheet is that pin 13 seems to be left high (but input) which means the internal pull-ups are enabled. Perhaps the bootloader shouldn't do that.

(edit) This was with version 0022 of the IDE so the actual figures vary slightly from those above.