Arduino Uno R4 Button Simulator: Wiring, Code and a Live Circuit
This is a pushbutton wired to an Arduino Uno R4 Minima, with a debounced press counter running live: hold the button on the drawing and the serial monitor counts each press exactly once.
Two wires and no resistor: pin 2 to one side of the button, GND to the other, and the Arduino Uno R4’s internal pull-up does the rest.
- 5 V logic
- No library
- Input on pin 2
- Internal pull-up, 20 ms debounce
Wiring the Button to the Arduino Uno R4
Every connection in the circuit above, with the board’s own pin names.
| Button pin | Arduino Uno R4 pin | Why |
|---|---|---|
| One side (1a) | pin 2 | With INPUT_PULLUP the Renesas RA4M1 (Cortex-M4)’s own resistor holds the pin high. |
| The other side (2a) | GND | Pressing connects the pin to ground, so it reads LOW. |
No resistor and no supply wire: the Renesas RA4M1 (Cortex-M4)’s internal pull-up ties the pin to 5 V until the button pulls it to ground.
Pins to leave alone on the Arduino Uno R4
An Uno sketch’s source builds for the R4, but a compiled Uno program will not run on it: different chip. The R4’s pins source less current than an Uno’s, about 8 mA each, so do not drive a relay coil or a motor straight from one.
On a real bench
The Uno R4 Minima fits every Uno shield and every Uno breadboard layout, so wire the Button exactly as you would on an Uno R3. It has a USB-C socket, and its VIN accepts up to 24 V where an Uno R3 stops at 12.
The code
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
const int BUTTON_PIN = 2; // pin 2 to the button, the button to GND
const unsigned long DEBOUNCE_MS = 20;
int lastReading = HIGH;
int state = HIGH;
unsigned long changedAt = 0;
unsigned int presses = 0;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP); // HIGH when up, LOW when pressed
Serial.println("Press the button");
}
void loop() {
int reading = digitalRead(BUTTON_PIN);
if (reading != lastReading) changedAt = millis();
lastReading = reading;
if (millis() - changedAt > DEBOUNCE_MS && reading != state) {
state = reading;
if (state == LOW) {
presses++;
Serial.print("Pressed, count ");
Serial.println(presses);
}
}
}
pinMode(BUTTON_PIN, INPUT_PULLUP) turns on the pull-up inside the chip, so the pin reads HIGH while the button is up and LOW while it is held. That is backwards from what most people expect the first time, and it saves a resistor.
A real button bounces: for a few milliseconds after it closes, the contacts open and close many times. The sketch notes the time of every change and only accepts a new state once the reading has stayed put for 20 ms, so one press counts once. The simulated button bounces like a real one, so you can see it fail by setting DEBOUNCE_MS to 0.
Serial.begin(9600) opens the serial monitor. Serial is the USB port; Serial1 is on pins 0 and 1, which on the R4 are free for other serial devices.
Common mistakes with the Button on the Arduino Uno R4
A floating input
With plain INPUT and no resistor, the pin reads whatever noise is nearby and the sketch sees presses nobody made. Use INPUT_PULLUP, or a 10 k resistor to the supply.
Counting the bounce
Without a debounce, one press can count as three or four. The simulated button bounces for a few milliseconds the way real contacts do, so the fix here is the fix on a desk.
Expecting an Uno’s pin current
The R4’s pins are 5 V like an Uno’s, but each one sources only about 8 mA where an Uno’s gives 20. A signal line to the button is fine; an LED or a coil hung straight off the pin is not.
Questions and answers
Can I simulate a button with an Arduino Uno R4 Minima?
Yes. The circuit at the top of this page is an Arduino Uno R4 Minima with a pushbutton, running in your browser on a simulated Renesas RA4M1 (Cortex-M4). Hold the button on the drawing to press it; the contacts bounce like a real switch. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
Why does the button read LOW when pressed?
Because the pull-up holds the pin HIGH and the button connects it to ground. Compare with LOW in the code, or wire the button to the supply with a pull-down resistor if you want the opposite.
Should I use attachInterrupt() for a button?
You can, though on the Uno R4 the simulator does not model the interrupt controller, so polling as this sketch does is the way here.
Is there a library for buttons?
Bounce2.h, OneButton.h and AceButton.h all compile here. The sketch above does the same job in a dozen lines, which is worth understanding before reaching for one.
Build your own Button project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.