Electronic – Read Switch Status without interfering with LED

ledswitches

I'm a software guy trying to get my head around how to measure the status of a switch connected to a LED as shown below through a digital input of a MCU.

My first try was many variations of circuit 1 below – seems every iteration I've tried causes the LED to dimly light even when SW1 is open. I've eliminated R3 and this issue still exists. I believe R3 and the MCU input provides a path to ground causing the LED to dimly light.

I then stumbled upon circuit 2 below which seems to work well. I can read the status of SW1 and the LED works correctly. Unfortunately I don't know why it works and therefore don't feel comfortable with it.

Can anyone suggest methods to read the status of SW1 with a MCU without interfering with the LED? Also why does circuit 2 appear to work?

Thanks!

schematic

simulate this circuit – Schematic created using CircuitLab

Best Answer

Your second circuit is perfect, provided that the pullup on the input is enabled.

When the switch is open, the anode of D1 is at VDD, and the cathode of D1 is at 13V-Vf. The diode prevents the higher voltage from reaching the input pin and destroying the MCU.

When the switch is closed, the cathode of D1 is at 0V, pulling the anode down to 0.7V and registering a low at the input.

You don't need the resistor since the pullup will not source enough current to damage the diode, but you must have the diode in order to prevent the higher voltage from reaching the MCU.