If you have a relay that is activated by a LOW or ground path, and is deactivated
by an open circuit or HIGH signal applied, then you could try switching the pinMode
to act as the HIGH or open circuit.
const byte pin = 2;
void setup(){
// Define the pin as INPUT_PULLUP until you are ready to use it.
pinMode(pin, INPUT_PULLUP);
}
void loop(){
delay(1000);
// Activate the relay. Supplying a low impedance
// path to ground turns the relay on.
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delay(1000);
// Deactivate the relay.
pinMode(pin, INPUT_PULLUP);
// Using digitalWrite(pin, HIGH); is an alternative to using
// pinMode(pin, INPUT_PULLUP); If your relay module is the
// same as the one shown below, then it doesn't matter which
// method you choose.
}
I'm not sure which relay module you are using, but if it is the same as this type, using INPUT_PULLUP will work.

As Edgar Bonet mentions in his comment to kot's answer, using pinMode(pin, INPUT_PULLUP);
is clearer than using digitalWrite(pin, HIGH);
.
This code does 2 things:
digitalWrite(pin,HIGH);
pinMode(pin,OUTPUT);
First it sets the pin to INPUT_PULLUP, then it sets it to VCC (for lack of a better term). If you connect a modern LED with a 1k ohm resistor to pin 2, and use this sketch, you can see the LED "dimly" lit for 5 seconds, then it is "brightly" lit.
const byte pin = 2;
void setup() {
// pre-set output value to HIGH
// The internal pull up resistor is now connected to the pin.
digitalWrite(pin,HIGH);
// Wait for 5 seconds.
delay(5000);
// set to output, it will be HIGH
// The pin is now connected to VCC (for lack of a better term).
pinMode(pin,OUTPUT);
}
void loop(){}
setup()
– Juraj Feb 17 '19 at 20:13OUTPUT
HIGH
without anOUTPUT
LOW
transient? – Edgar Bonet Feb 17 '19 at 20:14