Arduino:Reading Digital Inputs with Pulldown Resistors
Posted by Unknown on Friday, July 11, 2014 with No comments
All digital inputs use a pullup or pulldown resistor to set the “default state” of the input pin. Imagine the circuit in Figure below without the 10kΩ resistor. In this scenario, the pin would obviously read a high value when the button is pressed.But, what happens when the button is not being pressed? In that scenario, the input pin you would be reading is essentially connected to nothing—the input pin is said to be “floating.” And because the pin is not physically connected to 0V or 5V, reading it could cause unexpected results as electrical noise on nearby pins causes its value to fluctuate between high and low. To remedy this, the pulldown resistor is installed as shown in Figure .
Now, the current has two options:
■It can flow through a nearly zero resistance path to the 5V rail.
■It can flow through a high resistance path to the ground rail
Now, consider what happens when the button is not pressed with the pulldown resistor in the circuit: The input pin will be connected through a 10kΩ resistor to ground. While the resistor will restrict the flow of current, there is still enough current flow to ensure that the input pin will read a low logic value. 10kΩ is a fairly common pulldown resistor value. Larger values are said to be weak pulldowns because it easier to overcome them, and smaller resistor values are said to be strong pulldowns because it is easier for more current to flow through them to ground. When the button is pressed, the input pin is directly connected to 5V through the button.
■It can flow through a nearly zero resistance path to the 5V rail.
■It can flow through a high resistance path to the ground rail
Pulldown and pullup resistors are important because they ensure that the button does not create a short circuit between 5V and ground when pressed and that the input pin is never left in a floating state.
Sketch
simple LED control using push button
const int LED=9; //The LED is connected to pin 9
const int BUTTON=2; //The Button is connected to pin 2
void setup()
{
pinMode (LED, OUTPUT); //Set the LED pin as an output
pinMode (BUTTON, INPUT); //Set button as input
}
void loop()
{
if (digitalRead(BUTTON) == LOW)
{
digitalWrite(LED, LOW);
}
else
{
digitalWrite(LED, HIGH);
}
}
Categories: Arduino
0 comments:
Post a Comment