Circuit Creation

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.

Beginner

Components 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.

  1. Insert the push button into the breadboard.
  2. Connect one end of the button to 5V on Arduino.
  3. Connect the other end of the button to digital pin 2 on Arduino.
  4. Connect a 10kΩ pull-down resistor from pin 2 to GND.
  5. Insert the LED into the breadboard with the longer leg (anode) positive.
  6. Connect the longer leg of the LED to digital pin 13.
  7. Connect a 220Ω resistor from the shorter leg (cathode) of the LED to GND.
Arduino Button Controlled LED circuit connection

Here is the circuit diagram for reference.

Arduino Code

                    
/*
    Project: Button Controlled LED Project using Arduino
    description: Learn how to control an LED using a push button and Arduino. Step-by-step

   Components:
     -  Arduino Uno
     -  Push Button
     -  LED
     -  220Ω Resistor (for LED)
     -  10kΩ Resistor (pull-down resistor for button)
     -  Breadboard
     -  Jumper Wires
   Author: Circuit Creation RN
*/


// This code allows you to control an LED using a push button connected to an Arduino board. When the button is pressed, the LED will turn ON, and when the button is released, the LED will turn OFF.
// Circxuit Creation RN

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

  1. Connect the Arduino board to your computer using the USB cable.
  2. Open the Arduino IDE and paste the code into a new sketch.
  3. Select the correct board and port from the Tools menu.
  4. Click the Upload button.
  5. 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.