I'm confused about pull-up and pull-down resistors.
Can I just apply 5V to Pin 2 when I want a RISING interrupt. Or do I have to add a pull down resistor? Likewise, do I use a pull-up resistor when I want a FALLING interrupt?
Also, can I always use the internal pull-up resistors instead of hard wiring? When using the internal pull-ups by adding digitalWrite(Pin, HIGH), what happens when the Pin goes low or I write LOW to the pin in my sketch??
Please unconfuse me!
Ok, this is probably best asked on the Hardware forums where those guys know more about this stuff. But you're here, and I got here first, so here goes...
I have found it's best to engage the pull-up resistor whenever possible, and then connect a switch to ground when I want to catch something, so I'm always looking for a FALLING edge.
If you absolutely demand a RISING interupt, then you have to be sure it was 0V before the +5V. So you either switch 0V over to it, or add a pull-down resistor so it's normally at 0V.
If you WRITE the pin LOW in your sketch, presumably you changed it to an OUTPUT now? digitalwrite HIGH to an INPUT is what engages the pull-up resistor.
Can I just apply 5V to Pin 2 when I want a RISING interrupt. Or do I have to add a pull down resistor?
It depends what you are connecting. If it is an active device that always has an output, then no resistor is needed. But if it is a switch that is normally open, then the resistor is needed so that when the switch is open, the value is not just “floating”.
Ditto for a falling interrupt. Basically the objective is, that if you are reading a pin it has a known value on it, not just what is floating around in the air.
Also, can I always use the internal pull-up resistors instead of hard wiring?
It depends a bit on the cable length and what you are doing. For wiring a simple switch, wiring it between the pin and ground, and using the internal pull-up should work fine. However the internal pull-ups are just that - pull-ups. They don’t pull-down.
When using the internal pull-ups by adding digitalWrite(Pin, HIGH), what happens when the Pin goes low or I write LOW to the pin in my sketch??
If you write LOW to it (and it is in input mode) that disables the pull-up. If the pin is in output mode, then it will either be actively sinking current (if you write LOW) to it, or actively sourcing current (if you write HIGH to it).
Instead of Falling, use the internal pullup resistor as noted above, and try a Low interrupt if falling does not give the results you want.
There are no Internal pulldown resistors. Only pullups.
There is a big difference BTW between pulling up and writing HIGH to an output pin. Pulling up is engaging around a 33K resistor between the pin and +5V, so by my calculations that would source around 150 microamps of current. This could easily be overcome by your circuit when it needs to make the pin low.
However if you set the pin to output and write HIGH to it, the chip is going to try hard to push the pin up to 5V, and will be prepared to expend 20 milliamps of current, up to a maximum of 40 milliamps (before damage potentially occurs). Similarly if you write LOW to an output pin, it will try to absorb (sink) 20 milliamps.
Please unconfuse me!
I will give it a shot.
First needing to use or not to use pull-up or pull-down resistors depends on what kind of device or signal you are wiring to an arduino input pin. If you wiring a electrical signal from another electronic active device then there is no need for resistors as the device will supply a active low and high voltage as per it's function. It's when you are dealing with passive components like switch contacts that the pull-up or pull-down requirements come to play.
A switch is a simple two terminal passive device (it doesn't generate a voltage by itself) and lets say one terminal is wired to +5vdc and the other terminal is wired to a arduino digital input pin. So you press the button and the switch contacts close it routes the +5vdc voltage to the input pin and the pin will read as a HIGH if you perform a digitalRead() statement, and will continue to read a high as long as you keep the switch contacts close. Now what happens when you release the switch contacts, what voltage does the input pin 'see' then? It's undefined as there is no valid voltage level being applied. Many newcomers assume for some reason that with no voltage going to a input pin, it should read a LOW, but that is just not the case. That is called a 'floating input pin' condition and reading the pin in software will result in reading random high and low values depending on internal and external circuit electrical noise, the phase of the moon and how hard you stare at the pin. ;) To read out as a LOW there has to be a valid 0 vdc signal wired to the pin.
So pull-up and pull-down resistors are one way to wire a 'default' voltage to a input pin when there is no other electrical signal going to the pin. For our above switch example we need a default 0 vdc signal, so we wire a resistor from the pin to ground and we then have an electrical 0 vdc signal for the pin to read out as a valid LOW when the switch is not being pushed. When we do push the switch the +5vdc is now wired to the pin and one side of the resistor, so now some current flows from ground through the other side of the resistor and on to the +5vdc voltage source, but the input pin only sees the +5vdc side of the resistor so it reads as a HIGH.
Now if we wire our switch contact instead of from a +5vdc source but rather to ground and the other side of the switch to a digital input pin we have the opposite state from above, the input pin will read a valid LOW when we press the button, but not a valid HIGH when we release the switch. That is where we need a pull-up resistor, to provide a valid electrical +5vdc signal when the switch contacts are open. Finally the pull-up can be either a real external resistor or we are free to use a software controlled internal pull-up resistor if we wish. As the internal pull-up resistor is free and takes up no board space it's a better solution. Note however there in no internal pull-down resistor option. That means that to use the internal pull-up resistors for switches we have to always wire the other end of the switch to ground. And it means in our software we have to remember that reading a LOW on such a switched input pin means the switch is being pressed on, and reading a HIGH means the switch is not being pressed, it's off condition. In logic talk that is saying the input signal is an 'active low' signal.
That's enough for now, how did I do.
Thanks to all for the clarification - you all did a great job!
To Crossroads and Nick;
You may recognize the issue from my other post about a cat feeder. I was trying to simulate the Op Amp solution for interrupting when using an analog signal by manually applying 5v (RISING) and removing 5v (FALLING). But now I see I need the resistors to do that, but I won't need them when the active device (Op Amp) is used.
Terrific! Thanks again