How to detect a 12 volt signal using an ESP32 and ESPHome

published Jul 25, 2023

And how to optionally use it in Home Assistant.

How to detect a 12 volt signal using an ESP32 and ESPHome

A situation that occurs often in home automation:

You have a device (maybe an old stereo) that sends 12 volts DC (perhaps through an output on the back) when it's active, and you want to detect this event with a sensor (of the binary kind).  Perhaps your plan is to activate an amplifier, or a turntable, or a television, when the device turns on.  Software like Home Assistant can help you enormously with this, but you still need the hardware side of things.  We'll explore that side here.

You'll need:

  • An ESP32 device (with a suitable power supply, usually USB).
  • ESPHome installed on your computer.
  • A cheap 12 volt relay such as this.  Make sure the input to the relay is 12 volt and ground, rather than requiring a separate power supply.  Also ensure that the current rating on the relay won't exceed the current rating on your device's 12 volt power supply.

Here is how you hook up the electronics:

[ ESP32 ]               [ RELAY              ]
[       > GND  -----    < NO (normally open) ]
[       > D2   -----    < COM (common)       ]
[       ]               [                    ]       [Your dev]
[       ]               [                12V+> ----- <12V out ]
[       ]               [                GND > ----- <GND     ]
[       ]               [                    ]
[       ]               [                    ]
   USB

Here is a very short excerpt from an ESPHome sketch you'll have to flash to your ESP32 device:

# Example configuration entry
binary_sensor:
  - platform: gpio
    pin:
      number: D2
      mode:
        input: true
        pullup: true  # needed to detect continuity on D2
    name: "Music player active"

The way it works is: upon turning on your device, it will emit 12 volts, which will cause the relay's normally open circuit to close.  Upon closing this circuit, the D2 pin and the ground in the ESP32 device are bridged, and ESPHome reflects this in the binary sensor as a state of on. When the 12 volt signal is gone (you turn off your device), a spring in the relay will open the circuit again, and this change will be reflected in the binary sensor as a state of off.

You can add your ESP32 device to Home Assistant using the ESPHome integration, and the sensor will appear in Home Assistant.  Now that the sensor is active, you can use it in automations to make other things happen in your home!

Alternatively, if you add the Web server component to the ESP32 sketch, you will be able to see the status of the sensor through your Web browser, and will be able to access the status of your sensor via the REST API provided by ESPHome.

Some relays advertise normally-open and normally-closed connectors.  Choose whether to hook up normally-open or normally-closed based on safety — if the power to the device powering the relay were to fail, what state do you want the sensor to switch to?  Should you then need to invert the meaning of the sensor, you can then use the options of the binary sensor platform in ESPHome to decide whether on means off or vice versa.