3
\$\begingroup\$

I'm working on an introduction to Arduino and circuits course and I've run into a stumbling block.

Everything works the way the course says that it should - I just don't understand WHY it works.

Here's the schematic:

Arduino and breadboard with LED and dip switch

The program is easy to understand. When digital input 2 receives input it lights the LED. The whole path with the LED makes sense to me, no need to discuss it. It's the path with the red wire and green wire going into the DIP switch.

My understanding is that the DIP switch is connected to both the digital input and the resistor and the resistor provides a path to ground for any stray voltage that gets induced on the wire by the environment.

What's confusing me is - isn't the digital input also a path to ground? If not, how does the circuit ever get closed? If there is stray voltage the resistor is providing a path to ground, but if the switch is closed then suddenly the digital input is the best path to ground?

When the switch is closed, the 5 volts can either go to the digital input or through the resistor and the resistor path has more impedance and so the 5V goes into the digital input. When the switch is open, why would any stray voltage not take the same path that the 5V takes?

\$\endgroup\$
2
  • 3
    \$\begingroup\$ The digital input is a very high impedance. You need the resistor to make sure the voltage goes to zero instead of floating and picking up stray electric fields. \$\endgroup\$
    – Hearth
    Mar 26, 2021 at 14:43
  • 4
    \$\begingroup\$ Microcontroller manufacturers try hard to make sure that current doesn't go through the input pins, because usually you don't want the input pin to affect the circuit it's trying to measure. \$\endgroup\$
    – user253751
    Mar 26, 2021 at 14:57

3 Answers 3

5
\$\begingroup\$

No current flows in or out of the input pin.

It has no path for current to flow, whether the input pin is connected to 5V or 0V (ground).

So no current flows when resistor keeps the voltage at 0V.

When DIP is set ON, only current from 5V flows via resistor to ground.

That is how ideal input pins work. In real life there could be some leakage currents flowing in the order 1 microamps, which for simple beginner circuits can be ignored.

\$\endgroup\$
4
  • \$\begingroup\$ No current flows in or out of the input pin regardless of the state of the dip switch? OK, obviously when the dip switch is open no current can flow. When the switch is closed, the current flows through the resistor to ground - which makes sense to me. But if that is the case, how does the input "sense" that current is flowing through the resistor? When the switch is closed, does the input "feel" a change in current or voltage or something? \$\endgroup\$ Mar 26, 2021 at 15:32
  • \$\begingroup\$ The input works by sensing the voltage at pin. A modern CMOS input pin stage can be simplified as being two FET gates. The voltage at the input pin either turns on a P-FET or N-FET. \$\endgroup\$
    – Justme
    Mar 26, 2021 at 16:08
  • \$\begingroup\$ oh goodness, I don't know what FET gates are. \$\endgroup\$ Mar 26, 2021 at 16:10
  • \$\begingroup\$ I suspected so, otherwise you would not be wondering how the current flows, because it doesn't :) I suggest looking up how a CMOS input stage works, like reading a Wikipedia page on CMOS inverter. It only contains two transistors like I explained. \$\endgroup\$
    – Justme
    Mar 26, 2021 at 16:14
4
\$\begingroup\$

isn't the digital input also a path to ground?

It may be. It may also be a path to Vcc or left floating. Depending on the uC it may support input configuration to be: pull-up (weak or strong), pull-down (weak or strong) or a floating input (which depends on the pin to have a potential defined externally).

This image from (internal pull-down) shows a situation where the external resistor drives the pin up, since R1 << R2, and the button drives it down, since its resistance is almost zero:

enter image description here

Your illustration shows a configuration in which Vcc and GND are switched. If the input pin is configured as floating, you may consider that R2 is so large that practically no current will enter or exit the pin. Hence, any external resistance (R1 or the button) will determine the voltage at the pin.

This post may also be very useful: pull (up/down) resistor

If the input pin is configured as "floating" your diagram would look like this:

enter image description here

This post also has excellent answers on different pin configurations: internal pull-up/down resistors

Yet another example of an input pin configuration with software configurable lower internal resistance: arduino

\$\endgroup\$
4
\$\begingroup\$

The key you are missing in the understanding is the fact that the pins have three states. The states are high state, low state and HIGH IMPEDANCE state. When you have the pins configured as digitalWrite(), the pins can be set to either high or low state with digitalWrite(pin,HIGH) and digitalWrite(pin, LOW) respectively. When the pin is set to HIGH mode, the digital pin acts like conncection to Vcc. When the pin is set to LOW mode, the digital pin acts like conncection to ground and this is what you are talking about.

But when you set the pin to read with digitalRead() function, you set the pin in high impedance mode.

schematic

simulate this circuit – Schematic created using CircuitLab

The above schematic is the illustration of how the Arduino is actually configured. Here, when the input is configured to INPUT state, the 1 GOhm impedance comes into action. Thus there is no path to ground through the digital pin.

How the circuit works then?

Well, this is pretty simple to understand with the concept of HIGH IMPEDANCE state of input pin. When the dip switch is off, there is no current flowing through the 220Ohm resistor. Thus there is no voltage drop across it meaning the node you are checking at has the same potential as the ground.

When the dip switch is on, there is path for the current to flow throught the resistor. Since the input impedance is very high, the current takes the path of 220 Ohm resistor. Now, the node you are checking is at +5V. (I am assuming your red wire is supplying +5V.)

\$\endgroup\$
7
  • \$\begingroup\$ I am sorry for bad schematic before. Now, I have edited it. See if you can understand it. \$\endgroup\$
    – G-aura-V
    Mar 26, 2021 at 15:29
  • \$\begingroup\$ So based on your answer and Justme's answer, it sounds if the switch is closed the current is always going to flow through the resistor and go to ground. That makes sense to me. The input is much higher resistance than my 220 so the current will always take that path. But if no current flows into the Arduino input, what is the input detecting? \$\endgroup\$ Mar 26, 2021 at 15:36
  • \$\begingroup\$ When the dip switch is open it makes sense that voltage and current are (effectively) zero at the Arduino input. When the switch is closed, the current goes through the resistor and out to ground. So current and voltage are present at the resistor, not the Arduino input. What would cause either the voltage or current to change at the Arduino input? \$\endgroup\$ Mar 26, 2021 at 15:38
  • \$\begingroup\$ There is very little current flowing through the high impedance resistor. I=V/R. Plugging in V=5V and R=1GOhm, I= 5nA. The red wire supplies the current required by both resistive paths. The total current through red wire is 5nA + (5V/220). \$\endgroup\$
    – G-aura-V
    Mar 26, 2021 at 15:38
  • \$\begingroup\$ Excuse this very simple question - when the 5v is applied to the circuit does that 5v apply to the whole circuit even though the current will take a single path? When the dip switch in my example is closed do both the 220 resistor and the Arduino input get 5v even though the current flows through the resistor? \$\endgroup\$ Mar 26, 2021 at 15:46

Your Answer

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

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