I am new to arduino programming and I have bought arduino UNO a few days ago.
I am trying to read the value of pin 4 using digitalRead
with pinMode INPUT_PULLUP but it keeps giving me 1 even when the pin 4 is not connected to anything.
Here is the code:
void setup() {
pinMode(2,OUTPUT);
pinMode(4,INPUT_PULLUP);
Serial.begin(9600);
digitalWrite(2,HIGH);
}
void loop() {
Serial.print(digitalRead(4));
Serial.print('\n');
delay(150);
}
Pin 2 is connected to one side of the button and Pin 4 is connected to other side of the button. According to condition, when the button should be off , the value should be 0 and when the button should be on , the value should be 1 , but the value is 1 even when I press the button is off or on.
On the other way , if I replace INPUT_PULLUP
with INPUT
then the value is floating.
Why the value with INPUT_PULLUP is 1?