user48726: 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?

user37622: There are to simple options how to connect a simple button. You mixed them.
Better option is connect the button between pin with INPUT_PULLUP and ground. The pin is HIGH when the button is open and LOW when the button grounds the pin. This option is better because if you disconnect the button, the pin is still pulled up to HIGH and not floating.
If you connect the button with a pull-down circuit, then the pin is HIGH when the button is pressed and LOW when the button is open, but when you disconnect the circuit, the pin floats and the sketch reads chaos.

user27653: You have set Pin 2 to HIGH, which is 5V. You have set Pin 4 to INPUT_PULLUP, which means if no signal is driving it, it will be HIGH.
You have connected a button between Pins 2 and 4. So, if you don't press the button, the input is pulled HIGH. And if you do press the button, Pin 2 is driving it HIGH.

48726: <p>I am new to arduino programming and I have bought arduino UNO a few days ago.</p>

<p>I am trying to read the value of pin 4 using <code>digitalRead</code> with pinMode INPUT_PULLUP but it keeps giving me 1 even when the pin 4 is not connected to anything.</p>

<p>Here is the code:</p>

<pre><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);
}
</code></pre>

<p>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.</p>

<p>On the other way , if I replace <code>INPUT_PULLUP</code> with <code>INPUT</code> then the value is floating.</p>

<p>Why the value with INPUT_PULLUP is 1?</p>
 37622: <p>There are to simple options how to connect a simple button. You mixed them.</p>

<p>Better option is connect the button between pin with INPUT_PULLUP and ground. The pin is HIGH when the button is open and LOW when the button grounds the pin. This option is better because if you disconnect the button, the pin is still pulled up to HIGH and not floating.</p>

<p>If you connect the <a href="https://www.arduino.cc/en/Tutorial/Button" rel="nofollow noreferrer">button with a pull-down circuit</a>, then the pin is HIGH when the button is pressed and LOW when the button is open, but when you disconnect the circuit, the pin floats and the sketch reads chaos.</p>
 27653: <p>You have set Pin 2 to <code>HIGH</code>, which is 5V. You have set Pin 4 to <code>INPUT_PULLUP</code>, which means if no signal is driving it, it will be <code>HIGH</code>.</p>

<p>You have connected a button between Pins 2 and 4. So, if you don't press the button, the input is pulled <code>HIGH</code>. And if you do press the button, Pin 2 is driving it <code>HIGH</code>.</p>
