Welcome to this beginner-friendly tutorial by Madras Academy, where you'll learn how to control an LED using a push button with Arduino Mega. This fundamental project is perfect for students and electronics enthusiasts starting their Arduino journey!
In this tutorial, you will:
This project lays the foundation for more complex interactions and is a great first step in physical computing.
👉 Use the tabs below to navigate through the project:
Let's build your first interactive Arduino project!
Watch the project in action below:
This simple project requires just a few basic components to create your first interactive circuit:
Essential components:
Component | Quantity | Description |
---|---|---|
Arduino Mega | 1 | Main controller board |
Push Button | 1 | Momentary tactile switch |
LED | 1 | Any color |
220 ohm Resistor | 1 | For LED current limiting |
10K ohm Resistor | 1 | Pull-down resistor (optional) |
Breadboard | 1 | For building the circuit |
Jumper Wires | 5-7 | For connections |
const int buttonPin = 2; // Push button connected to digital pin 2
const int ledPin = 13; // LED connected to digital pin 13
int buttonState = 0; // Variable to store button status
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up
}
void loop() {
buttonState = digitalRead(buttonPin); // Read button state
if (buttonState == LOW) { // Button is pressed (LOW because of pull-up)
digitalWrite(ledPin, HIGH); // Turn LED ON
} else {
digitalWrite(ledPin, LOW); // Turn LED OFF
}
}
Note: We're using the Arduino's internal pull-up resistor, so the button connects between the input pin and GND.
Enhance your skills with our Arduino Beginners Course: https://www.madrasacademy.com/arduino-adventures-beginners-course