2
\$\begingroup\$

I am attempting to control a KVM switch using ESPHome on a D1 Mini ESP8266 board, so learning some electronics along the way.

The KVM has the main unit and a controller on the end of a 4 core cable. The controller consists of a single momentary switch, two resistors and two LEDs. (a photo of this board is attached below) The resistors say 222, which having googled appear to be 2.2kohm

controller board

The 4 wires inside the cable appear to be as follows

  • red - 5v
  • black - ground
  • green - data
  • white - momentary switch

When the green wire is

  • high (5v), LED1 is lit
  • low (0v), LED2 is lit

I have tried to recreate the controller with an Arduino while learning so I don't break the device itself.

I've drawn this circuit which I believe is how the controller board is wired. This is I suppose my first question. Is my understanding of how this is wired correct?

It functions as expected, when my button is pressed, the INPUT pin on the Arduino goes HIGH and the program inverts the OUTPUT pin for the LED and one LED switches on when the other switches off.

kvm and controller replica

I have then added a D1 Mini flashed with ESPHome and a PC817 optocoupler to press the button independently of the circuit. This works as expected. I've added this to my diagram. I can trigger the press via ESPHome and the LEDs on the controller swap over.

added optocoupler for button

I am then trying to read the LED pin via another optocoupler. The problem I'm facing is that once I connect the second optocoupler to the LED pin, I can't get the high/low signal to register the change on the D1 Mini unless i actively connect and disconnect the ground so I'm presumably wiring something wrong and i can't work out what.

I know the optocoupler is working correctly as I can place an LED on the other side of it and have it turn on and off when the LEDs on the controller alternate when switched.

section of circuit connecting d1 mini to optocoupler

If I test the connection with a multimeter between the output of the optocoupler and the ground pin on the D1 mini, it registers 3v when the controller is on LED 1 and 0v when the controller is on LED 2 as expected.

However, nothing is registered on the D1 mini. If however I introduce a connection to ground at the point indicated by the arrow in the below diagram, I can plug that in and unplug it and get an on/off on the D1 Mini.

with arrow

The fact that I can see results by adding a ground makes me think it's an physical electrical issue, hence posting here. However, in writing this and rubber ducking I wonder if actually it could be all wired correctly and just be an ESPHome/D1 Mini issue.

Any advice would be greatly appreciated as I'm just feeling my way through this project from complete scratch.

† (Apologies for the hand drawn and possibly backwards symbols)

†† I noticed after drawing the diagrams that i missed the little arrows off all my LED symbols, so those Diodes are supposed to be LEDs...

This is the binary_sensor in the ESPHome configuration in case it's of any use.

binary_sensor:
  - platform: gpio
    id: led_pin
    name: kvm_led
    pin:
      number: GPIO14
      inverted: false
      mode: INPUT
\$\endgroup\$
4
  • \$\begingroup\$ Would it make it easier with just a photodiode so that the existing LED circuit can stay intact? \$\endgroup\$
    – winny
    Commented Apr 25 at 6:02
  • \$\begingroup\$ "the point indicated by the arrow in the below diagram" - what arrow? Also the pinout of the optocoupler would help as they're not bidirectional. \$\endgroup\$
    – Finbarr
    Commented Apr 25 at 12:58
  • \$\begingroup\$ The existing circuit lives inside a tiny plastic housing so I can't really add anything to it. My plan is to replace the cable with a longer one as that needs doing anyway and put the additional circuit in the middle \$\endgroup\$ Commented Apr 25 at 13:55
  • \$\begingroup\$ @Finbarr well spotted, for some reason my computer didn't keep the markup i added to my drawing. I'll update shortly \$\endgroup\$ Commented Apr 25 at 13:56

1 Answer 1

1
\$\begingroup\$

It looks like the input pin has a pullup resistor on it, which is why shorting it to ground allows you to see the change.

If that's the case, using the opto coupler to pull it high to Vcc won't do anything - if it's on it will just pull it up more strongly. Try reconnecting it like this:

schematic

simulate this circuit – Schematic created using CircuitLab

R2 can be something like 10k although if there is already a pullup on D5 it isn't really needed at all and can be left out completely.

\$\endgroup\$
6
  • \$\begingroup\$ The ESPHome pin mode can be set to INPUT_PULLUP to activate the resistor which isn't activated (it's just INPUT) so might not be this? I'll try rewiring like this shortly and give it a go, thanks. Will feedback once i've given it a try \$\endgroup\$ Commented Apr 25 at 17:30
  • \$\begingroup\$ Nope unfortunately doesn't change. I used a 10k as R2. Still the only thing which changes the input on the pin is shorting to ground. \$\endgroup\$ Commented Apr 25 at 19:18
  • \$\begingroup\$ Can you measure the voltage between D5 and ground in the two states? I suspect you might not be turning the optocoupler on enough. And where exactly is "LED PIN" connected to? \$\endgroup\$
    – Finbarr
    Commented Apr 25 at 21:15
  • \$\begingroup\$ LED Pin is connected between the 2k resistors on the controller circuit. The voltage between D5 was 3 and 0. I noticed that unless the ground was connected i couldn't light an LED and i'd assumed the D5 was an input and has a route to ground. As the LED doesn't light there must be no full circuit? I think I've worked out out. I decided to connect to the analogue pin on the D1, it reads either 0.003ish and 1 in either state so it's never actually off so when connected to a digital pin it'd always be HIGH? I can use the analog pin with ATD to determine high or low based on a voltage threshold. \$\endgroup\$ Commented Apr 26 at 0:47
  • \$\begingroup\$ Connected ground from where to where? There's no need to connect the ground of the Arduino to the KVM if you're using opto-isolators. \$\endgroup\$
    – Finbarr
    Commented Apr 26 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.