// Wokwi simulation of buttons configured per
//
// S1: Exter
// https://wokwi.com/projects/374361359504133121
// for https://forum.arduino.cc/t/wiring-a-button-pull-down-bad-vs-input-pullup-best/1162985/4
//
//
const byte S1pin = 11, S2pin = 5, S3pin = 3;
const byte L1pin = 7, L2pin = 9;
void setup() {
Serial.begin(115200);
pinMode(S1pin, INPUT);
pinMode(S2pin, INPUT);
pinMode(S3pin, INPUT_PULLUP);
pinMode(L1pin, OUTPUT);
pinMode(L2pin, OUTPUT);
Serial.println(
F("See https://forum.arduino.cc/t/wiring-a-button-pull-down-bad-vs-input-pullup-best/1162985/4\n"
"\nhttps://forum.arduino.cc/t/share-tips-you-have-come-across/428745/999 "
" "
));
}
void loop() {
int S1pressed = digitalRead(S1pin) == HIGH;
int S2pressed = digitalRead(S2pin) == LOW;
int S3pressed = digitalRead(S3pin) == LOW;
// switch LEDs if a button is pressed
if (S1pressed || S2pressed || S3pressed) {
digitalWrite(L1pin, HIGH);
digitalWrite(L2pin, HIGH);
} else {
digitalWrite(L1pin, LOW);
digitalWrite(L2pin, LOW);
}
}
External
Pullup.
External
Pulldown.
Typical Switch Wiring and Simple LED connection
Best S3: Internal Pullup pinMode(3,INPUT_PULLUP) goes low on a push
Good S2: External Pullup pinMode(3,INPUT) goes low on a push
Bad S1: External Pulldown pinMode(11,INPUT) goes HIGH on a push
Note:
S1 and R is not recommended as there is a possibility of shorting +5V to GND