Driving a relay with two Arduino

I got a 2 relay module. It's powered using an external 5v power supply and controlled by an Arduino Mega.

It's connected in the following way:

JD-VCC = 5v (external supply)
GND = GND (external supply)

IN1 = Pin 10 Arduino Mega
IN2 = Unconnected
VCC = 5V Arduino Mega.

I want to add an Wemos D1 Mini (ESP8266) (3.3v) to the relay so I will be able to control the relay through wifi. The Arduino Mega is very hard to reach. I would prefer to only connect it to the relay and not to the Mega.

Could I just connect an IO-pin from the Wemos to the IN1 of the relay? So that the wiring would be like this:

JD-VCC = 5v (external supply)
GND = GND (external supply)

IN1 = Pin 10 Arduino Mega & Pin 5 Wemos D1 Mini
IN2 = Unconnected
VCC = 5V Arduino Mega.

Is that safe to do? Could it give me trouble?

IN1 = Pin 10 Arduino Mega & Pin 5 Wemos D1 Mini

EDIT ... when I wrote the rest of this Reply I was not aware that there is an opto-isolater so what I wrote may well be irrelevant

NO

That would result in 5v being applied to a pin set as OUTPUT and LOW which would overload and damage the pin.

To do what you want you need a pullup resistor on the Relay pin and a diode between the relay pin and each of the boards so that when the Relay pin is HIGH it cannot feed to either board. But the diode will allow either board to pull the relay pin LOW.

You will also need a GND connection between the relay and the Arduino and Wemos.

I don't know if it is necessary to have a 5v connection from the Arduino to the relay - I suspect not if the relay has an external 5v supply. Post a link to the datasheet for the relay.

...R

Robin2:
NO

That would result in 5v being applied to a pin set as OUTPUT and LOW which would overload and damage the pin.

To do what you want you need a pullup resistor on the Relay pin and a diode between the relay pin and each of the boards so that when the Relay pin is HIGH it cannot feed to either board. But the diode will allow either board to pull the relay pin LOW.

...R

Ah that makes sense. Thank you for pointing that out.

It's just a standard songle two relay module from Aliexpress. I have no idea where I would get a schematic for the module.

You will also need a GND connection between the relay and the Arduino and Wemos.

I don't know if it is necessary to have a 5v connection from the Arduino to the relay - I suspect not if the relay has an external 5v supply. Post a link to the datasheet for the relay.

The relay won't turn on if there is no 5v connection between the Arduino and the relay. It's probably for the optocoupler.

Wiring the GND together is not a good idea if I want opto-isolation.

Lennyz1988:
I have no idea where I would get a schematic for the module.

....

Wiring the GND together is not a good idea if I want opto-isolation.

I did not mean a schematic or circuit drawing - just the datasheet that explains how to use it. For example, before your Reply #2 I had no idea there is opto-isolation.

...R

Robin2:
I did not mean a schematic or circuit drawing - just the datasheet that explains how to use it. For example, before your Reply #2 I had no idea there is opto-isolation.

...R

I am sorry for the lack of information. I assumed that this module is commonly known. I have no idea where i would get a the datasheet, or if even one exsists. The attached schematic is probably for the same module. The module can be operated with or without an external power supply.

JD-VCC = 5v (external supply)
GND = GND (external supply)

IN1 = Relay 1
IN2 = Relay 2
VCC = 5V controller
GND = gnd controller

Without having one of them to experiment with I am reluctant to advise further in case the smoke escapes.

What I said in Reply #1 is also probably irrelevant because I was not aware of the opto-isolator.

...R

Robin2:
Without having one of them to experiment with I am reluctant to advise further in case the smoke escapes.

What I said in Reply #1 is also probably irrelevant because I was not aware of the opto-isolator.

...R

Could you reconsider? I have plenty of Arduinos, Relays and other chips to test with. I do not care if some of them go broke. I cannot learn without making mistakes.

The module is an active-low.

digitalWrite(relay, LOW); Will power it on.

Do I have to make a voltage divider to make the output of the Mega pin to 3.3v because the Wemos is also 3.3v?

Maybe you could make another circuit that turns power (or GND) for the opto-isolator with two other separate opto-isolators one each powered and controlled by the Arduino or the Wemos.

Hopefully someone with better knowledge of this sort of circuit will come along.

Make sure the Arduino 5v does not find its way to the 3.3v Wemos.

...R

It seems you have two power supplies: Vcc and JD-Vcc. It seems they can be coupled, or separated (otherwise why have these two separate connections - coupling the two negates the function of the optocoupler) - the jumper seems related to this.

Now IF your schematic is correct, it may work like this.
JD-Vcc is connected to the external 5V, powering the relay.
Vcc is connected to the WeMOS, powering the optocoupler.
IN1 and IN2 go to the digital ports.

Based on your schematic, this should not fry your ports as the highest voltage they see is 3.3V. It may still not work, as the current through the optocoupler drops (how much depends on the voltage drop on the external LED and the value of R1 and R4), the optocoupler conducts less, and the transistors may not open enough.

wvmarle:
Based on your schematic, this should not fry your ports as the highest voltage they see is 3.3V. It may still not work, as the current through the optocoupler drops (how much depends on the voltage drop on the external LED and the value of R1 and R4), the optocoupler conducts less, and the transistors may not open enough.

In addition to that there is the complexity that the OP wants to connect two separate microprocessors to the Optocoupler.

...R

wvmarle:
It seems you have two power supplies: Vcc and JD-Vcc. It seems they can be coupled, or separated (otherwise why have these two separate connections - coupling the two negates the function of the optocoupler) - the jumper seems related to this.

Now IF your schematic is correct, it may work like this.
JD-Vcc is connected to the external 5V, powering the relay.
Vcc is connected to the WeMOS, powering the optocoupler.
IN1 and IN2 go to the digital ports.

Based on your schematic, this should not fry your ports as the highest voltage they see is 3.3V. It may still not work, as the current through the optocoupler drops (how much depends on the voltage drop on the external LED and the value of R1 and R4), the optocoupler conducts less, and the transistors may not open enough.

Thank you for your post. My question however is not how to drive this relay. The relay is already in place and working properly. My question is if I can switch one relay with 2 microprocessors by connecting both the Mega and the Wemos to the IN1 of the relay.

Robin2:
Maybe you could make another circuit that turns power (or GND) for the opto-isolator with two other separate opto-isolators one each powered and controlled by the Arduino or the Wemos.

Hopefully someone with better knowledge of this sort of circuit will come along.

Make sure the Arduino 5v does not find its way to the 3.3v Wemos.

...R

I'll probably just add another NC relay to the circuit. That way I will achieve the same result.

Yes, use a second relay. You may even use the 2nd one on the same board, so long as the Wemos and Mega can share grounds.

You should never connect two outputs together (the two processors).

Lennyz1988:
My question is if I can switch one relay with 2 microprocessors by connecting both the Mega and the Wemos to the IN1 of the relay.

Normally you could achieve this by adding a diode in each line, this way you basically create an OR port.

This will most likely work fine for the Arduino, with its 5V there's enough left after the 0.6V loss over the diode. For the ESP however it's almost certainly not going to work. 0.6V for the diode, 1.2V for the optocoupler, another 1.5V or so for the LED and there's nothing left to push any current through the resistor.

In your case, you could connect those two diodes to a small transistor such as a BC547, which in turn switches the relay input.

wvmarle:
Normally you could achieve this by adding a diode in each line, this way you basically create an OR port.

This will most likely work fine for the Arduino, with its 5V there's enough left after the 0.6V loss over the diode. For the ESP however it's almost certainly not going to work. 0.6V for the diode, 1.2V for the optocoupler, another 1.5V or so for the LED and there's nothing left to push any current through the resistor.

In your case, you could connect those two diodes to a small transistor such as a BC547, which in turn switches the relay input.

This also sounds like a good idea. It's probably more efficient then using a second relay module in my circuit. Thank you. I am going to built this setup.

I am unable to drive the relay module using a transistor. I am able to drive the relay module using a transistor by NOT using an external power supply. But as soon as try it with an external power supply I don't know how to hook it up.

I think that the problem is that I do not know how to deal with the not connected grounds to make sure the external power supply is separated from the Arduino. The module is an active-low. If the voltage applied to IN1 goes below 2v, then the relay will switch on.

I attached a hand drawn schematic.

JD-VCC = 5v (external supply)
GND = GND (external supply)

IN1 = Pin 7 Arduino
VCC = 5V Arduino

I tried a 2N3904 and a 2N3906. The datasheet for the 2n3904:

I can get the circuit to work by connecting the emitter side of the 2n3904 to the ground of the Arduino and the collector side to the IN1 of the Relay. But I am unsure if then the circuit is still opto isolated.

This circuit works to switch the relay:

Is this circuit properly connected and is it safe in terms of optical-isolation?
I also made a little mistake in the drawing. I use the 2N3906, PNP version.

ps I know this circuit is without diodes, but adding diodes is not the problem.

Lennyz1988:
I can get the circuit to work by connecting the emitter side of the 2n3904 to the ground of the Arduino and the collector side to the IN1 of the Relay. But I am unsure if then the circuit is still opto isolated.

IN1 is the optocoupler isn't it? Then it's still isolated. No problem there.

How about this circuit: switch with either Arduino or WeMOS, no risk for 5V reaching WeMOS. D4 is not really necessary; R3 value may be too high.
schematic.png

wvmarle:
IN1 is the optocoupler isn't it? Then it's still isolated. No problem there.

How about this circuit: switch with either Arduino or WeMOS, no risk for 5V reaching WeMOS. D4 is not really necessary; R3 value may be too high.

Thank you! I really appreciate all the help you gave me.

I have build that circuit and placed an extra diode between the Arduino and R3. I also used a value of 4.7k for R3. The transistor I used is the 2N3906. I am unsure if the resistor value is to high, but the circuit works fine. Why did you choose for a 220resistor in your circuit?

Datasheet: https://www.onsemi.com/pub/Collateral/2N3906-D.PDF

220 Ohm to provide plenty of current to the base of the resistor to saturate it and fully open it (no heat or so - not likely an issue though at these currents).
I didn't bother to look up the specs of that particular transistor.

You need to provide at least 10 mA for your optocoupler - if the transistor has an amplification of only 10 times (could be 100 times) you need to provide 1 mA current. 3.3V minus 0.6V for the diode is 2.7V for the resistor. 220 Ohm gives 12 mA - was intended to provide just over 1 mA so should have been 2k2 - miscalculation, lost a 0 somewhere.

4k7 gives 0.57 mA, so you'd need a 20x amplification for the 10 mA needed. Most small transistors around 50x so no problem there.

The actual value of that resistor is flexible. 10k may still work fine even.