Arduino Mega - Button - LED

This guide shows you how to use the Arduino Mega and a button to control an LED. We will learn two easy ways to do this.

Application 1 - The LED shows the same state as the button.

Application 2 - The LED changes every time you press the button.

For Application 2, we need to debounce the button so it works reliably. We will see why this matters by comparing how the LED behaves with and without debouncing in the Arduino code.

Arduino Mega control LED

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 Push Button
1×Push Button Module
1×LED Kit
1×LED (red)
1×LED Module
1×220Ω Resistor
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 .

Buy Note: Use the LED Module for easier wiring. It includes an integrated resistor.

Overview of LED and Button

If you're new to using the LED, Button, and Arduino Mega, please check out these tutorials:

These tutorials explain how LED and Button work, their pinouts, how to connect them to the Arduino Mega, and how to program Arduino Mega to work with the LED and Button.

Wiring Diagram

The wiring diagram between Arduino Mega Button LED

This image is created using Fritzing. Click to enlarge image

Application 1 - The LED follows the button's state

Arduino Mega Code

/* * 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-led */ #define BUTTON_PIN 7 // The Arduino UNO R4 pin connected to the button #define LED_PIN 3 // The Arduino UNO R4 pin connected to the LED void setup() { Serial.begin(9600); // initialize serial pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode } void loop() { int buttonState = digitalRead(BUTTON_PIN); // read new state if (buttonState == LOW) { Serial.println("The button is being pressed"); digitalWrite(LED_PIN, HIGH); // turn on } else if (buttonState == HIGH) { Serial.println("The button is unpressed"); digitalWrite(LED_PIN, LOW); // turn off } }

Detailed Instructions

Follow these steps one by one.

  • Connect the parts as shown in the diagram.
  • Plug the Arduino Mega into your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the Arduino Mega board and the correct COM port.
  • Copy the code and open it in the Arduino IDE.
  • Click Upload in the Arduino IDE to build and send the code to the Arduino Mega.
Arduino IDE Upload Code
  • Press and hold the button for a few seconds.
  • See how the LED changes.

You'll see that the LED and the button show the same state.

Code Explanation

See the line-by-line explanation in the source code comments.

Application 2 - The LED changes its state each time the button is pressed

Arduino Mega Code - Button Toggles LED Without Debouncing

/* * 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-led */ #define BUTTON_PIN 7 // The Arduino UNO R4 pin connected to the button #define LED_PIN 3 // The Arduino UNO R4 pin connected to the LED int led_state = LOW; // the current state of LED int button_state; // the current state of button int prev_button_state; // the previous state of button void setup() { Serial.begin(9600); // initialize serial pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode button_state = digitalRead(BUTTON_PIN); } void loop() { prev_button_state = button_state; // save the last state button_state = digitalRead(BUTTON_PIN); // read new state if(prev_button_state == HIGH && button_state == LOW) { Serial.println("The button is pressed"); // toggle state of LED led_state = !led_state; // control LED arccoding to the toggled state digitalWrite(LED_PIN, led_state); } }

Code Explanation

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

In the code, the line led_state = !led_state does the same thing as the code shown below:

if(led_state == LOW) led_state = HIGH; else led_state = LOW;

Detailed Instructions

  • Copy the code and open it in the Arduino IDE.
  • Upload the code to the Arduino Mega.
  • Press the reset button several times.
  • See the LED turn on and off.

You may see the LED change every time you press the button. But it doesn’t always work perfectly. Sometimes the LED changes too many times quickly when you press once, or it may not change at all (it can change twice very fast, which is hard to notice).

To fix this problem, filter out the extra presses of the button.

Arduino Mega Code - Button Toggles LED With Debouncing

Getting a button press to work reliably can be hard for beginners. Luckily, the ezButton library makes it easy.

Why do we need debouncing? For more information, see the Arduino Mega Button Debounce tutorial.

/* * 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-led */ #include <ezButton.h> #define BUTTON_PIN 7 // The Arduino UNO R4 pin connected to the button #define LED_PIN 3 // The Arduino UNO R4 pin connected to the LED ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 7; int led_state = LOW; // the current state of LED void setup() { Serial.begin(9600); // initialize serial pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) { Serial.println("The button is pressed"); // toggle state of LED led_state = !led_state; // control LED arccoding to the toggleed sate digitalWrite(LED_PIN, led_state); } }

Detailed Instructions

  • Install the ezButton library. See this guide for steps: https://arduinogetstarted.com/tutorials/arduino-button-library#content_how_to_install_library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to load the code to the Arduino Mega.
  • Press and release the button several times.
  • Watch the LED change.

You will see the LED switch on or off exactly once each time you press the button.

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!