Relay module is active low...how to reverse that

arusr:
The Arduino's output will still go low when it reboots, and the relay board would still be active LOW, would it not? So when the Arduino reboots (1-0-1 transition), the relay would still pull in until the microcontroller runs the program again.

Someone correct me if I am wrong here, but the Arduino output does not go low when it reboots, it is open circuit and will not actuate the opto-coupler.

What does sound of interest now, is that you are powering the relay board from the Arduino Vcc. I am betting that is the problem and that if you provide a more appropriate power source for the relay board (JD-Vcc), the problem will go away.

Oh, and if the relay power supply is only used for the relay board, do not deliberately connect its ground to the Arduino ground.

Universal problem reported here but the Arduino Vcc should never be used for powering motors, relays, or other than a couple of LEDs drawing 10 mA or so.

dlloyd:
This should work providing the power link is removed and you only drive one relay.

So what happens if he wants to use the other relays, eh? ;D

My advice - find the actual problem and fix that.

Paul__B:

arusr:
The Arduino's output will still go low when it reboots, and the relay board would still be active LOW, would it not? So when the Arduino reboots (1-0-1 transition), the relay would still pull in until the microcontroller runs the program again.

Someone correct me if I am wrong here, but the Arduino output does not go low when it reboots, it is open circuit and will not actuate the opto-coupler.

What does sound of interest now, is that you are powering the relay board from the Arduino Vcc. I am betting that is the problem and that if you provide a more appropriate power source for the relay board (JD-Vcc), the problem will go away.

Oh, and if the relay power supply is only used for the relay board, do not deliberately connect its ground to the Arduino ground.

Universal problem reported here but the Arduino Vcc should never be used for powering motors, relays, or other than a couple of LEDs drawing 10 mA or so.

dlloyd:
This should work providing the power link is removed and you only drive one relay.

So what happens if he wants to use the other relays, eh? ;D

My advice - find the actual problem and fix that.

I second this, it does not go low on reset. I have this relay board in 4/8/16 channels, and I have reset/uploaded sketches countless times with the relay hooked up to my arduino, it never goes low. Try using external power for the board, if that still doesn't work, you will probably need to tell us more about your setup.

Someone correct me if I am wrong here, but the Arduino output does not go low when it reboots, it is open circuit and will not actuate the opto-coupler.

No correction required here. Pins default to inputs and so can and will float so there is no current path and the relay will not turn on.

Oh, and if the relay power supply is only used for the relay board, do not deliberately connect its ground to the Arduino ground.

I would disagree with that. You need to sing the current to activate the opto to activate the relay so you do need the arduino ground connecting to the relay board ground if it has a separate supply.

Arduino Vcc should never be used for powering motors, relays, or other than a couple of LEDs drawing 10 mA or so.

I would disagree with that as well, providing you apply sufficient decoupling you can run everything off one supply. If you don't decouple then the simple way round is to use a separate supply.

Grumpy_Mike:

Oh, and if the relay power supply is only used for the relay board, do not deliberately connect its ground to the Arduino ground.

I would disagree with that. You need to sing the current to activate the opto to activate the relay so you do need the Arduino ground connecting to the relay board ground if it has a separate supply.

You haven't been following the theme here. Read it again and recant!

Grumpy_Mike:

Arduino Vcc should never be used for powering motors, relays, or other than a couple of LEDs drawing 10 mA or so.

I would disagree with that as well, providing you apply sufficient decoupling you can run everything off one supply. If you don't decouple then the simple way round is to use a separate supply.

I said - the Arduino Vcc. The one derived from the on-board regulator (or the USB which will admittedly provide a little more power). It will not provide power or motors and the like. You are thinking of running the Arduino Vcc from a serious power supply, which is a totally different matter.

Q

Paul__B:
So what happens if he wants to use the other relays, eh? ;D

A

arusr:
...I'm only using one of the relays, so I have a digital output from Arduino to relay 4.

Q

Paul__B:
My advice - find the actual problem and fix that.

A
problem: Re: Relay module is active low...how to reverse that
Q

Paul__B:
eh? ;D

A
Who talks like that??? (I'm from Canada) ;D

You do realize that the relay switch has TWO positions. Instead of using the Normally Open position, you could use the normally closed instead. Then just change your program logic accordingly.

dlloyd:

arusr:
I'm only using one of the relays, so I have a digital output from Arduino to relay 4.

Makes you wonder - why would you get a four relay module and only use one relay?

dlloyd:
Q

Paul__B:
My advice - find the actual problem and fix that.

A
problem: Re: Relay module is active low...how to reverse that

And - as so often turns out to be the case in these discussions, that is absolutely not the problem once you get down to it!

dlloyd:
Who talks like that??? (I'm from Canada) ;D

You would perhaps prefer "Oi!"?

KenTF:
You do realize that the relay switch has TWO positions. Instead of using the Normally Open position, you could use the normally closed instead. Then just change your program logic accordingly.

Again, you need to read the thread and understand what the actual problem is. If you do, you realise that is not a solution at all!

Actually, I am getting to quite like the new forum system in many ways (but not all). I have it set to display 25 topics per index page, and 25 messages per thread. And I have - largely accidentally - got rid of the botched "WYSIWYG" reply window function.

Depending if I read correctly...

Pins revert to input on reboot, ie a floating state?.. a 10k resistor from the relay pin you're going to drive to vcc/5v.

Should keep your relay off until pinMode switches it back to output...

Geez Paul__B, relax. The eh? comment was directed at myself (how could I NOT know who talks that way) :-[

I decided to literally interpret the issue: "Relay module is active low...how to reverse that". To me "active high" is the solution to reversing active low.

The circuit I suggested is the only way I could think of that externally configures the relay's coil to energize with a digital control signal that goes HIGH. (limitations: 1 relay, untested circuit).

Really, it's what the OP requires that's important.

What's a good way to make the relay modules active high? I have an NPN transistor that I can use to flip it, but the example I find use a ULN2803, and I don't know how to take that schematic and apply it to a single NPN transistor.

Isolated power connections not shown:

EDIT: Add 10K pulldown resistor at D3 to prevent a floating condition at the transistor's base during bootup of the Arduino.

[b][u]D3[/u]     [u]NPN[/u]    [u]RELAY COIL[/u][/b] 
HIGHZ  OFF    OFF
LOW    OFF    OFF
HIGH   ON     ON

1 Like

cjdelphi:
Depending if I read correctly...
Pins revert to input on reboot, ie a floating state?

Yes, that's exactly the point!

cjdelphi:
a 10k resistor from the relay pin you're going to drive to vcc/5v. Should keep your relay off until pinMode switches it back to output...

This is what people keep on missing in this discussion. Because pins are floating on the reboot, they simply cannot be providing the current to the opto-coupler to switch the relay. Saying "add a pull-up" isn't going to "cut it" because whatever is providing the spurious current to the opto-coupler is no doubt equally capable of pulling down the pull-up. You have to figure out the cause in order to solve the real problem.

The glib suggestion of inverting the logic isn't going to "cut it" either because that will make things worse - the relay will then be switched on by the correct operation of the open-circuit pins on reset.

Out of curiosity. Do we even know WHICH input pin is being used? Would it perchance, happen to be pin 13?

"Relay module is active low...how to reverse that". To me "active high" is the solution to reversing active low.

But that changes it to default On. It isn't really active high, it is still active low, but now the output is On by default.

But that changes it to default On. It isn't really active high, it is still active low, but now the output is On by default.

Hello polymorph. That comment was referring to the circuit in reply#7, where logic HIGH would energize the relay coil, logic LOW would de-energize the relay coil. While the Arduino pin is unconfigured, the relay coil would remain de-energized by default. Although I'm sure it would work, I really wish I didn't post that suggestion ... but it fit the known requirements at the time (a one relay - active HIGH solution).

KenF:
Out of curiosity. Do we even know WHICH input pin is being used? Would it perchance, happen to be pin 13?

Good catch! :money_mouth_face:

Should not matter on a UNO where pin 13 is buffered (resulting in "phantom" activation of the "L" LED where the sketch does not define it), but will on a Pro Mini or Nano - it would cause exactly this effect.

I had the same problem as described using an 8 relay setup with low triggered inputs.. They were optoisolated exactly as what you have here.. upon a restart of the arduino the relays could be triggered momentary, something that caused undesired effects.. I found out that it was happening between when I used pinMode and digitalWrite.. Seems that when they were set as outputs they could go low enough to trigger the led's in the optoisolator and trigger the relay before the digitalWrite could set them high..

My solution was to simply set the pins high BEFORE using pin mode setting them as outputs.. I did this in the setup loop.. I copied my code below.. You could use pinMode and digitalWrite to do the same thing..

Not sure if this is approved or supported, or just plain wrong for some reason but it has been working great for my setup for the last year..

 // door outputs turning them high before using pinMode keeps them from twitching on startup..
  PORTD = PORTD | B11100000; // sets digital pins 7,6,5 HIGH
  PORTC = PORTC | B00111111; // sets analog pins 2,1,0 HIGH

  
DDRD = DDRD | B11100000; // turns 5,6,7 into outputs..
DDRC = DDRC | B00111111; // turns analog 0-5 into outputs..
1 Like

tek1229:
Seems that when they were set as outputs they could go low enough to trigger the LEDs in the optoisolator and trigger the relay before the digitalWrite could set them high.

My solution was to simply set the pins high BEFORE using pin mode setting them as outputs.. I did this in the setup loop.. I copied my code below.. You could use pinMode and digitalWrite to do the same thing..

Not sure if this is approved or supported, or just plain wrong for some reason but it has been working great for my setup for the last year.

You are of course, quite correct, the only limitation being the length of time between defining pins as outputs, and setting them high. It is rather unlikely that the relays will respond to a pulse (considerably) less than a millisecond.

This may be significant if you do not set the state in setup() and rely on the first pass (or so) through loop() and it would be habit to expect outputs to be active high and not require such special initialisation. And of course, this reverse order of operations insignificantly results in a transient INPUT_PULLUP state.

1 Like

Paul__B:
You are of course, quite correct, the only limitation being the length of time between defining pins as outputs, and setting them high. It is rather unlikely that the relays will respond to a pulse (considerably) less than a millisecond.

This may be significant if you do not set the state in setup() and rely on the first pass (or so) through loop() and it would be habit to expect outputs to be active high and not require such special initialisation. And of course, this reverse order of operations insignificantly results in a transient INPUT_PULLUP state.

Thanks for the feedback :slight_smile:
If I used pinMode and then a digitalWrite right at the start of setup() the relays would still be triggered for a split second.. I even tried direct port control thinking that might be faster and prevent the relay 'chatter'..Those optoisolators need almost nothing to be activated.. Since I'm operating 3 garage doors with separate open/stop/close buttons they would effectively go haywire during boot up of the arduino.. It gets shut down at night and turned on every day so I needed to correct this.. I was looking into adding a relay to cut out power to the relays when I discovered the software solution I posted took care of it..

I was looking into adding a relay to cut out power to the relays when I discovered the software solution I posted took care of it.

I like your solution to setup the registers prior to using PinMode.

Ideally all pins should be stable during bootup. I think the issue is that we can't explicitly declare PinMode to cover all possible initial pin states that the user may require:

[b]Initial State    PinMode Function[/b]
Input High Z     pinMode(pin, INPUT); 
Input Pullup     pinMode(pin, INPUT_PULLUP);
Output LOW       pinMode(pin, [color=red]OUTPUT[/color]);
Output HIGH      pinMode(pin, [color=red]OUTPUT[/color]);

Perhaps in a future revision of the Arduino IDE, for stable outputs during bootup, it could use something like this:

[b]Initial State    PinMode Function[/b]
Input High Z     pinMode(pin, INPUT); 
Input Pullup     pinMode(pin, INPUT_PULLUP);
Output LOW       pinMode(pin, [color=red]OUTPUT_LOW[/color]);
Output HIGH      pinMode(pin, [color=red]OUTPUT_HIGH[/color]);

Of course, for compatibility, pinMode(pin, OUTPUT) would still be available and work as previously.

You have GOT to be kidding us! ALL the time it was your code setting the pin to output mode before you send it HIGH DESPITE the fact that you KNOW having it in output mode while it's low will trigger the relay!

THIS is why we should really insist that people post their code!

What a waste of everybody's time!

But quite funny really :slight_smile: