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);
}
}
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);
}
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.
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..