Can't seem to get internal pullup resistor working

Hello - I have a project that has 6 momentary push button switches. I am using a pro mini. I can't even get really simple code to work. I am using the internal pullup resistors with INPUT_PULLUP. I am getting both 1s and 0s coming up in the serial monitor. I have tried to simple codes, also switched to an UNO and changed pins and nothing seems to help. On the pro mini the internal LED that is not called out does go from lit to unlit when I push the button. The buttons test fine on a meter. Please see the code. I commented some of the code that called for lighting the internal LED since I was using that pin for the button. Am I going to need to add resistors?

Anthony

/*
  Input Pull-up 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 Mar 2012
  by Scott Fitzgerald

  This example code is in the public domain.

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

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

}

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

  // Keep in mind the pull-up 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 == HIGH) {
  //  digitalWrite(13, LOW);
  } else {
  //  digitalWrite(13, HIGH);
  }
}
//int led_pin = 13;
int button_pin = 13;
void setup() { 
  Serial.begin(9600);
//  pinMode(led_pin, OUTPUT); 
  pinMode(button_pin, INPUT_PULLUP); 
}
void loop() { 
 
  int button_state = digitalRead(button_pin);
  if (button_state == HIGH) { 
      //digitalWrite(led_pin, LOW);
      Serial.print("Button Open");
      Serial.println();
      
  } else { 
     // digitalWrite(led_pin, HIGH);
     Serial.print("Button Closed");
     Serial.println();
      
  } 
}
  pinMode(13, INPUT_PULLUP);
  int sensorVal = digitalRead(2);

See the problem now?

int button_pin = 13;It is sensible to leave pin 13 to what it does best; driving a LED.

Thank you, I have fixed that and still have the same issue with pin 13.

To you point. I am running out of I/O pins. I am using 21 of the 22

4 load cells - 8 digital inputs
6 buttons - 6 inputs (5 digital, 1 analog on A6)
1 pot - 1 input (1 analog on a7)
4 valves - 4 outputs - digital out to opto coupler
1 lcd - 2 outputs - SDA - A4, SCL - A5

That puts me at 21 inputs / outputs vs pro mini of 22

So I can leave 13 alone. If I need to add something in the future, am I better making 13 an output?

Thank you,

Anthony

have fixed that and still have the same issue with pin 13.

So post your fixed code and let’s see if we can fix it some more.

I ended up with this code working on the uno and not working on the pro mini. I had the pro mini soldered to a pcb and could not fix it by rewetting the solder. I grabbed another pro mini and another pcb and it now work fine. So either I had flaky solder or a flaky board or a board I harmed.

Thank for you help. I am just staying away from pin 13 because for now I can.

Anthony

int pin1;

void setup() {
  //start serial connection
  Serial.begin(9600);
  //configure pin as an input and enable the internal pull-up resistor
  pin1=0;
  pinMode(pin1,INPUT_PULLUP);


}

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


}

pin1=0; ?

Ya - pretty bad nomenclature but I did not know if pin was a reserved work for the compiler. I do try to do better in non test code.

Now the next problem. I am using the same code at A6 to do an analog read since I dont think it can read digital like some of the other analog pins and I don't think there is a pull up resistor. Well, i would expect it to read full voltage or 1023 when the switch is open. it does this 1 in 4 readings with 2 readings at 0 and the other reading at 30 or so. I would expect to hit the switch and it would go to 0 and it does for 100% of the readings. The only reason I am doing this with A6 is because I am out of digital pins and analog pins that can be made digital

Any thoughts? One option may be to take an average of 4 readings and look for zero but I bet there is a better option.

Anthony

Pin zero is the serial interface's receive pin.
Good thing you're not planning on using it for anything serial-related.

For the analogue pin, just test for less than, say, 100 for logical zero.

AWOL - Thank you, but I am getting the following repeating

1023
33
0
0
1023
35 varies a little
0
0
1025

testing for less than 100 would give me 3 out of 4 false positives.

Is it reading too fast for an analog pin?

pinMode(13, INPUT_PULLUP); // does not work on small Arduino boards
Because there is a LED/resistor directly connected to this pin/ground.
The internal pull up is too weak to overcome the LED/resistor pull down.
Better to avoid pin13 on small bards, or only use it as output.
On an Uno/Mega this pin is buffered with an opamp, so less of a problem.

Pin A6,A7 don't have internal pull up AFAIK.
Leo..

aiannar974:
AWOL - Thank you, but I am getting the following repeating

1023
33
0
0
1023
35 varies a little
0
0
1025. <<< [AWOL]You have very serious problems

AWOL - That was a typo

aiannar974:
To you point. I am running out of I/O pins. I am using 21 of the 22

If only one switch will be pressed at a time an analog switch tree can used to decode multiple values on a single analog input as different switches.

aiannar974:
AWOL - That was a typo

You should not be typing things like the output into this post, it should be cut and paste, for exactly that reason.