Measure if LED is on with esp8266 and oktocoupler

Hey folks,

electronics is really not my field so I am asking for help.

I want to turn on and off my pc via an esp8266 and a few octocouplers. This I got working following some examples on the internet. No problem here so far.

But I also want to check if my PC is on or off via the power led.
I found that there was made a video from the hookup guy with this schematic:

My esphome code:

switch:
  - platform: gpio
    pin: D7
    id: power
    name: "Power toogle"
    icon: "mdi:PC"
    on_turn_on:
    - delay: 300ms
    - switch.turn_off: power
  
  - platform: gpio
    pin: D8
    id: reset
    name: "reset"
    icon: "mdi:PC"
    on_turn_on:
    - delay: 300ms
    - switch.turn_off: reset

binary_sensor:
  - platform: gpio
    name: "Power LED"
    pin:
      number: D0
      mode: INPUT_PULLUP
      inverted: true
    filters:
      - delayed_off: 30ms

Would that ESPHome code be right for the binary sensor?
Because for me this does not work in my current breadboard setup (might have missed something though).

1 Like

The way the circuit is drawn, when the power LED is on the opto will deliverer 3.3V to D0. When the power LED is off there will be no connection to D0. So when the LED is off the input will be ‘floating’. This is not good, floating inputs can be subject to noise and switch on / off randomly.

If you use mode: INPUT_PULLUP the input will be pulled ‘up’ to 3.3v by an internal resistor, so now you have 3.3v whether the LED is on or off. So it will always register as on.

You need a way to pull the GPIO down to ground when the LED is off, ie. use mode: INPUT_PULLDOWN not mode: INPUT_PULLUP.

This way the when the power LED is on the opto will deliverer 3.3V to D0. When the power LED is off the input will be pulled to ground.

binary_sensor:
  - platform: gpio
    name: "Power LED"
    pin:
      number: D0
      mode: INPUT_PULLDOWN
      inverted: true
    filters:
      - delayed_off: 30ms

Also the circuit is drawn with D5 controlling the power switch, yet you have used D7 in your switch code. Either wire the power switch opto to D7 or change your code to use D5.

Also you are using D8 for the reset switch (not shown in the drawing). This is not the best pin to use for outputs. See the table lower down on this page for the best pins to use:

Thanks for your answer!

This part I got working before with another example. But changed the pin layout accordingly:
Now using this for that part:

switch:
  - platform: gpio
    pin: D7
    id: power
    name: "Power toogle"
    icon: "mdi:PC"
    on_turn_on:
    - delay: 300ms
    - switch.turn_off: power
  
  - platform: gpio
    pin: D6
    id: reset
    name: "reset"
    icon: "mdi:PC"
    on_turn_on:
    - delay: 300ms
    - switch.turn_off: reset

Okay I think with this code I got the optocoupler working on the esp8266 side:

binary_sensor:
  - platform: gpio
    name: "Power LED"
    pin:
      number: D0
      mode: INPUT_PULLDOWN_16
      inverted: false
    filters:
      - delayed_off: 30ms

The sensor is off when no connection is happening. When I connect the cables of 3.3V and D0 the sensor turns on. When I release it turns off. Also pretty fast.
So I guess that part works.

Now when I connect

  • 3.3V to the Collector and
  • D0 to the emitter

The binary sensor is off like (LED circuit not connected yet.)

But somehow I can not get it to work with the led circuit.
My brain is not able to figure out how to connect the anode and cathode to the existing led circuit.


The 3rd optocoupler from the left is the one whos is supposed to check the led status.

I am trying to use two octocoupler for an low voltage 3.3v switch, and need to switch on and off quickly.

So how should the octocoupler and esphome yaml config look like ?

I can try the config you sent me :slight_smile:

I figured this out 2 weeks ago. I just do not use the optocoupler :sweat_smile:
Jane measured the voltage for the power led on my Mainboard and it was 3.3V.

In my Dev branch is the current working esphome config:

I have connected each of the switches via the opticoupler and the power led directly to the pins.

Works perfectly!
Need to finish this up.:sweat_smile: