Wiring a relay to a 3.3V Nano ESP32

I am trying to avoid frying anything. The world of MCUs seemed like a much safer place before Arduino followed Chinese manufactures down a 3.3V rabbit hole. Ah, the good ole days when everything was 5 volts. :slight_smile: :). There are now a number of videos regarding components and / or boards going up in smoke with the new voltage standards.

Please see the screenshots of the hookup and relay (I am going on faith the images will be displayed to facilitate communication. I haven't tried posting one before)

Hookup:

Relay

In addition this is what I have for documentation for the Amazon for the relay.

Module Size: 73x50x18.5mm (LxWxH)
4 Fixed Holes for Screw: Diameter 3.1mm
Work Voltage: 5V
Quiescent Current: 5mA
Max Current: 190mA
Trigger Current: 3-5mA
Low Level Trigger Current: 0-1.5V
High Level Trigger Current: 3-5V
Input Interface:

  1. DC+: 5V positive power supply
  2. DC-: 5V negative power supply
  3. IN: corresponding with trigger signal level of relay
    Output Interface:
  4. NO: normally open interface
  5. COM: common interface
  6. NC: normally close interface
    Trigger Mode Configuration:
    The module has a trigger level selection terminal, each relay trigger level can be individually controlled, through the short-circuit cap (better known as a jumper :)) to control. COM side is common, in the middle;
    When the LOW (L) terminal is connected to the common terminal, it is a low level trigger; when the HIGH (H) terminal is connected to the common terminal, it is a high level trigger.

I left out the chineselish for the high voltage side. It's clear as mud but I understand the high voltage side.

Package Include:
1pcs 5V 4 Channel High/Low Trigger Relay Module

Note the hookup shows a 5v connection directly to VIN on the Nano ESP 32 and then to the D+ and D- on the relay, while the logic input from the Nano ESP32 is a 3.3V pin. My concern is mixing voltages without a level shifter. The relay in the hookup is not from the manufacturer.

Also, I believe powering the 3.3V Nano which is not 5V tolerant with 5V on VIN works as there is a regulator? Would like to confirm that.

I think the hookup is correct but I would rather not find out by frying the Nano or having to troubleshoot whether it is my code or the hookup if the relay doesn't function.

  1. Does the hookup shown work?
  2. IF so, how is it that a 5 volt relay can be triggered with a 3.3V input?

As a side note I have noticed several posts regarding SD holders that seem to have a difficulty it using 3.3 or 5V components. I will be doing that next and wiring one on the prototype section of the Arduino Nano Terminal shield, but that is another topic.

This will work with any LOW level triggered relay and won't damage the ESP

I seriously consider zero crossing relays. As you have shown your circuit it will be susceptible to EMI (Electromagnetic Interference). You can get it from both the relays and the AC load. Here is a picture of one similar to what I use:


Note: It works directly with 3 - 32 volts from the processor.

1 Like

I think the safest way to wire that would be:
Power for relay coils:
5V to DC+
GND to DC-

Remove the jumpers

Arduino digital pin to IN1
Arduino GND to Com1(middle pin of high/low jumper)

This way you have HIGH trigger and you should be completely protected by optoisolators.

Thanks Jim,
I'm not an electrical engineer. This is not going to be a breadboard experiment. Looking for a simple plug and play solution in which I don't have to solder eight transistors on a prototype board or have PCB made. Doesn't you wiring diagram create a single direction level shifter? Does it solve the problem of a 3.3V possibly being to low voltage to trigger a 3-5V input on the relay?

My question is really:

  1. Focusing on the part of the spec repeated below, is a transistor or level shifter necessary to protect the ESP32 or are the relay logic "inputs" IN1 through In4 isolated from 5V DC+ / DC-?.

Low Level Trigger Current: 0-1.5V
High Level Trigger Current: 3-5V

  1. Is the 3.3 volts from the Nano ESP32 enough to trigger the relay dependably?

  2. The question really comes down to. Is a level shifter necessary?

If I do need to protect the board or 3.3V not enough trigger wouldn't it be easier to use a bidirectional 8 channel level shifter as there will be two banks of 4 channel relays so that would be your wiring diagram times 8 soldered onto a prototype board or I would need to have a PCB board made?

These level shifters may not be the best, but they are cheap with a TXS0108E IC with 8 low and 8 high connections with 3.3V and 5V IN and a GND broken out. The question is. Is it necessary?

Amazon.com: WWZMDiB TXS0108E Logic Level Shifter 3.3v 5v High Speed Full Duplex 8 Channel (6Pcs) : Electronics

This is the full add for the relay. I also don't understand what the optocoupler isolation accomplishes?

DZS Elec 5V 4 Channel Relay Optocoupler Isolation Module Red Board 3-5V High and 0-1.5V Low Level Triggered Load AC 0-250V / DC 0-30V 10A SCM IO Control: Amazon.com: Industrial & Scientific

It's Industry, not PLA-China.
Component manufacturers don't figure hobbyist use for any part of their production considerations.
As a matter of fact, the days of 3.3V are growing short - 1.8V is the next step. 1.2V is gaining adoption.

1 Like

I'll be lazy and not talk to uncle Google; are there smaller SSR modules?

1 Like

No

Does it solve the problem of a 3.3V possibly being to low voltage to trigger a 3-5V input on the relay?

Yes

Is a level shifter necessary?

No but a circuit like I show is.

I also don't understand what the optocoupler isolation accomplishes?

Nothing.

Thanks Jim,
Thanks.

I don't understand how the circuit can increase the logic voltage from 3.3V to 5V. Can you please explain?

To clarify, the circuit you provided is only necessary if I don't use a level shifter. Is that correct?

As it is two four-channel relays I would need to implement the circuit you provided 8 times on a prototype board or for a cleaner solution design a PCB and have it manufactured. Correct?

Thanks again

It does not.
The transistor basically acts as a switch that is connected to ground. So if you set-up the relay for low level trigger it provids the low.

The board still requires a 5V supply to operate the relays.

circuit you provided 8 times

Yes but in you original post you only showed 1 relay, I thought the 4 channel board was just an example.

I think I can come up with one or two DIP style ICs that will work for all 8.

Can you buy parts from Digi-Key or Mouser?

This would be the simplest solution, a TBD62783APG. It's an 18 pin DIP


This IC will supply 5V to the relay inputs, so you need to set the relay for HIGH trigger.
Connect Vcc to 5V and GND to GND
I1-8 connect to the nano ESP, O1-8 connect to the relays.

This IC is also nice: CD74ACT541E. All the inputs are on one side and the outputs on the other so it will be easy to connect.
You can use it with low or high level trigger.

Hi Jim, I appreciate your help. It has been educational, but again I am trying to do this with plug and play components. I did a bunch more looking into the relays and many of them claim that 3.3 Volts will trigger the relay and the optocoupler isolates the 3.3V IN from the 5V relay coil. All the ones that claim to be compatible with 3.3V arduino or python pins (high trigger of 3V to 5V and Low of 0 to 1.5V) have the same set of components, probably all from the same factory. Each distributor has a little bit of incomplete information. I had a long chat with chatGPT and finally learned the optocoupler theoretically isolates the 3.3V IN from the 5V relay by transferring voltage with light. However, the relays also appear to have the circuit you drew on board.

The component marked 2TY is a PNP transistor. They all have the same four resistors, 2 marked 222, one marked 102 and one marked 103 which is what leads me to believe they appear to have the circuit you first drew on board. The large black square with 4 connectors appears to be the optocoupler but can't make out what it says.

I have no idea what the round glass barrel next to the 2TY transistor is. Can you fill me in on that? Inquisitive minds need to know! :slight_smile:

It doesn't matter if they are in one, two, four or eight channel configurations, The only thing that changes is how many high/low jumpers they have and the how many input pins they have. (one for each channel)

So what I did was set up a 3.3V and 5V leg on a breadboard powering a 3-6V motor I had laying around, using the high voltage side of the relay. I used 5V on DC+ and DC- and connected the ground on both legs together. I then turned the power on and touched the lead from the IN pin to the 3.3V+ leg. The LED on the relay lit up, the relay clicked and the motor ran. I placed multimeter leads on the 3.3V+ lead and GRD on the bread board. Voltage measured approximately 3.3V.

So satisfied the relay wasn't back feeding 5V through the IN contact, I set it up with the IN lead on Pin 2 of the Arduino Nano ESP32 and wrote a simple sketch using digital write HIGH with a delay of 5,000 and then digital write LOW with a delay of 5,000. In other words, I turned the motor on and off every 5 seconds in void loop(). 5V was connected to the coil (DC+ DC-), 5V was connected to the Motor and a 3.3V pin was connected to relay IN. I let it run for about an hour. It worked like a champ.

For my project I will be powering 12V devices and using a Mini360 buck converter to step down 12v to 5V for the relay coils. I will power the Arduino Nano ESP32 with 12V on VIN. Wiring diagram and load calcs attached. Note the MP2322GQH regulator on the Nano ESP32 is rated for 1A with 90% efficiency so it can't handle the load for 9 relay coils with a 30% margin of safety making it is necessary to power the relay coils externally. In addition, powering the ESP32 with VIN disables VBUS (formally 5V). Nor could it handle the internal load of the Arduino in combination with the external load from the relay coils and SD card. The load on the digital pins is very low at 5ma, but with 9 relays that adds up to 45mA. The ESP32 regulator can handle the internal load of the ESP32 (240mA running WiFi), the load of the digital pins (45mA) and the external loads of 3.3V SD card (150mA) with a very comfortable margin of safety. Should work. Please let me know if you see any fail points.

Thanks again!!


No it does not.
If you look at the schematic for the relay, you will see that is not true.

It does if you follow what I wrote in post#4

1 Like

Hi kmin,

I'm haven't learned how to draw schematics, but it looks the schematic you provided is a bit different from the components I see on the relay. My background is systems analysis and design /software development in Oracle, SQL, Java, Javascript, HTML, etc.. My equivalent to a schematic is UML to conceptualize object-oriented n-tier applications and ERD to design data structures for relational databases. I am a hobbiest with hardware!

My "sense" is the 3.3V IN is isolated from the 5V DC+ and DC- when making the connections shown in the hookup below which come from a tutorial for the Nano ESP32

Again, I tested it by checking voltage with a multimeter (-) on 5V DC - via leg of a breadboard and multimeter (+) on IN connected to the 3.3V leg of the breadboard. IN was connected to the 3.3V leg of the breadboard. I measured approximately 3.3 V (a little less) with a 3V to 6V motor connected to the high voltage side of the relay.

So the question: The tracings on the relays PCB are pretty transparent. The manufacture doesn't recommend connection to the common of the high / low. The configuration of components is used by several "manufactures" and the relay is used in several tutorials on connecting this same relay to an Arduino Nano ESP32. The hookup attached is from one of the tutorials.

I'm trying to understand it. Can you reverse engineer a schematic of the relay from the image? Thanks in advance!

Not really the way that it would be more reliable than what I can assume from pinout and my experience. Since there is that H/L jumper, one contact of optocoupler has to be in the middle. The other contact is IN pin. So the isolated left part of the schematic I posted is valid. As long as you remove the jumper and connect your your Esp between jumper middle pin and IN pin you are isolated from 5V circuit. And that's the only way to do it correctly.

5V to DC+, GND to DC-. That supply can come even from separate power supply, common GND is not needed for optoisolated circuit.

Thank you, but I already tested it on with the Nano ESP32. I am satisfied it works as designed and won't fry the esp32. Just trying to learn how it works. Thanks again.

Only other possible way to wire it without damaging your Esp is with relay jumper to low side(probably labeled high trigger, who knows) and gpio to IN.
But that way there is electrical connection between both sides of the optoisolator.
In your case it's ok anyway.

Good you have confidence.