2
\$\begingroup\$

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

\$\endgroup\$
  • 2
    \$\begingroup\$ Is the ground of the MCU connected to the negative terminal of the battery? \$\endgroup\$ – Ignacio Vazquez-Abrams Jun 16 '14 at 4:13
  • \$\begingroup\$ Yes the MCU is connected to a common ground with the battery \$\endgroup\$ – user45675 Jun 16 '14 at 4:30
2
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

In the second circuit, with the values of R2 and the MCU's enabled pull-up unknown, a voltage divider will be formed which might not allow the MCU's I/O to get close enough to 0V, when SW1 is made, to read a logical 0.

This will work with either the MCU's internal, or an external pull-up.

                                             Vcc       MCU
                                              |    +--------- 
                                              +----|Vcc
                                              |    |     
  13V    +----------------------+            [R2]  |     
     \   |                      |             |    |
      +--|---[R1]---[LED>]---O--|---[<DIODE]--+----|I/O
      |  |                  /   |                  |
    [BAT]|                 / O  |                  |
      |- |              SW1  |  |                  |
      +--|-------------------+--|------------------|GND
         |                      |                  +----
         +----------------------+
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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