For a control system I am using a relay module that controls a larger relay for high-current electrical stuff, as part of a battery protection circuit for my solar plant. This module switches 120 VAC for the larger one. The larger one is set up to latch the power off until a button is pressed manually.

I have several Adafruit relays, (single, dual, quad) and they all seem to be active-LOW. I am using one of them for this project.

My problem is that I can't seem to prevent my Arduino Nano code from briefly setting the output pin LOW in setup, setting in motion the cascade involving latching the power off.

If at all possible with this hardware, I would like to prevent the need to manually reset the latch when uploading code changes.

Here is how I am initializing that pin in my setup code:

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

All in one line. The LOW pulse is just long enough to trigger the other relay.

I know I can probably find parts like an inverter chip or maybe a different relay, but really want this working yesterday.

So my question is: Is there any way to prevent it from pulling it LOW during initialization? Or, is there a different approach I should take in relation to this relay output?

Thanks in advance

  • 4
    All in one line or not does not matter to C. It will execute the first expression (before the ";") first and the second expression second. You might try swapping the expressions and testing the results.
    – st2000
    Commented Apr 18, 2017 at 1:41
  • 1
    Most embedded processor initialize to inputs. You did not specify which Arduino you were using. Different Arduinos use different processors. Assuming this default behavior, you might add a pull up resistor to prevent the output of the processor (input to the relay driver) from going low while it is in the high impedance input state. This will add to the current drain. But only by a small amount. Let me know if either of these ideas work and I'll copy the text to an answer.
    – st2000
    Commented Apr 18, 2017 at 1:43
  • I edited the post to say it is a Nano. Hey, @st2000, that was genius. I first set it high, then set it as output, then just for good measure set it high again and voila, no low pulse! If you write that up as an answer I will upvote and accept for sure. Thank you very much. I can't help but think your second answer might work also, but don't have time to test it at the moment. Thanks again.
    – SDsolar
    Commented Apr 18, 2017 at 1:51
  • 1
    What if you write HIGH and then set it to output? And make sure to have a pull-up resistor. Commented Apr 18, 2017 at 4:34
  • 1
    fwiw, a capacitor can slow down relay transitions, fit to size...
    – dandavis
    Commented Apr 18, 2017 at 5:18

1 Answer 1


Arduino sketches are actually (somewhat constrained) C programs. As such, what is true for C is usually true for sketches. In C, a segment of code usually ends with a ";". It matters not if there are two (or more) of these on a single line. The computer will execute the segment before the first ";" first and the segment after the first ";" second.

In this case you are setting a register inside the Atmel processor on the Nano to the "out" direction. So it will force the associated pin to what ever the data in already set up in that pin's data register. Then you are setting that pin's data register to high or 1.

Instead, try setting the data register to 1 before setting the direction register to "out". Do this by switching the order of the code segments.

In case this does not work. Or in case you find the relays acting randomly on power up, consider the relay driver inputs to be high impedance and susceptible to any noise prior to the Nano taking control of the relay driver control pins. In such a case, consider adding pull up resistors small enough to ensure the inputs of the relay drivers are held at a known voltage but high enough such that the Atmel processor in the Nano can easily over power the pull up resistor when necessary.

  • 1
    The first option worked perfectly. I first set it to HIGH then set it to output then set it to HIGH again for good measure. Voila, no LOW pulse at all. Totally solved the problem. Thank you very much, @st2000 !
    – SDsolar
    Commented Apr 18, 2017 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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