Please help tell the pin output between pinMode and digitalWrite.

Hi!

I would like to use Digital Pin to output 5.0V/0V.
But I find that it seems to take two steps to let a Digital Pin to generate the expected output.

Step 1. pinMode(pin, OUTPUT);
Step 2. digitalWrite(pin, HIGH);

Could you please tell me what the pin output is between Step 1 and Step 2?
Will the pin output be in an undefined or unexpected state between Step 1 and Step 2?

Thank you.

Best regards.

tbacarrot:
Could you please tell me what the pin output is between Step 1 and Step 2?
Will the pin output be in an undefined or unexpected state between Step 1 and Step 2?

It will be in a logic low (0V) state.

aarg:
It will be in a logic low (0V) state.

Thanks, aarg!
Could you please tell if there is a reference/spec to confirm it will be a LOW state?

tbacarrot:
Thanks, aarg!
Could you please tell if there is a reference/spec to confirm it will be a LOW state?

Yes, you can try it! Just put a delay in so you can see it!

But, you can download the AVR datasheet for the chip as well.

If you want the pin to be high when first configured as output, set it high first:

digitalWrite(pin,HIGH);
pinMode(pin,OUTPUT);

Step 1 only needs to be performed once, typically in setup(), to setup the hardware to drive the pin High or Low as needed late in loop() using digitalWrite.

@tbacarrot, look at page 68, figure 13.2, Ports as General Digital I/O. Flip flop PORTxn is cleared by RESET.

aarg:
@tbacarrot, look at page 68, figure 13.2, Ports as General Digital I/O. Flip flop PORTxn is cleared by RESET.

Great thanks, aarg. I will refer to that part.

aarg:
@tbacarrot, look at page 68, figure 13.2, Ports as General Digital I/O. Flip flop PORTxn is cleared by RESET.

Or page 76 or 80

Those pages describe the chip reset state, which need not be the same as what the early arduino codes sets them to. I don't think that the arduino code DOES document this at all, although it does leave them in the chip reset state, and of course you can look at the code.

Normally the first digitalwrite is so soon after the pinmode that it doesn't matter.

westfw:
Normally the first digitalwrite is so soon after the pinmode that it doesn't matter.

If you use Port Manipulation you can reduce the gap to a single clock cycle (62.5ns).

IIRC digitalWrite(pin, HIGH) when in INPUT mode changes the mode to INPUT_PULLUP and I don't know what happens when the pin is then changed to OUTPUT mode (too lazy to read the datasheet).

...R

westfw:
Those pages describe the chip reset state, which need not be the same as what the early arduino codes sets them to. I don't think that the arduino code DOES document this at all, although it does leave them in the chip reset state, and of course you can look at the code.

Normally the first digitalwrite is so soon after the pinmode that it doesn't matter.

I completely see your point, however it is hard to imagine a good reason why the initialization code would deliberately set them to a high state at startup.

They seem to have a hard time documenting everything that they do, let alone what they don't do. :wink:

aarg:
let alone what they don’t do. :wink:

A description of what a program cannot do (or is not intended to do) is often more useful than the info about what it can do.

…R