Driving a Relay using ESP32 - Page 1
Forum section: electronics / projects

author: stafil
author_rank: Regular Contributor
timestamp: July 04, 2025, 11:21:59 pm
content: Hi!I am trying to design a board for controlling my irrigation valves (they need 24VAC). I thought of using an ESP32 and a Relay for that purpose. The schematic I came up is the one attached here. I'd appreciate some feedback.What is not seen in the schematic:Power comes in via a power 24VAC 0.3A power supply(VIN+ and VIN-). It is converted to 3.3V and 5V using two buck converters based on LMR14206. ESP32 controlling PIN connects to R4.What I have in the schematic:- PC847 optocoupler to drive the relay with an 1N4148 input clamp across it- The relay with:* A ferrite bed in the +5V relay to reduce high frequency switching noise* Two bypass capacitors* A flyback diode across the relay coil* And finally a snubber for the AC valvesWould appreciate any feedback if I am missing anything or if any of the above look like overkill?Thanks so much in advance!

author: PCB.Wiz
author_rank: Super Contributor
timestamp: July 05, 2025, 12:11:06 am
content: Quote from: stafil on July 04, 2025, 11:21:59 pmA ferrite bead is not really needed around a relay.You need to check the CTR of the opto, coming from a 3V MCU with modest port drive, you only have a few mA into the opto.5V relays normally need 10's of mA, and a PC817 specs a very broad 50%-600% CTR, so you will come up short.A relay already gives some isolation, unless that 5V is isolated too, an opto is not adding much. A simple MOSFET can do the same task.Expecting much more than 100% from a NPN optocoupler, especially over temperature and time, is optimistic, so if you must opto isolate, look at SSR type optos.eg a OR-406AS-TP SSR is 60V 550mA < 2.5 Ohms with a 3mA turn onor you can add a MOSFET or digital transistor to an NPN opto coupler.Quote from: stafil on July 04, 2025, 11:21:59 pmWhat current do the valves need ?You could dispense with the mechanical relay entirely, and select a suitable SSR (PhotoMOS or PhotoTRIAC) to keep things very simple.

author: tooki
author_rank: Super Contributor
timestamp: July 05, 2025, 12:14:21 am
content: I would replace all of that with two things:-24V to 3.3V buck converter module-a solid state relay with low-voltage input

author: PGPG
author_rank: Frequent Contributor
timestamp: July 05, 2025, 12:18:35 pm
content: Replace optocoupler with npn transistor.Optocoupler is used to isolate two sides having separate GND levels (depending of optocoupler difference can be 1kV...4kV or even more).As in your circuit optocoupler both sides are connected to the same GND than optocoupler works like npn transitor having beta being equal to its CTR.For PC847 I see that for IF=5mA Ic can be as small as 2.5mA (so beta > 0.5).So if your relay need for example 20mA you have to drive optocoupler LED with 40mA (to ensure working with any PC847 that happens to get). Really you should add some extra margin and drive LED with 50mA what is specified as Absolute max meaning it is not save to use this current.Using any small signal transistor you get beta around 100 so you can drive its base with for example 1/20 of needed Ic current. If relay need 20mA you can drive base with 1mA.

author: stafil
author_rank: Regular Contributor
timestamp: July 05, 2025, 06:48:36 pm
content: Thanks folks. You are right. Using the optocoupler doesn't make sense. Also SSR is a good idea. Let me revise, thanks!

author: SteveThackery
author_rank: Super Contributor
timestamp: July 05, 2025, 09:04:23 pm
content: Have you considered a ready made relay module? They are really low cost, tried and tested, and very easy to use. The one in the link is designed to be connected directly to the microcontroller pins, and requires a 5V supply for the relay coils. I've had good service from them.I don't know if the relay contacts are sufficiently rated for your needs, but I'm sure there are other similar modules.https://banggood.onelink.me/zMT7/gesvy16k