initalizing ouput as low?

is there a way to declare a pin an ouput and write it low at the same time? I'm witing code to control a relay and when my arduino first starts it sets pin 4 to ouput and then writes low, activating the relay for a second or two. i need a way to do both at the same time so the relay wont activate till i need it to. thanks

You could try setting the pin low in input mode and switching to output mode when you want to activate the relay. I have not tried it but it should work. Make sure that the relay is not drawing more curren than the pin can provide (40ma)

You could try setting the pin low in input mode

That just turns off the pull up resistors.

I’m pretty sure that the ports are low when entering output mode, but I can’t seem to find that in the datasheet at the moment.

“A second or two” sounds wrong. digitalWrite() is long compared to direct port manpulation, but it isn’t nearly that slow. I use pinMode() followed by digitalWrite(0) with a solid state relay and don’t get any undesirable behavior.

Check the rest of your code, I don’t think this is the problem.

-j

That just turns off the pull up resistors.

as I recall, the same register is used in input and output mode. Do you have any experience to indicate that it wont work as suggested in my previous post?

Do you have any experience to indicate that it wont work as suggested in my previous post?

Just the data sheet.

I do have experience with writing a high while in input mode turning on the pullups.

-j

Do you have any experience to indicate that it wont work as suggested in my previous post? (i.e. declare a pin to be an ouput and write it low at the same time)

Just the data sheet.

Where?

I interpret section 13.2.3 of the ATmega168 datasheet as indicating that if the PORTxn value for a pin is low then it will be high impedance when in input mode and active LOW when the mode is set to output.

Conversely (although the OP didn't ask for it), if pull-ups are enabled in input mode, switching to output mode will set the pin HIGH.

Your post seemed to imply that setting pull-ups was not related the port value when switching to output mode.

Yes, the pullups are related as you state.

But we’re talking about an apparent initialization issue; the pin is an input hi-Z with no pullup when the device powers up. Writing to the pin in input mode won’t change anything in this case, because it’s already in that state.

Again, based on the “for a second or two” comment and my own research and observations designing a circuit to control some power equipment, I really think there’s an issue elsewhere. Could be code, could be hardware, but I don’t think it is the way the pin is getting initialized.

Maybe the original poster could provide us with some code and maybe a schematic?

-j