False INPUT_PULLUP values

So I was doing the “DigitalInputPullup” tutorial when I thought I noticed a logic error. Basically, I had

if (sensorVal == LOW) {
   digitalWrite(13, HIGH);
 } else {
   digitalWrite(13, LOW);
 }

and even when the button wasn’t pressed, which is when the value should have equaled high due to the INPUT_PULLUP assignment, the LED was still on. Am I missing something here, or do I have a faulty board?

DigitalInputPullup.ino (1.29 KB)

Did you open Tools > Serial Monitor to see what the button reading was?

Depending on how your LED is wired, it could be active low. This would mean that

digitalWrite(13, LOW);

turns the LED on and

digitalWrite(13, HIGH);

turns the LED off.

Unless your sketch is longer than the forum’s maximum length(which yours is definitely not), please post the sketch instead of attaching it.

Please USE CODE TAGS(</> button on the toolbar) whenever you post code or error/warning messages on the forum. This will ensure that your code is properly formatted and make it easy for us to read and copy it to an editor or IDE.

// Keep in mind the pullup means the pushbutton's
// logic is inverted. It goes HIGH when it's open,
// and LOW when it's pressed. Turn on pin 13 when the
// button's pressed, and off when it's not:

Pin pullups / pulldowns do not invert logic, the way the pin is wired determines that, is your button wired between pin 2 and GND?
Pull ups / downs force the pin to either a HIGH or LOW level when the switch is open so it's not "floating" between states.
If your button is wired from 5V to pin 2 and pin 2 is pulled UP it will be HIGH whether the button is pushed or not.

Huge variety of things that can be going on. Maybe it's hardware - you have wires connected to the wrong pins, but what's equally likely is that your code is wrong and this snippet that you have given us never gets executed at all.

True:
The LED may be wired "upside down", anode to +5V and cathode to output pin, ON when LOW.
Of course "upside down" was the "standard" way with old timey TTL.

That's because old timey TTL had shi* for driving source current, all the drive was pulling pins low.

I still remember 12 inch square boards shingled with 74 series DIPs, sprinkled liberally with little blue tantalum caps that required a 10 Amp PS and a fan or 2.

stephend43:
Am I missing something here, or do I have a faulty board?

Faulty wiring, more likely. Can you post a photo?

So when I set the pinmode for pin 2 as INPUT_PULLUP or as INPUT, the results are the same. When the button is not being pushed, or a LOW reading, the serial monitor displays 0, and the LED is not on, and when it is pushed, the serial monitor displays 1 and the LED is lit. The code I posted above is the code I am using.

stephend43:
So when I set the pinmode for pin 2 as INPUT_PULLUP or as INPUT, the results are the same. When the button is not being pushed, or a LOW reading, the serial monitor displays 0, and the LED is not on, and when it is pushed, the serial monitor displays 1 and the LED is lit. The code I posted above is the code I am using.

If the digital read off the pin is 0 when the button is not being pushed, then that means it is being pulled down to ground.

If you use the normal pull-up way of wiring things … could it be that your pushbutton happens to be a normally-closed type? You can check it with a multimeter, or just connect a LED to it and see what you get.

Hi,
What switch are you using, if a tactile press button with 4 pins, you may have it wired wrong, which is easily done with these press buttons.

Do you have a DMM to measure the voltage going to the arduino input?

Tom… :slight_smile:

If you have the button wired between pin 2 and ground, it must be a normally closed button, how many terminals does it have?

stephend43:
So I was doing the "DigitalInputPullup" tutorial when I thought I noticed a logic error. Basically, I had

if (sensorVal == LOW) {

digitalWrite(13, HIGH);
} else {
  digitalWrite(13, LOW);
}



and even when the button wasn't pressed, which is when the value should have equaled high due to the INPUT_PULLUP assignment, the LED was still on. Am I missing something here, or do I have a faulty board? 
![](https://mail.google.com/mail/u/0/?ui=2&ik=6f26f4bc61&view=att&th=157cb09d2bcc5471&attid=0.1&disp=safe&realattid=1548306550252109824-local0&zw)

Take short look at the schematic of your processor or read the description how on board LED 13 is wired.
Pulling the LED output pin low will turn it on. Find out why.

How is your input pin wired and coded has no relation on how the output ( LED) pin is wired.
Also including your code here is just "make busy" exercise for those who cannot read.

You did an excellent job describing your issue and that what is important, kudos.

Jim

TomGeorge:
What switch are you using, if a tactile press button with 4 pins, you may have it wired wrong, which is easily done with these press buttons.

Yeah, this is the most likely guess. I hate those frigging little things. OP - spin your button by 90 degrees and see if that fixes all your problems.

No - hang on - didn't he say that his input was HIGH when the button was pressed? That wouldn't be happening if this was the problem.

@julyjim - that's another possibility: the OP has put his button on pin 13. … no, his sketch has it on pin 2. Maybe he wired it onto 13 anyway, hence the weirdness. No … that aint it ether. He is printing out the values he gets from reading the button, and they seem to be varying when he presses the button.

stephend43:
The code I posted above is the code I am using.

No, it does not look like it. No setup, no loop. Now it may be in the attached code but I can’t open it as I’m on a tablet. Please include your complete code inside a post; it’s small enough.

Here is the OP's attached code

/*
 Input Pullup Serial

 This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a
 digital input on pin 2 and prints the results to the serial monitor.

 The circuit:
 * Momentary switch attached from pin 2 to ground
 * Built-in LED on pin 13

 Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
 20K-ohm resistor is pulled to 5V. This configuration causes the input to
 read HIGH when the switch is open, and LOW when it is closed.

 created 14 March 2012
 by Scott Fitzgerald

 http://www.arduino.cc/en/Tutorial/InputPullupSerial

 This example code is in the public domain

 */
const int button =2;
void setup() {
  //start serial connection
  Serial.begin(9600);
  //configure pin2 as an input and enable the internal pull-up resistor
  pinMode(button, INPUT_PULLUP);
  pinMode(13, OUTPUT);

}

void loop() {
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(button);
  //print out the value of the pushbutton
  Serial.println(sensorVal);

  // Keep in mind the pullup means the pushbutton's
  // logic is inverted. It goes HIGH when it's open,
  // and LOW when it's pressed. Turn on pin 13 when the
  // button's pressed, and off when it's not:
  if (sensorVal == LOW) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }
}

outsider:
If you have the button wired between pin 2 and ground, it must be a normally closed button, how many terminals does it have?

4 terminals

julyjim:
Take short look at the schematic of your processor or read the description how on board LED 13 is wired.
Pulling the LED output pin low will turn it on. Find out why.

How is your input pin wired and coded has no relation on how the output ( LED) pin is wired.
Also including your code here is just "make busy" exercise for those who cannot read.

You did an excellent job describing your issue and that what is important, kudos.

Jim

So I read over the page or so on digital inputpullup and saw what you were referring to how pin 13 will register as low no matter what and for this example an external pull down resistor is needed.

However, the same exact issues happen when I use any of the other pins as well

When generic I/O ( input / output ) pin is used in INPUT mode it is internally , inside processor chip, "left open / floating" meaning the internal input state is undetermined. At this point there is no need to go to explanations why is that, maybe later.
When you connect one side of your button to this INPUT pin and the other to ground, assuming your button is of "NO" normally open variety, when your software reads the INPUT pin its voltage can be anywhere between 0 and 5 V, hence you cannot be sure about its state.

When you set the mode to INPUT_PULLUP the input pin gets connected internally to 5V or 3.3V depending on processor. Now the input is preset to HIGH state and when your software reads it will always read HIGH. No more "maybe" state.

These are main reasons why to use pull - ups and active ground, there are others, one of them being that signal is idle at voltage and active at ground. Voltage in general can be in wide range, but "ground is ground all way around".
End of lesson. There will be test tomorrow , but no homework.

Jim l

I think it was described above but the reason for using INPUT_PULLUP is to prevent floating inputs that will react on random noise; further the functionality is the same.

In general you can test the difference by simply moving your hand closer to or further away from the wire that connects the button to the Arduino and use Serial.println to display the value. In one of the two positions of the switch, it will change randomly if you don't use a pull-up resistor (external or internal).