Need some help with the concepts of pull-ups/downs.

New to electronics and have playing around with things successfully. Now I am trying to understand them. I did the following tutorial which helps to explain pull-ups and pull-downs...

Here is the code used...

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);    
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

...and it worked correctly; the LED would light up when the button was pressed and go out when it was not pressed. But in needing to get a better understanding, I pulled out my multimeter to look at things and got some unexpected readings. Given, I might be taking the readings at the wrong place, but still...

Here is a copy of the actual circuit with a few nodes highlighted followed by the measurements I took.

Measurement 1.

From Node 3 (5v) to Node 2 (Digital 2)
Button not pressed = 5v
Button pressed = 0v

Measurement 2.

From Node 3 (5v) to Node 4 (Ground, after the resistor)
Button not pressed = 5v
Button pressed = 5v

I also noticed that if I moved the jumper wire from Node 3 (5v) to Node 1 it worked the same. But if I moved Node 2 (Digital 2) to Node 1 then it lit up the LED as if I were pressing the button.

So here is my confusion...

I thought that Measurement 1 would be the opposite. The program is set up so that if Digital 2 is high, then the LED is lit. However, it was 5v the whole time until I pressed the button which subsequently dropped the voltage to 0 and lit the LED. I also thought that Measurement 2 would be different as well, and now that I think about it, I might actually see why the voltage was the same. Is it because the voltage always stays the same but the current changes because of the resistor? And if so, then why does voltage "float" and a resistor fixes it when pulling it down?

Also, I googled it, but couldn't find anything conclusive: Why does the LED light up when I moved the jumper wire from Node 2 to Node 1? Is that something specific to 4 button switches? I really haven't been able to figure this one out yet, but the LED is solid on regardless of the switch's position.

Thanks for any help...

On those push buttons with 4 pins each pair of pins is common (node 1 = node 3, node 2 = node 4 in your diagram)

Measuring w.r.t. ground is always going to be less confusing! All voltage measurements are differences in electrical potential (hence the term "potential difference" which is the official term for a voltage). If you name voltages w.r.t. ground (such as the +5V supply), then measure them w.r.t. ground to make you life easy.

So the voltage from node 2 to ground is +5V with the button not pressed, 0V when pressed - go and measure it. Remember input pins act as open-circuits so disconnecting the input pin won't affect the measurements you make at all, just stop the program noticing the button-press.

Because input pins are open circuits they "float" if not connected to anything - this means the voltage on the pin varies according to charged particles in the air, static electricity on nearby insulators, capacitive coupling to nearby signals, pretty much anything. This is why you have to have a pull-up or pull-down resistor - without it the pin would float until the button is pressed, generating HIGH or LOW seemingly at random until the button is pressed.

On the Arduino input pins can be configured with internal pull-up resistors (value about 40k) if so desired - search these forums for details.

Thanks for the reply. I did lol a bit when you said "remember that..." I am far more noob than that. I have to learn it before I can remember it. But I did learn a lot from your post. Thank you. Taking measurements from ground will definitely help in the future as it essentially gives a standardized result.

If you use those pins it is a good idea to use a lower value of pull up or down resistor 4K7 is a good value it will source or sink 1 mA of current so an accidental pin change isn't going to draw much current.
Nor will the pin be as sensitive to noise and stray electrical fields. That kind of 'accidental' pickup has bitten me several times... thus the low value of resistor. I want to know... that a pin is high or low.
I use 470 ohm resistors for my terminator resistors. If I make an error in a sketch it's only 10 mA 1/4 of the pin 's current source/sink capability and I see it on the amp-meter I use to monitor my projects current drain with.

Doc

I do "accidentally on purpose" pin changes all the time. I take it I should power down my Arduino before doing so? Also, I am assuming that 4k7 = 4700 Ohm resistor. If this is true, wouldn't a 10k resistor be better at reducing accidental pickup? I don't know the answer here, just posing the question.

Also, what is a "terminator resistor"? Is it just a resistor inline with an Arduino i/o pin?

No on both points it would take 1/2 the "induced" voltage to cause equal interference with a 10 K resistor as it would with a 4IK7 resistor it will source or sink .5mA, a 4K7 will S/S 1 mA and a 470R will S/S 10 mA. The more current (to a point) pulled up or down the better conditioned the port is to outside interference, especially if you connect long (more than 6 - 8 in) wires to the ports in question. A "terminator is any connection to a port pin to determine it's function and If there are either unused pins or long wires connected (Short ones too) to pins used for indication from or to external devices they should be "Terminated" properly connected and pulled up or down depending on pin function. in short pull-ups or downs are terminators...

Doc

Also, what is a "terminator resistor"? Is it just a resistor inline with an Arduino i/o pin?

No not in line.
It just looks like a pull up or pull down resistor, sometimes even both. However, it's function is to match the line impedance to minimise reflections of a signal. In practice this only matters with long lines and high speed signals.
Any impedance discontinuity reflects a signal, if you were to look at that reflection and time how long it took you can work out where the discontinuity is. This is use in cables so that they know roughly where to dig up the street, or on your phone lines to see where the fault is.

For information about inputs see:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Mike it also serves to lower the impedance of the "Termination" so that the "influences" of "Strays" is minimized. If the output is supplying 1 mA (4K7/5V)it takes 1mA + the "stray" to influence or change the voltage at that point... If the current is .5 mA (10K/5V) it takes 1/2mA + the "Stray" to "influence" the measured point... Thus the reason to use as low a value of resistor as is possible... this is commonly done in "Hostile" or EMI rich environments to lower or prevent induced noise from other sources as well as to terminate a line in its correct load impedance. an example is the resistors used to "terminate" an RS485 link, any two resistors that will "Split" the reference (Vcc) voltage can be used... the Bus operates @ Vcc/2... So with ideal components 2 1M resistors could be used... But the bus wouldn't work real well in a noisy or long wire run environment.

Doc

To terminate a transmission line the resistor (or resistor network) must have a resistance close to the line's characteristic impedance, these values are between 50 and 200 ohms for most practical transmission lines (50 ohm for coax, 100 to 150 for twisted pair, 120 to 200 for untwisted pair roughly). When using transmission lines the terminators are mainly to prevent reflected energy messing up high speed signals. A transmission line can be far longer than an unmatched signal line for the same performance - for instance all RF signals are sent down transmission lines.

Most pull ups and pull downs are not terminators in this sense - the reason logic lines should be kept short (6 inches or so) is to keep the propagation time down the line shorter than the logic levels rise/fall times, so that reflections aren't a problem. If the line is a properly terminated transmission line it can be metres (or even km) long (see the RS485 specifications for instance).

You may also notice that a characteristic impedance of 100 ohms or so at 5V is very power hungry (50mA per signal line!).

Unfortunately this thread took a left turn when I used very loosely the word Termination. What I meant was that any 'input' whether used or not should have a resistor, value as low as practible... connected from that input either to Vcc or Ground as required. a foot of wire connected to an input port with a 10K pull-down makes a formidable antenna, using large 'filter' capacitors .1uF works, But there is a finite time required for their charge, discharge and as such may well create another problem, after fixing one... Longish runs of SPI or 2 wire can be issues for noise pickup/radiation and although a low value 'might' create a slightly larger EMI issue it is offset by the same resistor values that create it. My observations about the 'relative' impedances of I/O terminated @ 10K 4K7 and 470R and the required current for all are still to my way of thinking and experience valid, I've Been There Done that...
I once had a board where there was communication between two processors and one of them caused a relay to pull in... When that happened the Comm was lost (2 wire) and the processor WDT was the only way I found out... the device would display the initial power up message... and the transmitter was for all intent and purpose "Locked on" until I or the test tech's did a master reset.
The relay coil was powered from Raw 12Volts (about 16 - 18V and a resistor/cap Snubber and diode clamp for back EMF. Everything else was powered from a switcher and or a linear reg connected to the switcher. The issue turned out to be a magnetic pulse induced into the 2 wire bus leads... 3 inches away. The fix was easy once the issue was determined... 1K pull-ups on either end of the 2 wire bus forcing 10 mA current instead of the Mfr's 4K7 pull-ups on the slave end of the two devices. Didn't need to move or shield the relay coil.
I used to have a mosfet with a 4" square piece of copper screening attached to the gate and a BIG Red Led/resistor in the drain circuit, connected to my main power supply on my bench and many times that warning was enough for me to prevent damage to cmos equipment being developed or repaired. When I got near my seat If it was dry that led would light and give me a chance to touch something grounded before I did anything else.
The two issues are very similar and to finally end this chapter... using the word 'connect' instead of 'terminate' seemed somehow out of place... I wanted to get across the idea of creating a real "known state" for Arduino I/O, used or unused... If the issues are interference/erratic or improper intermittent operation a "loose" port pin might well be an issue...
After All that technique provides the Seed value to "randomize" Random()... Put a 470R resistor from the Analog pin in question to ground and then note the first 10 Random() values on three runs... Bet they are all the same. In My Direct experience... not an opinion... for Me.

Doc

I wanted to get across the idea of creating a real "known state" for Arduino I/O, used or unused... If the issues are interference/erratic or improper intermittent operation a "loose" port pin might well be an issue...

I just question the need of a pullup/down or termination of a input pin that is not being used nor has anything wired to it. AVR datasheet does not carry such a warning or recommendation as best I recall. I mean sure if the whole device is placed in the near field of a kilowatt RF ham linear amplifier, most every possible filtering method might have to be employed. But for 'normal' hobbyist usage do you feel that unused input pin actually require something to be done to them?

If indeed unused and unwired I/O pins required something to be done, why not simply set them as output pins and set them as low outputs, that would place the pin into a pretty low impedance path to ground without requiring any external components?

Lefty

About 5 chapters back in this thread I mentioned that same point... when I mentioned inputs not properly dealt with in code... Also I think my comments on SPI and 2 wire are relevant as well as my $.02 on wires of more than 6 inches and the 'possible' issues and what I would do to make sure that regardless of code they can not be an issue, it's easy to make a compliable error there and spend hours trying to 'Fix' a code error electronically. Evidence my comments about the source of the random seed and what that implies for any wire digital or analog that isn't driven appropriately. Because a design works doesn't mean the design is either good or robust, All it means is that it doesn't fail under the immediate testing conditions All I can say is that when I recommend something it has either been part of a working production device I made... or it was a required 'Fix' because I took a 'shortcut' and didn't test it my self... Prior to trying to write code for it. This is the way I work and what I design usually works as well as I designed it and no more. I haven't made all the mistakes I ever will But I have learned some techniques that when followed religiously cause the devices that I design to work better... usually. That's the reason for some of my suggestions. In the end Every one has an opinion about everything they do and if it doesn't work well maybe the opinion needs some revision. The method isn't really important so much as the integrity of the whole... Sometimes a perfectly great idea can be made worthless simply because of an inappropriate method of execution, the best of course do the best with the least anywhere it needs to be done... I just wish I was that good.

Doc

Unconnected inputs - three problems I know of with CMOS are:

  1. shoot-through in the input stage because both FETs are on - typically in unprotected CMOS logic this would cause upto a few mA of wasted current - prohibitive for battery operation when the circuit sleeps. Thus unconnected inputs should always be strapped to one of the rails.

  2. oscillation - CMOS inverter is a high gain amplifier, coupling from input to output can lead to oscillation if the input get biased in the conducting region (this is how CMOS Xtal oscillators work for example). Again this can lead to unwanted power dissipation and also noise-injection into nearby circuitry.

  3. noise pickup - of course in an unwanted input this doesn't really matter. If there's a PCB trace on the pin it will pickup nearby signals capacitively - if a fast clock signal then the input pin will again be dissipating power unnecessarily.

The AVR microcontrollers seem to have a simple mechanism to reduce the severity of these issues - the inputs have a small amount of hysteresis. I think this is enough to prevent the first two problems noticeably.

However if you want to guarantee no wasted power due to open inputs I'd suggest either adding pull-ups/pull-downs or enabling internal pull-ups. Since a microcontroller pin can be configured as both an input and an output I would not recommend strapping the pin directly to GND or Vdd.

Docedison:
Unfortunately this thread took a left turn when I used very loosely the word Termination.

Being a noob at this, I would say I have learned a lot from this thread's unfortunate left turn. I don't quite understand it all, but seriously appreciate the bantering/discussion.

Well Young Man, It has been my experience that any reasonable education will come in handy, sooner or later... My biggest fear is that I will forget which answers the questions are for... Think about it. My Very last rant on pull downs, I promise other that to question once in a while... Is that I take a different approach... Kill 'em All and let no one but me.. When I am doing something new I usually use 470 ohm "terminators or pull up/down's... I never really worry about current drawn, I use op-amp Voltage followers on analog measurements and when I am through I start the real engineering and that is to remove components until it's operation is either departing from the design spec's or has flat quit... Put that part back and then test it and try to break it... whatever you can think of that it might see in use... If it works good it's good enough to field test... Maybe many times I have had a piece of equipment fail miserably in the field after heat and cold testing both heat soaked and transitioning between hot and cold... Just roll over and die from something I thought could never happen... about 90% of the time. Granted many seem really out there... as far as time and parts, knowledge and experience... have taught me to remember that "Murphy Was an Optimist". What I recommend here worked for me or I wouldn't advocate it. However there is one real final teacher that few of us know to heed regardless of exposure... Pain is the real teacher... It's the one we knee jerk remember rather than the intellectual or reasoning parts we like to think we use...did you ever wonder why people stick their fingers in light sockets... at least once? it's simply because they have been told not to... By someone who was told not to... Why would I want to go to the trouble of finding one to begin with?
Actually that isn't really fair however it might be true... we do things like that because we are constantly testing our boundaries... That's how we learn.
I can't remember but one board I ever produced that went into production with anything as small as 470R But I do use them until I know that the noisy thing I am concerned about is under control.

I use them and the lesson that taught me their use went out with spare pins tied high or low. It was also the only project I ever put in a nema plastic enclosure... Steel after that. Again I've wasted a perfectly good space somewhere with my blathering... IT Was Fun though. IMO

Doc