Hi,
I know why we use the pull-up/down resistors but my problem is: consider a circuit with only a button and a pull up resistor connected to pin 3 for example. So when we don't press the button the pin will read high, but when we press the button, why does it read low? isn't it still connected to the 5 volts? how does the pin read the voltage, that's my question.
It might be simple but I am a real beginner
Because the connection to ground is a direct short, of much lower resistance than the pullup.
picture is worth a thousand words:
So when we don't press the button the pin will read high, but when we press the button, why does it read low? isn't it still connected to the 5 volts?
No. [u]Here is a schematic of a pull-up resistor and a switch[/u]. One end of the pull-up resistor is connected to 5V and the other end is connected to the input.
The input resistance/impedance of the Arduino is very high so with the switch off (open) virtually no current flows. With no current there can be no voltage dropped across the resistor ([u]Ohm's Law[/u]). i.e. There is 5V (relative to ground) on both ends of the resistor so we have 5V on the input and digitalRead() reads high.
When we turn the switch on, the switch has virtually no resistance. Now current flows through the resistor and switch, and now there is a voltage drop across the resistor. Since the switch has no resistance (when switched-on) there can be no voltage across the switch (Ohm's Law again). So, now the input voltage is zero and digitalRead() reads low.
how does the pin read the voltage, that's my question.
You'd have to study how MOSFETs work. But if you look at the datasheet for the ATmega chip, it says anything greater than 3V (0.6 x Vcc) is read as high and anything less than 1.5V (0.3 x Vcc) is read as low. In-between is "undefined" and it may read high or low.
With appologies to wolframore for plagiarizing his diagram, here's a more in depth expatiation:
If you think of a switch as a two state resistor, then what we have here is a voltage divider -- a very specialized voltage divider. Let me explain.
The "switch", in this case, is a "Push Button", and the two states are:
- Open, with a super large resistance, so large, for most practical purposes, it can be considered infinite.
- Closed, with a very low resistance, on the order of milliohms.
The formula for the voltage across the lower resistor, in a voltage divider [using the component labels in wolframore's diagram] is:
[b]V[sub]SW[/sub] = (R[sub]SW[/sub] * 5V) / (R[sub]SW[/sub] + R)[/b]
[because [b]5V / (R[sub]SW[/sub] + R) =[/b]
the current flowing through the two resistors--for the sake of this discussion, lets ignore the minuscule current on the MCU input, since it has practically no impact, anyway--and that current * lower resistance is the voltage across that lower resistance].
So, when the push button is closed, it behaves like a resistor with only a few milliohms, so lets call it [b]2 mĪ©[/b]
. Plug that into the formula, and you get:
[b]V[sub]SW[/sub] = ([/b]``[b]2 mĪ©[/b]``[b] * 5V) / ([/b]``[b][tt][b]2 mĪ©[/b]
+ 10k) = 1 mV[/b][/tt]
In other words, nearly [b]0V[/b]
.
When the push button is open, it behaves like a really large resistance, so lets call it ā
. Plug that into the formula, and...
[b]V[sub]SW[/sub] = ([/b]``ā``[b] * 5V) / ([/b]``ā
[b] + 10k) = 5V
[/b]
And, of course, [b]V[sub]SW[/sub][/b]
is the voltage that affects the MCU input, and becomes either a HIGH or a LOW in the Arduino sketch.
Aib:
I know why we use the pull-up/down resistors but my problem is: consider a circuit with only a button and a pull up resistor connected to pin 3 for example.
The button that you're talking about is for controlling a switch. And the state of this switch (closed or open) determines the voltage at pin #3.
When the push-button is untouched (ie. a spring mechanism keeps the switch open), then the terminal of the pullup resistor (that is also connected to pin #3) will be at 5 volt. That terminal of the resistor (connected to pin #3) is 5V because NO CURRENT flows through the resistor for this case. This means there's no 'voltage drop' across the resistor. And since the 'other end' of the resistor is connected to a 5V source, then both ends of this resistor will be at 5V (because the voltage drop across the resistor is zero).
But once you manually push the button, and you keep the button pushed in, then the switch will 'close', which then connects the terminal of the resistor (which is also connected to pin #3) to 0V.
OMG I was struggling with this a couple of days but thanks to you I finally got it. Thanks guys.