Intial Pin state when setting Pin to Output

I am working on a project that will operate a Garage Door using a relay board. The relay board inputs are active Low meaning that 0v will operate the relay and 5V will do nothing. In the setup part of my code I have:

pinMode(doorActivateRelayPin    , OUTPUT); // set door relay pin as Output
digitalWrite(doorActivateRelayPin , HIGH  ); // Active low so default to 5v

I need to be sure that the output is never at 0v during startup as this could open my garage door. My question is: What is the intial state of a pin when set to OUTPUT? Is it 0v or floating until set to HIGH? I know there is only going to be a few clock cycles between the the two lines of code so the risk of the relay operating is pretty low. I'd just like to know if there is a clean and safe way of ensuring that the output pin is floating or at 5v after startup and never at 0v.

Can anyone help?

GraemeP

graemep: I am working on a project that will operate a Garage Door using a relay board. The relay board inputs are active Low meaning that 0v will operate the relay and 5V will do nothing. In the setup part of my code I have:

pinMode(doorActivateRelayPin    , OUTPUT); // set door relay pin as Output
digitalWrite(doorActivateRelayPin , HIGH  ); // Active low so default to 5v

I need to be sure that the output is never at 0v during startup as this could open my garage door. My question is: What is the intial state of a pin when set to OUTPUT? Is it 0v or floating until set to HIGH? I know there is only going to be a few clock cycles between the the two lines of code so the risk of the relay operating is pretty low. I'd just like to know if there is a clean and safe way of ensuring that the output pin is floating or at 5v after startup and never at 0v.

Can anyone help?

GraemeP

You can shorten the time the output pin is low by setting the bit high while it's still in input mode as below. Place the steps early in the setup function. Not sure it is required for relay operation but it's good to know the method:

digitalWrite(doorActivateRelayPin , HIGH  ); // this sets the pin ports value to high and turns on the internal pull-up 
                                                            // resistor 
pinMode(doorActivateRelayPin    , OUTPUT); // set door relay pin as Output, which should be a HIGH at this time
digitalWrite(doorActivateRelayPin , HIGH  ); // Active low so default to 5v, yes to make your intentions clear.

Heres what pinmode does, not xure if that helps

void pinMode(uint8_t pin, uint8_t mode)
{
    uint8_t bit = digitalPinToBitMask(pin);
    uint8_t port = digitalPinToPort(pin);
    volatile uint8_t *reg;

    if (port == NOT_A_PIN) return;

    // JWS: can I let the optimizer do this?
    reg = portModeRegister(port);

    if (mode == INPUT) { 
        uint8_t oldSREG = SREG;
                cli();
        *reg &= ~bit;
        SREG = oldSREG;
    } else {
        uint8_t oldSREG = SREG;
                cli();
        *reg |= bit;
        SREG = oldSREG;
    }
}

retrolefty: You can shorten the time the output pin is low by setting the bit high while it's still in input mode as below. Place the steps early in the setup function. Not sure it is required for relay operation but it's good to know the method:

digitalWrite(doorActivateRelayPin , HIGH  ); // this sets the pin ports value to high and turns on the internal pull-up 
                                                            // resistor 
pinMode(doorActivateRelayPin    , OUTPUT); // set door relay pin as Output, which should be a HIGH at this time
digitalWrite(doorActivateRelayPin , HIGH  ); // Active low so default to 5v, yes to make your intentions clear.

Yes, that makes sense. as an Input (the default at start up) the pin will be floating (which is OK). After setting the pullup resistor on it will be at 5v.

I've just run a little test with this code and it appears that if you set a pin HIGH when it's at input - as you suggest, it actually maintains the HIGH setting when the pin is set as OUTPUT. So the pin will never be pulled down to 0v - which means it will work as required. Excellent.

void setup() {                
  delay(2000) ;
  digitalWrite(13, HIGH);   // LED glows dimly - it's getting 5v via pullup & onboard LED13 resistor.
  delay(2000) ;
  pinMode(13, OUTPUT);   // LED glows bright - output must be HIGH     
  delay(2000) ;
  digitalWrite(13, LOW);   // LED off - so I can try it again with a RESET.
}

Cheers

GraemeP