1
\$\begingroup\$

I'm confused as to how an active-low push button detects a change in voltage. Below is a model of a conventional setup of one of these buttons I found.

enter image description here

When the switch is open (like in the image), there is some current, I, flowing into the MCU.
But when the switch is closed, wouldn't the current just flow to both the GND and the MCU like in a parallel circuit? (because the electrons have the same amount of potential energy so the voltage is still the same?)
If so, how does the MCU detect when the button has been pressed if there is constantly current flowing into it?

I'm a novice with microcontrollers and electronics, so please don't hesitate to point out the obvious!
Thanks.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ The MCU doesn't detect current. It detects voltage. And the MCU pin draws almost zero current, so ask yourself, if the switch in your diagram is open, what is the voltage drop across the resistor if zero or almost zero current flows through it? And what voltage appears at the MCU pin as a result of this if 5V is on the other side of the resistor? \$\endgroup\$
    – DKNguyen
    Apr 13 at 21:08
  • \$\begingroup\$ But isn't the voltage also the same when the button is open/closed? \$\endgroup\$
    – Andy Krugs
    Apr 13 at 21:11
  • \$\begingroup\$ It isn't. When the switch is closed, the pin is connected directly to GND and since the MCU pin uses GND as a reference to measure voltage against the pin will read 0V. When it is open is the situtation is as described above. \$\endgroup\$
    – DKNguyen
    Apr 13 at 21:11
2
\$\begingroup\$

When the switch is open (like in the image), there is some current, I, flowing into the MCU.

Yes.

But when the switch is closed, wouldn't the current just flow to both the GND and the MCU like in a parallel circuit?

No. When the switch is closed, there is a short between the mcu pin and ground. So current will flow out of the mcu to ground, and through the resistor to ground.

If so, how does the MCU detect when the button has been pressed if there is constantly current flowing into it?

Current flows into the MCU when the switch is open, and the voltage on the MCU pin is high. Current flows out of the MCU when the switch is closed, and the voltage on the MCU pin is ground.

Most MCU's these days are CMOS, and the current into or out-of a CMOS input (in steady state) is quite tiny. It is the voltage at the gates of the input mosfets which determine whether the MCU is detecting a "high" or a "low" input.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ MCUs have CMOS logic inputs these days. There will be (virtually) no current flowing in or out of the input pin, whether or not the button is pushed or not. My assumption is that the few nanoamps of leakage current or currents to move charge to change the voltage due to input pin capacitance does not count, so that is why I said virtually no current. \$\endgroup\$
    – Justme
    Apr 13 at 22:13
  • \$\begingroup\$ @Justme That's fine. It wasn't me who voted your answer down. I agree that the input pin of a cmos device sources/sinks very little current in steady state. A little more than that when switching. \$\endgroup\$ Apr 13 at 22:34
  • \$\begingroup\$ I didn't think it was you - It's just that this answer gives an impression that there is some significant current flowing in/out of MCU pin on purpose which is used for detecting pin state, while the main thing is the voltage, and any currents via IO pin are just non-idealities of real world components. In a MCU text book for beginners, it would be considered ideal and said that no current flows. \$\endgroup\$
    – Justme
    Apr 13 at 22:40
  • 1
    \$\begingroup\$ I made an edit to incorporate you point. \$\endgroup\$ Apr 13 at 22:46
  • \$\begingroup\$ @MathKeepsMeBusy But even if there was current flowing from the MCU pin to GND, wouldn't the MCU still experience 5V? If not, what does it experience, and how does it detect this? \$\endgroup\$
    – Andy Krugs
    Apr 21 at 21:28
0
\$\begingroup\$

There are a lot of false assumptions.

No, current does not flow in or out of MCU input pin. No, MCU does not detect flow of current.

The only thing the MCU can detect is voltage.

Pushing the button connects GND to MCU input so there is 0V. Sure, current would flow via resistor and button.

When buttons is not pushed, no current flows and thus resistor keeps MCU input at VCC.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ "No, current does not flow in or out of MCU input pin." No - current certainly does flow into and out of a logic input pin, such as an MCU I/O pin in input mode. This is input leakage current (I_IH and I_IL), with common values being +/- 2..10 uA. This is often a significant characteristic and must be taken into account in input circuit calculations. \$\endgroup\$
    – TonyM
    Apr 13 at 22:21
  • \$\begingroup\$ @TonyM Sure, there is leakage. Also current due to changing voltage in the input pin capacitance. We both know that, and I do think that the concept of leakage current should be taken into discussion when the question is as simple as how an MCU can read a pushbutton. It might become relevant if the pull-up resistor would be 10 Mohm instead of 10 kohm. Common values are also near 1uA, at very high temperatures. \$\endgroup\$
    – Justme
    Apr 13 at 22:21

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.