Default digital/analog pin state - input or output? (do unused pins need to be grounded to prevent floating inputs)

Hello all, brand new to Arduino and playing with an Uno, just a quick question I haven't been able to find an answer to: are all pins set to 'output' mode by default? I didn't want to leave any as floating inputs in case some were 'input' by default. The few example programs I've looked at only set the mode for one or two pins without addressing this for unused pins, so I'm guessing it's not a concern unless you specifically set a pin to 'input'?
Thank you

At power up or reset, pins are set to input by default.

So should I set all unused pins to 'output' mode so they don't need to be pulled high/low?

If you want to enable the internal pullup resistors you can do this in setup.

pinMode(myInputPin, INPUT_PULLUP);

Ok, but do I NEED to? For example, the "Blink" tutorial built into the Arduino IDE doesn't touch any of the unused pins:

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

So for an unused pin, if they're set to 'input' by default and I don't change them to output mode, is there a danger to the board to leave them as floating inputs not pulled high or low via connection to 5v/ground?

If you don’t need to worry about saving power, leave the unused pin as the default input mode without worrying about pulling them up.

Thank you. Saving power wasn't the concern, again I was worried about the dangers of floating inputs.

FYI, the microchip that manufactures AVR has announced as follows.

https://microchipdeveloper.com/8avr:ioports

Bottom of page

So it sounds like the best solution is either manually define all unused pins as inputs and connect them to 5v or ground via resistor, or set as output and leave unconnected. Correct?

99% of the time, I leave pins at their default input power up condition.

Only if the pin needs to be an output do I change the pin definition.


Don’t overthink this topic.
:thinking:

IMO; recommend define electrical potential (a.k.a. level) of all unused pins.
I also do that for all my projects.

Floating unused inputs of CMOS ICs (not Arduino) with built-in protection diodes is not allowed many cases in this forum, but allowed floating unused inputs of microcontrollers with built-in protection diodes.
I thought there is mysterious tendency.

EDIT:
This is not the case for uCs that can turn off the any pin's digital input buffer.
ATmega328P does this only for PORTC (has Analog function).

Yes, generally, all unused CMOS inputs should be tied to either VCC or GND .

You can do like this:

for(int i = 0; i<20; I++)
{
    pinMode(i, INPUT_PULLUP);  //DPin-0 to DPin-13; DPin-14 (A0) to DPin-19 (A5) are inputs but not floating
}

Ok but isn't this the default setting for pins? Shouldn't this be unnecessary?

At power up, all pins are input and without internal pull-up; so, they are at floating conditions. As you have wanted to see them not in the floating states, what else comfortable option you have without using external pull-up/pull-down?

The 'option I have' is to either set all unused pins to 'input_pullup', or to 'output' to guarantee none will be floating inputs.

The question is, if all pins are 'input' mode by default (and thus are floating inputs), and we agree that floating pins are bad, why does no one, including the tutorial/example programs provided built-into arduino's IDE, do anything with their unused pins?

Setting an unused pin to output if not needed is, in my opinion, dangerous. If you short the pin by accident, you can damage it.

If you don't want them to float, use INPUT_PULLUP. Else leave them.

ok, but still doesn't answer the question, isn't "leaving them" bad, since they'll be floating?

I think so, but it's probably ignored because most likely it won't cause any problems.
I don't like it, so I won't leave it as INPUT.
Because the GPIO of the AVR is internally connected to the CMOS buffer/Schmitt trigger for digital input.

EDIT:
Excerpt from the ATmega328P datasheet, the pads connected from the pins are connected to the schmitt-trigger input via the transmission gate.
(The transmission gate does disconnect the digital input circuit from the pad (and schmitt-triger input are tied to gnd) when uC goes into deep-sleep mode.)

If you make floating the input at bare schmitt-trigger IC will probably got warned in this forum.
But mysterious, no one says anything even if the schmitt-trigger input is floating "in the microcontroller".
I feel a double standard for this at forum.:expressionless:

In the final digital design, all unused input pins are terminated to solid ground.