How to wire a push button to an Arduino Mega
To wire a push button to an Arduino Mega, connect 1a to pin 2 and 2a to GND. The button goes between the pin and GND and uses the board’s internal pull-up, so there is no resistor and no supply wire, and the pin never sees more than 5 V. The sketch below uses no library beyond what the board includes. It waits for the button, debounces it for 20 ms and prints a running count of presses.
- 5 V logic
- Library: none
- 2 wires
Push button to Arduino Mega wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| Push button pin | Arduino Mega pin | Wire color | Why |
|---|---|---|---|
| One side (1a) | pin 2 | amber | With INPUT_PULLUP the ATmega2560’s own resistor holds the pin high. |
| The other side (2a) | GND | blue | Pressing connects the pin to ground, so it reads LOW. |
Power and logic levels for push button and Arduino Mega
Logic level
The Arduino Mega’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.
This pair
No resistor and no supply wire: the ATmega2560’s internal pull-up ties the pin to 5 V until the button pulls it to ground.
Pins to leave alone
Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.
The working code for the Arduino Mega
Library: none, the sketch reads the pin with digitalRead() and its internal pull-up. This is the sketch the circuit above runs.
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);
}
}
}
What you should see
Open the serial monitor at 9600 baud. The sketch prints Press the button once. Each press then adds a line, Pressed, with a count that goes up by one; holding the button down counts once, because the sketch reacts to the press and not to the level.
A button that reads pressed all the time is wired to the wrong pin pair or the pull-up is off; one that counts twice per press is bouncing and needs the debounce time raised.
The serial monitor prints this, recorded from the circuit above with the button pressed once after 300 ms:
Press the button
Pressed, count 1Common mistakes wiring a push button to an Arduino Mega
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.
Taking the serial or I2C pins
Pins 0 and 1 are the USB serial port, 14 to 19 are Serial1 to Serial3 and 20 and 21 are I2C. the button works on any of them until the sketch needs that port, so start on pin 2 and up.
Build your own push button project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.