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);
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.