New to electronics and have playing around with things successfully. Now I am trying to understand them. I did the following tutorial which helps to explain pull-ups and pull-downs...
Here is the code used...
// constants won't change. They're used here to
// set pin numbers:
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);
}
}
...and it worked correctly; the LED would light up when the button was pressed and go out when it was not pressed. But in needing to get a better understanding, I pulled out my multimeter to look at things and got some unexpected readings. Given, I might be taking the readings at the wrong place, but still...
Here is a copy of the actual circuit with a few nodes highlighted followed by the measurements I took.
Measurement 1.
From Node 3 (5v) to Node 2 (Digital 2)
Button not pressed = 5v
Button pressed = 0v
Measurement 2.
From Node 3 (5v) to Node 4 (Ground, after the resistor)
Button not pressed = 5v
Button pressed = 5v
I also noticed that if I moved the jumper wire from Node 3 (5v) to Node 1 it worked the same. But if I moved Node 2 (Digital 2) to Node 1 then it lit up the LED as if I were pressing the button.
So here is my confusion...
I thought that Measurement 1 would be the opposite. The program is set up so that if Digital 2 is high, then the LED is lit. However, it was 5v the whole time until I pressed the button which subsequently dropped the voltage to 0 and lit the LED. I also thought that Measurement 2 would be different as well, and now that I think about it, I might actually see why the voltage was the same. Is it because the voltage always stays the same but the current changes because of the resistor? And if so, then why does voltage "float" and a resistor fixes it when pulling it down?
Also, I googled it, but couldn't find anything conclusive: Why does the LED light up when I moved the jumper wire from Node 2 to Node 1? Is that something specific to 4 button switches? I really haven't been able to figure this one out yet, but the LED is solid on regardless of the switch's position.
Thanks for any help...