Button Controlled LED Project using Arduino
In this project, we will control an LED using a push button and Arduino. This project teaches beginners how to use digital input to control digital output, making it a fundamental step towards interactive electronics.
BeginnerComponents Required
- Arduino Uno (or compatible board)
- Push Button
- LED
- 220Ω Resistor (for LED)
- 10kΩ Resistor (pull-down resistor for button)
- Breadboard
- Jumper Wires
- USB Cable
Circuit Connections
Follow the steps below to connect the button and LED with the Arduino board. Make sure the Arduino is not connected to USB while wiring.
- Insert the push button into the breadboard.
- Connect one end of the button to 5V on Arduino.
- Connect the other end of the button to digital pin 2 on Arduino.
- Connect a 10kΩ pull-down resistor from pin 2 to GND.
- Insert the LED into the breadboard with the longer leg (anode) positive.
- Connect the longer leg of the LED to digital pin 13.
- Connect a 220Ω resistor from the shorter leg (cathode) of the LED to GND.
Here is the circuit diagram for reference.
Arduino Code
// Button Controlled LED Project
// Arduino Hub
int buttonPin = 2;
int ledPin = 13;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
Upload the Code
- Connect the Arduino board to your computer using the USB cable.
- Open the Arduino IDE and paste the code into a new sketch.
- Select the correct board and port from the Tools menu.
- Click the Upload button.
- Once uploaded, press the button to control the LED.
Code Explanation
Let us understand how the Arduino code works step by step. This will help you modify the project later and build more interactive projects.
Declaring the Pins
int buttonPin = 2; stores the pin number where the button is connected.
int ledPin = 13; stores the pin number where the LED is connected.
setup() Function
The setup() function runs only once when the Arduino starts.
Here, we tell Arduino that pin 2 will work as an input (to read the button state)
and pin 13 will work as an output (to control the LED).
loop() Function
The loop() function runs continuously.
Inside this loop, we first read the current state of the button using digitalRead(buttonPin).
If the button is pressed (HIGH), the LED is turned ON.
If the button is not pressed (LOW), the LED is turned OFF.
Pull-down Resistor
The 10kΩ resistor is called a pull-down resistor. It ensures that when the button is not pressed, the pin reads LOW. Without it, the pin would read random values when the button is not pressed.
Common Mistakes & Troubleshooting
- LED does not glow: Check the LED direction. The longer leg should be connected to the Arduino pin.
-
No blinking:
Make sure the correct pin number is used in the code.
If you are using another pin, update
ledPin. - Wrong resistor value: Use a 220Ω or 330Ω resistor. Very high or very low values may cause issues.
- Upload error: Select the correct board and COM port from the Arduino IDE Tools menu.
- LED very dim: Check loose connections on the breadboard and jumper wires.
How It Works
When you press the push button, it completes the circuit, sending a HIGH signal to pin 2. The Arduino detects this HIGH signal and turns ON the LED.
When you release the button, the pull-down resistor ensures the pin reads LOW. The Arduino then turns OFF the LED.
This creates an immediate response: the LED turns ON when you press the button and turns OFF when you release it. This is a fundamental interactive control mechanism used in many Arduino projects.