InItializing Outputs

From the Blink example sketch:

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

I see this all the time, the initialized output state is not set before the output is enabled. The code should be:

void setup() {
// initialize digital pin LED_BUILTIN as an output.
digitalWrite(LED_BUILTIN, LOW); // set the initial output state, turn the LED off
pinMode(LED_BUILTIN, OUTPUT); // now enable the output
}

In the original code the initial output is dependent on the default value in the hardware output register. Its value is not guaranteed to be 0 or 1. To insure the proper initial state the state must be set to the appropriate value before the output is enabled.

For an active low output the following code will glitch the output if the default state is 0, even if the hardware as a pullup resistor to keep the output HIGH.

pinMode(LED_BUILTIN, OUTPUT); // if the initial state is 0, the output will go LOW
digitalWrite(LED_BUILTIN, HIGH); // set the output state inactive, HIGH

should be!!!:

digitalWrite(LED_BUILTIN, HIGH); // set the output state inactive, HIGH
pinMode(LED_BUILTIN, OUTPUT);

This will set the output HIGH without a glitch.

digitalWrite(LED_BUILTIN, LOW);
digitalWrite(LED_BUILTIN, HIGH);
Just turns the internal pull up ('resistor') on/off.

digitalWrite(LED_BUILTIN, HIGH); // old way
is the same as
digitalWrite(LED_BUILTIN, INPUT_PULLUP); // newer preferred way

Connect a 220ohm load from pin to ground, and see if it's still HIGH.
Leo..

rki009:
should be!!!:

No.

The way a HIGH is interpreted depends on whether the pin is set for INPUT or OUTPUT

...R

LED_BUILTIN is a bad example pin, otherwise the suggested code is correct.

Wawa:
Connect a 220ohm load from pin to ground, and see if it's still HIGH.

A load should be connected only to output pins.

Robin2:
The way a HIGH is interpreted depends on whether the pin is set for INPUT or OUTPUT

The interpretation of HIGH also depends on the controller type. Portable code should use INPUT or INPUT_PULLUP, not write to input pins.

DrDiettrich:
LED_BUILTIN is a bad example pin, otherwise the suggested code is correct.

A load should be connected only to output pins.

I always use 13, but I think it's used for portability. Not sure which boards/processors.

Not sure why you say that. We even connect heavy loads, like zero ohm, to input pins with pull up enabled.
Leo..

Pin 13 has attached a circuit for driving the LED, so that it behaves differently from other pins.

A button is a button (for input), not a load (for output).

DrDiettrich:
Pin 13 has attached a circuit for driving the LED, so that it behaves differently from other pins.

Not on a UNO where it is buffered by the op-amp.

rki009:
I see this all the time, the initialised output state is not set before the output is enabled. The code should be:

No, it is initialised by RESET. Registers are zeroed, which means the Direction Register is set to INPUT and the output register set to LOW.

You do need to write the output HIGH first (which sets it to INPUT_PULLUP) if you want it not to briefly pull something LOW when you set pinMode to OUTPUT.