Hi All,
First post here. I am planning to create my own esp32 relay and switching module for esphome/home-assistant. Below is a sample circuit for one relay and one switch that I am planning to use. Apologies before hand for the long post and also the design, its my first one and hopefully everything makes sense.
The circuit consists of two parts, a relay part and the switch part. My idea is to have basic logic within the esp32 so that the esp32 can function even if home-assistant is down. This will be limited functionality but at least one would be able to switch a light on/off and then more complex automations would be handled by home-assistant.
For the relay part, an optocoupler (EL817) is used since the relay uses 12v. On the low voltage side of the optocoupler, there is a 3.3v input from esp32 (pin 1) and pin 2 is connected to an led and resistor and connected to a GPIO pin on esp32.
The high voltage side is connected to a 12v supply (pin 4) and pin 3 is connected to a darlington array (ULN2804A), which has an inbuilt freewheeling diode for some level of protection to the circuit. The darlington array is also connected to the 12v supply on pin 10 and then pin 9 GND connected to the negative of the 12v supply. Pin 1 is connected to the output of the optocoupler (pin 3) and pin 18 is connected to the 12v coil of the relay. The other input for the coil of the relay is connected to the 12v supply. An AC load can then be connected to the relay.
For the swithcing part, I am also using an optocoupler, since I plan to wire a 12v supply to wall switches to act as the signal instead of 3.3v or 5v. The optocoupler on the high voltage side is connected to the 12v supply with a switch in between on pin 1, and pin 2 is connected to an LED and resistor and connected to the negative side of the supply. The low voltage side of the optocoupler has pin 4 connected to a GPIO pin on esp32 and pin 3 connected to GND on esp32.
The below is the esphome yaml config for the relay and switch:
switch:
- platform: gpio
pin:
number: GPIO16
inverted: true
name: "Relay Status"
id: relay_status
binary_sensor:
- platform: gpio
pin:
number: GPIO17
mode:
input: true
pullup: true
name: "Relay Switch"
id: relay_switch
on_state:
then:
- switch.toggle: relay_status
With this setup, when the physical switch is toggled, the relay is toggled on/off. If the relay is turned on from home-assistant (automation or manually via the UI) then if the physical switch is toggled this would turn off the light. With this setup it would also be possible to know if the physical switch was used to turn off/on the lights/relay which is great for automations. I have all this setup on a breadboard and everything seems to be working well.
What do you guys think, does this make sense at all? Are there any improvements that can be made to the circuit, mainly in terms of protection for back emf, transients, voltage arc’s in the relay contacts etc? I was thinking maybe an RC snubber would be required on the AC load side of the relay but not sure if anything else is required.
Thank you