1

I need to define a pin as output for relay module but when i do it in setup section, relay is activated (low - 0) automatically then i deactivated immediately with coding digitalWrite(pin, 1) but this issue results the relay is activated then deactivated within miliseconds. But I want to know that is it possible to define a pin as output but low in default.

void setup(){
  pinMode(pin,OUTPUT);
  digitalWrite(pin,1);
}

void loop(){
  if(start_relay){
    digitalWrite(pin, 0);
  }
}
  • you have a relay module which disconnects if signal is HIGH. the AVR MCU starts with pins in INPUT mode so the relay is not controlled by Arduino until setup() – Juraj Feb 17 at 20:13
  • 2
    You mean you want OUTPUT HIGH without an OUTPUT LOW transient? – Edgar Bonet Feb 17 at 20:14
2

There are two solutions:

  1. Use the pin as a open-collector pin as in the answer of VE7JRO.
    Switch the pin between input (high) and output with low. I prefer to use the INPUT_PULLUP.
  2. Write the output value before setting the pinMode.

All the microcontrollers of the AVR family (ATtiny and ATmega chips) allow to set the output value before setting the pin as output. This will enable the pullup resistor, but when it is done in the right order, that is fine.

Because this is a common way to use the microcontrollers and processors, the Arduino libraries allow to use that. The Arduino libraries for the SAMD processors (Arduino Zero, M0 and MKR) are written to allow that as well.

void setup() {
  digitalWrite(pin,HIGH);     // pre-set output value to HIGH
  pinMode(pin,OUTPUT);        // set to output, it will be HIGH
}

void loop() {
  digitalWrite(pin,LOW);
  delay(1000);
  digitalWrite(pin,HIGH);
  delay(1000);
}

When the relay module has floating inputs (as far as I know they do not) then you need extra hardware to pull the signal high with a pullup resistor to keep the relays disabled during reset.

1

As fas as I know, by default digital pins are set as inputs. Arduino has internally only pull-up resistors, which you can use in that way:

digitalWrite(pinOut[i], HIGH);

in first setup() line (without pinMode(pinOut[i], OUTPUT);).

If you need LOW state on start-up (pull-down resistors), you have to use an external resistor.

  • 2
    If you want to activate the internal pull-up, pinMode(pin, INPUT_PULLUP); is clearer (because more explicit) than digitalWrite(pin, HIGH);. – Edgar Bonet Feb 17 at 20:06
1

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.

enter image description here

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(){}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.