ESP Binary Sensor with state triggered by external voltage changes

Need some help on how to create a binary sensor in ESPHome which is driven by an external voltage value.

I have ESP’ed an Aroma Diffuser - a momentary button switch simulates the physical switch, which then turns on the internal timer on the Difuser. Once the internal timer ends, the diffuser shuts itself. There is no “end event” which is reported from the Difuser back to the ESP when the timer ends and the Difuser goes back to sleep.

I am trying to find a way to show the state of the Difuser as on, while it is running.

I can use the voltage across the internal diffuser fan as the trigger - it shows 12V when running, 0 when it is not. I have been looking at using the ESP Analogue to Digital (ADC) sensor. The actual voltage itself doesnt matter so long as it is zero or not-zero. I am thinking of sending the raw ADC value ( between 0-4095) to HA, then use a HA automation to determine if the difuser is on or off, where <20 = Off, >20 = On, or similar.

Apart from using the ADC is there any other simpler way to use the external voltage readings from the difuser to create a binary sensor in ESP, either via another ESP function or an external module etc?

Are there any other gotcha’s to look out when using the ADC function?

Many thanks for any help!

Thanks for that! If I understand it correctly, I still need to set up the ADC to get the actual voltage reading from the external voltage.

Based on the ADC readings, I can then use the Analogue Threshold Binary Sensor to set up the on or off states rather than passing the raw ADC reading across to HA and set up the binary sensor via a HA automation.

Is this how it should work?

To detect 0 versus 12 Volts - no need to use ADC.
Use optocoupler or even 12V relay or just a transistor to send on/off signal to GPIO.

Thanks for that tip! I did think of that but with my very limited limited ESPHome experience, have only used optocouplers to drive an external circuit when a GPIO pin was triggered high. Have never actually done the reverse ie. use an external circuit to set a GPIO pin high.

Simplistically, the solution feels like:

  1. Input - wire the optocoupler across the external 12V circuit with the appropriate limiting resistor in series.

  2. Output - wire the optocoupler so that 5V from the ESP is sent to the GPIO pin (say pin 17 or 18) when the optocoupler is high, 0V when the optocoupler is low. The optocoupler separates the circuits, so the ESP only uses its own 5V to set the GPIO pin high.

Is this correct? Sorry for the naive questions - think my ESP knowledge/use might just have gone up a notch with this! Many thanks for your help and patience… !

Exactly. IMHO 3.3V is quite enough and use pull-down resistor to keep input low rest of the time.