Ok, I had a heck of a time understanding the hows and whys of pull up/down resistors. I understand why it is needed, but there is one thing bugging me.
I don't quite understand the state of a pin when it is set to INPUT. When a pin is set as OUTPUT, I know that it is either HIGH (5V) or LOW (0V). What is happening to the pin when it is set as INPUT?
The usual example of pull up resistors is using a simple push button which is connected to ground and a pull up resistor connected to Vcc. What I don't understand is the flow of electrons in this setup.
When the button is not pressed the INPUT pin is now connected to Vcc (5v), which would make me think the pin would have to be 0v so that the electrons can flow from 0v to 5v. This seems to be the case, because I read that the resistor is required due to ground being directly connected to Vcc (a short).
However, when the button is pressed and the ground connection is made to the input pin, I would expect the INPUT pin to be 5V for the current to flow from ground to the INPUT pin. It doesn't seem logical that a ground to ground connected would outweigh a ground to Vcc circuit (via pull up resistor).
I am very new to electronics and what I know is what I have read on allaboutcircuits.com so far, so forgive my ignorance. Could someone please help me see the full picture here?
After looking at this, I realize that what we are doing here is actually bypassing the input pin all together when the switch is closed, hence giving a reading of 0v to the input pin.
So does this mean that a pin is set to LOW (0v) when it is set as INPUT?
So does this mean that a pin is set to LOW (0v) when it is set as INPUT?
The input circuitry for a digital input pin is a very sensitive 'voltage monitoring' circuit and has an input series resistance (impedance actually) of many megohms of resistance. Almost zero current flows into or out of a input in no matter if it's wired to +5vdc or ground.
The problem of this very high input resistance is when there is nothing external wired to the pin. This then becomes a very sensitive antenna like circuit and the input pin cannot tell for sure what state to be in as normal internal circuit noise will make it randomly read as low or high or keep switching back and forth with the noise.
So there is no true 'default' logic input state for a input pin if nothing is wired to it. It's simply unpredictable to know if the pin will report as a high or a low when you do a digitalRead() statement in you program.
Because of this very high input impedance can be a challenge for some passive external circuitry like switches, the AVR chip has an optional internal pull-up resistor of around 40k ohms that your software can turn on, if you desire. That allows you to wire a simple switch without using any external pullup or pulldown resistor. Just wire the switch between the pin and ground, when the switch is open the input pin will 'see' the internal 40k resistor wired to +5vdc (HIGH) and when the switch is closed it will ground that 40k resistor and the input pin will 'see' the ground, or digital low, condition. In your software you must then associate a low reading as switch pressed and a high reading as switch released.
So there is no true 'default' logic input state for a input pin if nothing is wired to it.
Such inputs are said to be "floating". They are allowed on an Arduino, as long as you realize that if you were to read the input, what you got would be meaningless.
With some other circuits... CMOS chips, for example, I believe... it is Not A Good Idea to have any floating inputs, even if you don't care what value appears on that pin. In such cases, you merely attach the pin to 0v or Vcc, to stop it "floating".
===
And....
And, of course, the impedance of the switch is zero for all practical purposes, pulling the pin node down to 0.
When you close the switch, yes, current flows through the resistor, but the more important thing to focus on is that you have now directly connected the input to 0v... so it SHOULD read "0", shouldn't it! The clever thing about using the resistor the way it is used is that you never have an "unconnected" state as you change from CONNECTED to 0v (momentary switch closed) to AS GOOD AS connected to 5v when the switch is opened. Yes, there is a resistor between the 5v and the input, but so little current flows that the input "sees" the 5v, "doesn't notice" the resistor.
With some other circuits... CMOS chips, for example, I believe... it is Not A Good Idea to have any floating inputs, even if you don't care what value appears on that pin. In such cases, you merely attach the pin to 0v or Vcc, to stop it "floating".
I recall playing with some of the early cmos logic chips available in the 70-80s. If you did not tie down or up all the unused inputs the chip would literally burn up due to oscillation. I burnt a few finger tips learning that lesson. ;D
Thank you all for your insight. I still do not understand 100%, but I understand 95% of it, which is enough to keep me out of trouble ;D
The input pin is sensing the voltage at that point where the resistor connects to the switch. That node is 5V when the button is open, and 0V when the button is closed.
I don't quite understand why the circuit would be 0v where the resistors connects to the switch while the button is closed. I would assume that the circuit would be 5v from Vcc to GND. Maybe this is the wrong form for that type of question though. It would seem my understanding of basic electronics is not where it should be.
When the switch is open the input is connected to 5V through the resistor. It takes so little current that there is no noticeable voltage drop across this resistor so the input "sees" 5V.
When the switch is closed the input is connected directly to ground and so "sees" zero volts. At the same time current flows through the resistor. The size of this current is determined by the resistor value and the 5V (ohms law) and plays no part in powering the input pin.