user57742: With digitalWrite(pin_number,value), we can set an output pin as HIGH or LOW. But if only coding pinMode (pin_number, OUTPUT), what is the default state for the pin pin_number, LOW or HIGH? It is noted that: Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state. Does that mean the pin will be in the state LOW?

user69425: I've had a similar issue with an ATTINY85 powered DIGISPARK board.
I was getting a glitch when setting a pin to OUTPUT and the driving it HIGH immediately. Looked something like this:
CH1:
pinMode(ch1, OUTPUT);          // it is at this line that ch1 becomes LOW
digitalWrite(ch1, LOW);        // technically redundant since ch1 pin already driven LOW
CH2:
pinMode(ch2, OUTPUT);          // at this point ch2 goes LOW
digitalWrite(ch2, HIGH);       // and only goes HIGH once this instruction executes

The solution that mitigated the above glitch in my case (ATTiny85 / Digispark Board) was, as Edgar Bonet pointed out above, going through the INPUT_PULLUP phase:
pinMode(ch2, INPUT_PULLUP);
pinMode(ch2, OUTPUT);
digitalWrite(ch2, HIGH);

user7508: On the AVR-based Arduinos at least, pins start in the INPUT, i.e. high
impedance state. If you then pinMode(pin, OUTPUT);, the pin turns to
OUTPUT LOW. Note, however, that if you first set the pin to
INPUT_PULLUP, and then to OUTPUT, the pin ends up in the
OPUTPUT HIGH state.
Sometimes you want to control the state the pin will have right when
it's turned to OUTPUT. You may for example simulate an open collector
output by switching between INPUT (or INPUT_PULLUP) and OUTPUT
LOW, while avoiding the OUTPUT HIGH state. In this case, a safe
idiom is to set the output level before switching the pin to OUTPUT,
e.g.
// Set pin to OUTPUT LOW without going throught the OUTPUT HIGH state.
digitalWrite(pin, LOW); // do this first
pinMode(pin, OUTPUT);

A side effect of performing a digitalWrite() on a pin that is not set
to OUTPUT is that the pin gets switched to either INPUT (if writing
LOW) or INPUT_PULLUP (if writing HIGH). This is an idiosyncrasy of
the AVR IO ports you should probably not rely on if you want your code
to be readable or portable.
Reference: the source code of pinMode() in the Arduino
core, and the datasheet of the relevant MCU. See for example the section
Ports as General Digital I/O in the ATmega328P’s datasheet.

57742: <p>With <code>digitalWrite(pin_number,value)</code>, we can set an output pin as <code>HIGH</code> or <code>LOW</code>. But if only coding <code>pinMode (pin_number, OUTPUT)</code>, what is the default state for the pin <code>pin_number</code>, <code>LOW</code> or <code>HIGH</code>? It is noted that: <code>Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state</code>. Does that mean the pin will be in the state <code>LOW</code>?</p>
 69425: <p>I've had a similar issue with an ATTINY85 powered DIGISPARK board.</p>
<p>I was getting a glitch when setting a pin to <code>OUTPUT</code> and the driving it <code>HIGH</code> immediately. Looked something like this:</p>
<p><code>CH1:</code></p>
<p><code>pinMode(ch1, OUTPUT);          // it is at this line that ch1 becomes LOW</code></p>
<p><code>digitalWrite(ch1, LOW);        // technically redundant since ch1 pin already driven LOW</code></p>
<p><code>CH2:</code></p>
<p><code>pinMode(ch2, OUTPUT);          // at this point ch2 goes LOW</code></p>
<p><code>digitalWrite(ch2, HIGH);       // and only goes HIGH once this instruction executes</code></p>
<p><a href="https://i.stack.imgur.com/N6MY8.png" rel="noreferrer"><img src="https://i.stack.imgur.com/N6MY8.png" alt="Glitch on CH2 when setting PIN to OUTPUT and then turning it HIGH" /></a></p>
<p>The solution that mitigated the above glitch in my case (ATTiny85 / Digispark Board) was, as Edgar Bonet pointed out above, going through the INPUT_PULLUP phase:</p>
<p><code>pinMode(ch2, INPUT_PULLUP);</code></p>
<p><code>pinMode(ch2, OUTPUT);</code></p>
<p><code>digitalWrite(ch2, HIGH);</code></p>
 7508: <p>On the AVR-based Arduinos at least, pins start in the <code>INPUT</code>, i.e. high
impedance state. If you then <code>pinMode(pin, OUTPUT);</code>, the pin turns to
<code>OUTPUT</code> <code>LOW</code>. Note, however, that if you first set the pin to
<code>INPUT_PULLUP</code>, and <strong>then</strong> to <code>OUTPUT</code>, the pin ends up in the
<code>OPUTPUT</code> <code>HIGH</code> state.</p>

<p>Sometimes you want to control the state the pin will have right when
it's turned to <code>OUTPUT</code>. You may for example simulate an open collector
output by switching between <code>INPUT</code> (or <code>INPUT_PULLUP</code>) and <code>OUTPUT</code>
<code>LOW</code>, while avoiding the <code>OUTPUT</code> <code>HIGH</code> state. In this case, a safe
idiom is to set the output level <em>before</em> switching the pin to <code>OUTPUT</code>,
e.g.</p>

<pre class="lang-cpp prettyprint-override"><code>// Set pin to OUTPUT LOW without going throught the OUTPUT HIGH state.
digitalWrite(pin, LOW); // do this first
pinMode(pin, OUTPUT);
</code></pre>

<p>A side effect of performing a <code>digitalWrite()</code> on a pin that is not set
to <code>OUTPUT</code> is that the pin gets switched to either <code>INPUT</code> (if writing
<code>LOW</code>) or <code>INPUT_PULLUP</code> (if writing <code>HIGH</code>). This is an idiosyncrasy of
the AVR IO ports you should probably not rely on if you want your code
to be readable or portable.</p>

<p>Reference: <a href="https://github.com/arduino/ArduinoCore-avr/blob/1.6.23/cores/arduino/wiring_digital.c#L29" rel="noreferrer">the source code of <code>pinMode()</code></a> in the Arduino
core, and the datasheet of the relevant MCU. See for example the section
<a href="http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf#G3.1185755" rel="noreferrer">Ports as General Digital I/O</a> in the ATmega328P’s datasheet.</p>
