Arduino Mega - Button

The button is a simple but important part used in many Arduino Mega projects. It may seem hard because of how it works and how it feels when you press it, which can be tough for beginners. This guide is made to help beginners understand it easily. Let’s start!

Arduino Mega button

※ NOTE THAT:

Before we learn how to use the button with the Arduino Mega, here are two common mistakes beginners make:

  • Floating input problem
    • Symptom: When a button is connected to an Arduino Mega input pin, the pin’s state is not reliable and does not show the real button state.
    • Cause: The input pin has no pull-down or pull-up resistor.
    • Solution: Add a pull-down or pull-up resistor to the input pin. We’ll explain more later in this guide.
  • Debounce (chattering) problem
    • Symptom: The Mega reads the button and tries to see a press by looking for changes in state (HIGH to LOW, or LOW to HIGH). But with one press, the Mega may think there are multiple presses.
    • Cause: The button’s mechanical action makes the input switch between LOW and HIGH quickly after one press.
    • Solution: Use debounce. See the Arduino Mega button debounce tutorial for details.

    Note: Chattering only affects projects that need an exact count of presses. In other apps, it may not be a problem.

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Breadboard-mount Button with Cap
1×Breadboard-mount Button Kit
1×Panel-mount Button
1×Push Button Module
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Button

The push button, also called a tactile button or momentary switch, is a switch that closes when you press and hold it, and opens when you release. Push buttons come in different kinds, mainly two groups:

  • Push buttons for PCB mounting (can be used on a breadboard)
  • Push buttons for mounting on a panel
Arduino Mega Push button

Pinout

The PCB-mount buttons usually have four pins.

Button Pinout

Inside, the pins are connected in pairs. So you only need to use two pins, choosing one from each pair.

There are four ways to connect the button, but really only two because they are similar. See the image.

How To Use Button

A button has four pins, but we only use two. This helps it stay steady on the circuit board and survive when pressed.

The panel-mount buttons usually have two pins.

two-pin push button Pinout
image source: diyables.io

The push button module includes an built-in pull-down resistor, which ensures that the output remains LOW when the button is not pressed. It has three pins:

  • GND: Connect this pin to ground.
  • VCC: Connect this pin to a 5V or 3.3V power supply.
  • OUT: Connect this pin to a digital input on your Arduino.

With this setup, the module gives LOW when the button is not pressed and gives HIGH when the button is pressed.

How It Works

  • If you don’t press the button, pin A is not connected to pin B.
  • If you press the button, pin A is connected to pin B.
How Button Works

Arduino Mega - Button

One side of the button connects to power or ground, and the other side connects to a pin on the Arduino Mega.

We can tell if the button is pressed by reading the pin on the Arduino Mega that is set as an input.

Button State and Pressing State

How the button's state relates to when it's pressed depends on how we connect the button to the Arduino Mega and the pin settings on the Arduino Mega.

There are two easy ways to use a button with an Arduino Mega.

  • Connect one side of the button to VCC and the other side to an Arduino Mega input pin, with a pull-down resistor to ground. When you press the button, the pin will be HIGH. When not pressed, it will be LOW. You must use an external resistor.
  • Connect one side of the button to GND and the other side to an Arduino Mega input pin, with a pull-up resistor. When you press the button, the pin will be LOW. When not pressed, it will be HIGH. You can use either an internal or external resistor; the internal resistor is built into the Mega and can be turned on in code.

※ NOTE THAT:

If we don’t use a pull-down or pull-up resistor, the input pin is floating when the button isn’t pressed. That means the pin’s state can change by itself to HIGH or LOW, giving wrong readings.

  • Worst practice: Set the Arduino Mega pin as an input with pinMode(BUTTON_PIN, INPUT) and no pull-down or pull-up resistor.
  • Best practice: Set the Arduino Mega pin with an internal pull-up resistor using pinMode(BUTTON_PIN, INPUT_PULLUP). This doesn’t need an external resistor.

To help beginners, this guide uses the easiest method: set the Arduino Mega pin to use its built-in pull-up resistor, so you don’t need an external resistor. Beginners don’t have to worry about connecting a pull-up or pull-down resistor. They only need to use the Arduino code shown here.

Wiring Diagram

  • Circuit diagram for Arduino Mega and a PCB-mounted button.
The wiring diagram between Arduino Mega Button

This image is created using Fritzing. Click to enlarge image

  • How to wire Arduino Mega with a panel-mounted button
The wiring diagram between Arduino Mega two-pin push button

This image is created using Fritzing. Click to enlarge image

How To Program For Button

  • Sets pin 7 on the Arduino Mega as an input with the built-in pull-up resistor using the pinMode() function.
pinMode(7, INPUT_PULLUP);
  • It uses digitalRead() to check the state of a pin on the Arduino Mega.
int button_state = digitalRead(BUTTON_PIN);

※ NOTE THAT:

There are two common ways to do this:

  • First: When the input is HIGH, do something. When the input is LOW, do the opposite.
  • Second: When the input changes from LOW to HIGH (or HIGH to LOW), do something.

Choose one based on what you want to happen. For example, with a button and an LED:

  • If you want the LED to turn ON when the button is pressed and turn OFF when it is not pressed, use the first way.
  • If you want the LED to switch between ON and OFF every time you press the button, use the second way.

Arduino Mega Code - Reading the state of button

#define BUTTON_PIN 7 // The Arduino UNO R4 pin connected to the button void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the pushbutton pin as a pull-up input pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: int button_state = digitalRead(BUTTON_PIN); // print out the button's state Serial.println(button_state); delay(500); }

Detailed Instructions

Follow these steps one by one.

  • If this is your first time using the Arduino Mega, see the guide for setting up the Arduino Mega in the Arduino IDE
  • Connect the button to the Arduino Mega according to the diagram you were given.
  • Connect the Arduino Mega board to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the correct board (Arduino Mega) and the right COM port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to your Arduino Mega.
Arduino IDE - How to Upload Code
  • Open the Serial Monitor.
  • Press and release the button several times.
  • See the result in the Serial Monitor.
COM6
Send
1 1 1 0 0 0 0 0 0 1 1 1
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

1 is on, 0 is off.

Code Explanation

The explanation is in the comments in the Arduino code above.

Arduino Mega Code - Detecting the button's press and release event

Let's change the code so it can tell when buttons are pressed and released.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-button */ #define BUTTON_PIN 7 // The Arduino UNO R4 pin connected to the button int button_state; // the current state of button int prev_button_state = LOW; // the previous state of button void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the pushbutton pin as a pull-up input pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: button_state = digitalRead(BUTTON_PIN); if (prev_button_state == HIGH && button_state == LOW) Serial.println("The button is pressed"); else if (prev_button_state == LOW && button_state == HIGH) Serial.println("The button is released"); // save the last state prev_button_state = button_state; }

Detailed Instructions

  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to upload the code to the Arduino Mega.
  • Open the Serial Monitor.
  • Press and release the button.
  • See the result in the Serial Monitor.
COM6
Send
The button is pressed The button is released
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

  • Even if you press and release the button once, the Serial Monitor might show many press and release events. This common problem is called button bouncing. You can learn more in the Arduino Mega - Button Debounce tutorial.
  • To make things easier for beginners who use several buttons, we created a library called ezButton. You can learn about the ezButton library.
  • If you are using the button module, set the pin to input mode with pinMode(BUTTON_PIN, INPUT). With this setup, the module outputs LOW when the button is not pressed and HIGH when it is pressed.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!