1

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?

1
  • 1
    when the button should be off , the value should be 0”: what led you to this assumption? Dec 19, 2017 at 13:08

3 Answers 3

6

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.

1
  • 2
    @J.Doe: The standard way to wire a push button is to connect it between a digital input and ground. If you set the digital input to INPUT_PULLUP mode, the pin will read LOW when the button is pressed and HIGH when it's not. Dec 19, 2017 at 13:20
3

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.

0

This looks as it is operating as wired and programmed. When the button is open, the INPUT_PULLUP is pulling pin 4 to HIGH, and when the button is closed & shorted to pin 2, the digitalWrite(2,HIGH) would also pull the pin 4 to HIGH.

Maybe you want digitalWrite(2,LOW) so that connecting pin 4 to pin 2 through the button would pull pin 4 low.

4
  • But then there is no point in using pin 2 instead of GND. Dec 19, 2017 at 13:21
  • I don't know why Doe did that, but he wasn't doing it correctly. I sometimes provide power or ground through the output pins so I can plug a device directly into a header, or so I can switch the device on or off, but it kind of a sloppy, cheap hack.
    – Dave X
    Dec 19, 2017 at 14:10
  • "digitalWrite" was misspelled as "digitalWtite" here. Not sure why it was rolled back. Nov 26, 2022 at 14:46
  • @Pikamander2 Tiny, trivial edits are discouraged and you added &nbsp at the end. arduino.stackexchange.com/help/privileges/edit
    – Juraj
    Nov 26, 2022 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.