How and why do buttons work.

So I'm trying to use a button... Seems easy, but I don't quite understand the principle.
I followed this picture: File:Pullup Resistor.png - Wikipedia

So, Vin is 3.3V, how much is Vout? (Arduino due digital input pin)
It can't be 0V, because current would run through the logic gate at all times and it can't be 3.3V, because if it was, why bother having the other 3.3V (Vin) input if you can just connect the logic gate and GND to a switch and you're done.

Why can't you just connect a switch to 3.3V, a resistor and an input pin?

Then there is this: http://arduino.cc/en/Tutorial/DigitalReadSerial
although I suppose the question is about the same thing.

Why do you need GND? Wouldn't the input pin have to be 0V in order for the current to be drawn there?

Someone please explain how and why this works, thank you - whoever you are.

Edit: and what is the difference between the placement of the resistors?

TheChosenHalf:
So, Vin is 3.3V, how much is Vout? (Arduino due digital input pin)

The input pin can only measure HIGH and LOW.

TheChosenHalf:
It can't be 0V, because current would run through the logic gate at all times and it can't be 3.3V, because if it was, why bother having the other 3.3V (Vin) input if you can just connect the logic gate and GND to a switch and you're done.

Logic gates have to be connected to something or they 'float' and give random results.

PS: Arduinos have pullup resistors built into the chips, no need to add any extras. Just initialize the pin properly:

pinMode(pin,INPUT);
digitalWrite(pin,HIGH);  // Enable pullup resistor

It boils down to this: an unconnected input is not in a guaranteed state. When you put 3.3v or 5v on an input, it's clearly at 3.3 or 5v; so far so good.... but just because it's not at 3.3 or 5v, it doesn't mean it's at 0v: there are all sorts of electrical signals in the air, and an unconnected pin may (will) pick up those stray signals and give random readings.

So, if your switch is there to take a pin to 3.3 or 5, then you need to connect it to 0v the rest of the time so it gives a guaranteed reading. You are pulling the pin down. Conversely, if your switch is there to force the pin to 0v, then you need to pull it up to 3.3 or 5v the rest of the time.

Why use a resistor for the pull-down (or -up) not just a piece of wire? Well if you look at the schematic here, you'll see that without the resistor, when the switch is closed, the 5v will be shorted to 0v. That will cause sparks and smoke to be released....

The Arduino inputs pins use no current (I repeat: no current). They kind of 'sense' the input either being 0V (LOW) or 3.3V (HIGH).

fungus writes about the internal pullup resistor. That would be the most simple way. Enable that internal pullup resistor, connect a button to the input and GND, and you are done.

Enabling the input resistor can be done with the pinMode function like this:

pinMode( pinButton, INPUT_PULLUP);

Why does connecting the pin to GND mean it's guaranteed, if the wire picks up a signal, shouldn't it go both ways? From what I understand, you're saying that the current prefers GND over the input pin, why?

Basically these are the only 2 possibilities, correct?
Imgur

Yes, those are the two options.

However, there is a pullup resistor inside the microcontroller to 3.3V as I wrote.

Please forget the 'current'.
The input pin does not use current. Think of as a voltage meter. I reads the voltage at the input.
When the button is not pressed, the resistor pulls the voltage to 3.3V or GND, whatever the resistor is connected to.
The resistance of the button is perhaps 0.1 ohm. So when the button is pressed, the button wins and the voltage is pulled to whatever the button is connected ot.

But in theory, a minimal proportion of the total current does go to the input pin, it's just small enough to be negated?

A very tiny amount of current does flow in/out of the input pin. Its resistance is in the range of 100s of megaohms, but that is not to ground or Vcc, but somewhere in between. Hence, it floats around, and just static electricity will affect it.

You probably have no idea how much static electricity is generated at any moment. Lift your arm - you just generated static electricity. Any time a nonconductor touches or stops touching another nonconductor or conductor, a charge is generated. The amount of charge depends on the triboelectric characteristics of the two objects, and the voltage depends on the distance they are separated. More distance, higher voltage.

And just about nothing is made of only one material, so your mixed cotton/poly blend shirt generates a charge by touching itself. Hey, chew some Orbits gum, dirty mind.

I see, thanks for the help everyone :stuck_out_tongue:

TheChosenHalf:
But in theory, a minimal proportion of the total current does go to the input pin, it's just small enough to be negated?

Its about 9 orders of magnitude less current than goes through the resistor, on the scale of picoamps at
room temperature - to all intents and purposes this is zero current.

We are talking about CMOS logic here, and the O in CMOS means oxide, specifically silicon dioxide from which
the gate insulation is made - this is quartz basically, an exceptionally good insulator, between the input and the
rest of the circuitry. The actual leakage current on inputs is mainly from the reverse biased protection diodes.

[ I assume "negated" means "neglected" ]

Doesn't seem to be working:
Imgur

R1 is 11kOhm, also tried 240kOhm.

BUTTON_1, 2 and 3 are pins 44, 41, 36, I set them all to input.

if(digitalRead(BUTTON_1) == HIGH) {
    scale = 0;
  }
  else if(digitalRead(BUTTON_2) == HIGH) {
    scale = 1;
  }
  else if(digitalRead(BUTTON_3) == HIGH) {
    scale = 2;
  }

What do you mean by Vout and Vin?

Please show clearly the Input pins and your +ve and -ve connections.

If I take it that Vin is your +5v rail, that the left hand side of each drawn switch is one terminal and the right hand side another, then you are trying to use a single resistor as a pull down.

Think about what will happen when you press any one of those switches...

...all of your inputs will go high via the common connection to that resistor.

You need a separate resistor for each switch.

Generally, it's easier to have default state of input as +5v, via a pull-UP (you can use the internal pull-up then), and the switches to Gnd. The ON state of your switch is then when you close the switch and have a LOW signal to the pin.

Ok, now I have 3 resistors, each between a button and GND.

Vout is the input pin, Vin is +3.3V (arduino due).

Still nothing :confused:

Edit: wait... wait... It helps to connect GND to.... GND. It works now, thank you.

http://opencircuitdesign.com/xcircuit/goodschem/goodschem.html