Using only two digital pins for a button? (no GND, no VCC)
-
If I only have two digital pins left, could they be used to support a button? Online searches have proven fruitless.
For example:
- D4 functions as output, set to permanently high. >> [ button ] >> - D5 functions as input, with pullup resistor enabled.
Would this work? Or would this fry the Arduino/pin?
-
@alowhum almost, if you set D4 permanently LOW.
Then you button on D5 toggles between HIGH (when open, due to pullup) and LOW (when closed, shorted to D4).
-
@Yveaux Thanks!
// It works!
For anyone finding this:
In the Setup / Before function:
// BUTTON pinMode(BUTTON_OUTPUT_PIN, OUTPUT); // Set a digital pin as power output digitalWrite(BUTTON_OUTPUT_PIN, LOW); pinMode(BUTTON_INPUT_PIN, INPUT_PULLUP); // Set another digital pin as power input
In the loop:
wait(20); // ON DEVICE TOGGLE BUTTON if (digitalRead(BUTTON_INPUT_PIN) == LOW) { button_pressed = true; Serial.print(F(".")); }
-
Out of curiosity, would there be any practical reason for doing it this way? Maybe there is an advantage that I am not thinking of. It just seems though that it is a waste of a pin, not to mention that it is more code to get it working.
-
@dbemowsk The Nano only has so many GND and VCC pins. This allows me to connect a relay, an LED and this button directly with dupont wires. No breadboard/expansion board/soldering required.
-
@alowhum Seems a bit wasteful of Nano pins for the sake of 2 lines of Veroboard and some header pins though, but if it works for you...
-
@zboblamont It's not very wasteful if you're not using them for anything else anyway. I'd rather use some extra code than extra hardware.
Suggested Topics
-
Update RF24 library to latest version
Bug Reports • 23 Mar 2014, 23:37 • andriej 24 Mar 2014, 22:52 -
No merge into master in the last 5 years, should we use development?
General Discussion • 23 Sept 2024, 17:48 • kiesel 25 Mar 2025, 15:30 -
Email notifications received twice
General Discussion • 6 Feb 2023, 16:15 • Jodaille 27 Mar 2025, 06:07 -
Is it possible to extract child ID from a just sent message?
General Discussion • 8 Nov 2021, 09:21 • Nigel31 29 days ago -
Which device I have to use to connect with accelerometer before connecting to my pc?
General Discussion • 24 Jan 2023, 17:16 • Yada Kijsathan 16 Mar 2025, 20:17 -
ESP-NOW
General Discussion • 22 Apr 2018, 05:58 • NeverDie 17 Feb 2025, 22:24