ESPHOME ESP32 DIY relay and switch module

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

If you are only controlling one relay you could replace that expensive driver ic and optocoupler with a gp NPN transistor.

You should include a flyback diode across the relay coil.

No need for an RC snubber on the AC side if you are switching resistive loads.

Also your switch monitoring optocoupler is shorting out the 12V supply and gpio (pins 1&2 are shorted, as are pins 3&4). You can remove that entire circuit and just feed the switch 3.3v and use a pull down resistor in software or hardware on the gpio.

1 Like

Thank you for your reply. The plan is to automate all the lighting in the house, that is why I am using that driver ic. The circuit is just for one relay and one switch but will be expanded accordingly by adding more relays and switches.

With regards to the switch circuit, the original idea was to use 3.3v as you suggested, however, after researching a bit I am not sure that 3.3v would be suitable for wiring around the house, mainly due to wire length, voltage drops and also potential emf’s, so I decided to go with 12v and isolate the esp with the optocoupler. The plan to wire the switches and passing the supply is by using strands from a cat5/cat6 cable.

Would there be any better way to wire the switching circuit by keeping the 12v circuit and without shorting?

Thanks

Right, but you didn’t say that did you, all you said was:

So I thought you only wanted one. Using the IC for multiple circuits is a good idea.

Your thinking about using the 12V switch circuit instead of 3.3V for long wires is correct. Do that.

The short circuit is just a problem with the way you have drawn the schematic:

Apologies for this, it clearly was not communicated properly.

With regards to the circuit diagram, as well, not the best you will find out there. But yes, in the way they are shown they seem to be shorted, however, in reality they should be just crossing over and not connected to each other.

1 Like

If you are going to generate a PCB using the netlist from this schematic, they are definitely shorted (see the red dots that denotes connections?) and will cause layout errors. Best to fix it.

1 Like

Yes, the plan is to eventually go for a PCB. Thank you I will for sure fix this.

With regards to the switching side, would there need to be any kind of protection for the optocoupler input? Since the 12v input will be coming from wiring around the house, could there be spikes there that can do some damage?

Nope. Should be fine.

1 Like

Thank you. Much appreciated.

Maybe you mentioned it and I didn’t see it, but what relay part number are you thinking of using?

Hi, no I did not mention it. I am planning to use din mounted relays, but have nothing specific yet. The reasons for this are:

  1. I am planning to turn this into a pcb (hopefully) and I am not comfortable in connecting AC mains power via a pcb

  2. Using din mounted relays, they can easily be changed/replaced if they malfunction or something happens and it will also be possible to use different relays that can handle different loads easily

1 Like

using a similar relay and switch setup. but im running into one issue with this code. if the switch is turned on, it turns on the relay and the relay status shows as on in HA. but at the same time the status of the switch itself is always backwards. what am i doing wrong?

switch:
  - platform: gpio
    pin: 
      number: GPIO16  
      inverted: true
    name: "Relay Status SW-1"
    id: relay_status_sw_1
  - platform: gpio
    pin: 
      number: GPIO19  
      inverted: true
    name: "Relay Status SW-2"
    id: relay_status_sw_2
binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO17
      mode:
        input: true
        pullup: true
    name: "Relay Switch 1"
    id: relay_switch_1 
    on_state:
      then:
        - switch.toggle: relay_status_sw_1
  - platform: gpio
    pin: 
      number: GPIO18
      mode:
        input: true
        pullup: true
    name: "Relay Switch 2"
    id: relay_switch_2 
    on_state:
      then:
        - switch.toggle: relay_status_sw_2

About to try this with a 12v DC fail secure magnetic strike. This blue thing which came attached from the factory to the contacts - it looks like a capacitor but guessing it’s a flyback diode? Can anyone confirm?

Probably a MOV (metal oxide varistor). It’s there to absorb transient spikes. Which you get from turning power on and off across big inductors like the coil that moves the strike, V = L * di/dt.

Where L = inductance of the strike coil and di/dt = the change in current (a freaking lot in a short amount of time when you turn on/off which generates a very high voltage).

Simplified: MOVs have a lower resistance at higher voltages = absorb spikes.

They do degrade.

1 Like

OMG Thank you! I have been wracking my brain trying to find something like this … be it an off the shelf product - which pretty much appears to be non-existent - or a method by which to do this myself.

I am working with a friend to do a school bus conversion - Skoolie -
The lighting in the bus shall all be run on 12v DC and I am looking into adding Home Assistant and '‘smarts’ to these lights as well.

The issue is that the bus owner is pretty much HA and electrically naïve so these switches have to work manually as well as HA driven … but manually in the case HA is down , network is down etc.

Thank you so much for this as I have been searching high and low for a 12v solution.
I have even considered trying to convert various Sonoff switches to work with 12vDC and haven’t got far with that either.

More to come as I try to work this out :slight_smile:

Cheers!

Hi MG10,

How did the project go?
Any updates, improvements, tips?
I’m starting a similar project.
I plan to really separate stuff with even separate EL817 boards from aliexpress. The idea is to be able to replace parts on the fly with a ready made stock of components.
I’m buying 30a relays in single DIN-ready modules.
The ESP32 will be an ethernet one on a separate holder ready to be swapped when it starts misbehaving :slight_smile:

Hi, did you make a sketch for ESP32 for your project that correctly works and connects to HA via Wi-Fi?