ESP32 24V Relay Control

Thread Starter


Joined Dec 5, 2021
I am designing a smart controller for an existing heating system. The system and pumps are controlled by 200V AC latching relays. As an interface to the existing controller, these are controlled by 24V DC relays. As a Mechanical Engineering I understand some basic electronics but not best practices and detailed infomation.

What I am trying to achieve?
To switch each relay from an ESP32 board that will be controlled from Home Assistant. Each pump has an ON and an OFF relay. Each relay only needs to be energised for 500ms or so. This can be programmed in Home Assistant. The switch in the diagram will be an output of the ESP32.

Additional Information
Relays are Omron MY2-D
All of the relays are already wired as common ground
Optocouplers I have bought are PC817
I have a range of transistors if needed

My Question...
Can I control these relays reliably just using optocouplers? Or is it best practise to also use a transistor? The relays have a resistance of 662Ω so I believe this acts as a current limit.

If optocoupler is off then it requires a pulldown resistor (10k in picture) to avoid the output floating?



Joined Jul 18, 2013
I would use a Mosfet, e.g. 2n7000 the relay would be wired in the drain circuit.
Likewise, your schematic should show the relay in the collector of the transistor, not the emitter.
If you have to change the logic of the switching of the relay, use the other output of the Opto.
collector side rather than emitter side as shown.
The optocoupler only should work.

The NPN won't work as you have it drawn.
In order to turn an NPN on you need a base voltage that is higher than the emitter voltage by at least a half a volt or so meaning you would need at least 24.5 volts at the base depending on the specifications. Normally NPN are used on the ground side of the coil.

You won't need the 10k resistor The output won't float because it is already being pulled down by the relay coil.


Joined Sep 24, 2015
I agree with both comments above. However, I'm calculating a current of 36mA. The Opto alone - if it can handle that current then it should be OK. If not - the 2N7000 N-Channel MOSFET would be advisable. And as @Yaakov suggests, the 2N7000 should follow the relay coil, not precede it.

Thread Starter


Joined Dec 5, 2021
Thank you for the sugggestions. I understand that the ideal solution is to use a NPN type transistor, with the relay on the collector.

With the existing system I can not see how this could be implemented. The grounds for the relays I want to control are all common, these are wired in multiple places. Therefore I assume that only high side switching is possible in this case. Unless I reconfigure the wiring.

The relays are located in the boiler room, the grounds are wired together.

2021-02-15 11.52.37.jpg

Blue wires from the relays connect to the multiple coloured wires that lead to the house.

2021-04-30 09.51.23.jpg

The control unit is located in the house, grounds are common on the existing control board.

2021-04-30 10.24.53.jpg

The signal wires between the two locations are around 20m long. While the optocoupler only setup worked on my bench, it did not work with this system. This was surprising as I didn't expect much voltage drop, so not sure of the reason for this.

2021-10-29 11.27.57.jpg

The existing controller is from Honeywell. Unfortunately this stopped working a long time ago, so heating control so far has been done manually. I took a look at the control board and it uses C2120 NPN transistors. How are they functioning in this setup?

Beau Schwabe

Joined Nov 7, 2019
Use a PNP variation of your original circuit.
The 2.2k limits the base current of your transistor and C-E junction of your OPTO to about 11 mA.

R = ( 24V - 0.65V ) / 2.2k = 10.6mA


Thread Starter


Joined Dec 5, 2021
Thank you for the suggested circuit, I tested this with the system and it worked correctly. I've used components that I currently have to hand, A1015 PNP transistor and PC817 optocoupler.

Do you have any other that would be better suited for optocoupler and transistor?