Explanation please: What is the (pulldown?) resistor doing in this drawing for the Tutorial project, involving turning an LED on/off with a pushbutton switch?
When I hooked this up and ran the sketch on an UNO card it works perfectly.
When I remove the resistor, the LED turns on as my finger approaches the switch, w/o ever pushing the button. Seems like no
way the read pin could be seeing 5V with the switch still open...
The resistor is keeping the input pin LOW except when the switch is closed otherwise the input pin would be floating at an unknown voltage subject to outside influences such as the charge on your body
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);
}
}
An open input on a processor has very high input impedance. Meaning it takes only a very low signal to make it switch from one state to another. Electrical noise from the air will easily send the input to different states.
If you've ever removed the computer plug for your computer speakers, heard the buzz, this is the same concept but instead of the buzz, the input just "flops" around.
You could use the built in pullup resistor activated by using INPUT_PULLUP in pinMode()
Arrange wiring to take the pin LOW when the switch is closed and detect the LOW as indicating that it has closed. It will be HIGH when the switch is open. No external resistor needed. Much neater wiring