How To Blink LED Using A Button In Arduino

by | Dec 30, 2022 | Arduino, Coding

Introduction

Today we will see how we can use a pushbutton to blink LEDs using Arduino Uno. This project has many applications and products like light shoes that kids wear, Rakhis that has a button on it, pressing it results in glowing light, friendship belts, etc. So let us begin to learn how to blink LED using a button in Arduino.

Once you have the hardware set up, we will use the Arduino software to write a program that will control the LED and button. In the program, you will need to set up the digital input pin connected to the button as an input and the digital output pin connected to the LED as an output.

 

Supplies

To learn how to blink LED using a button in Arduino, we will require the following components:

Components

Circuit Diagram

Steps To Blink LED Using A Button In Arduino

Step 1: Assemble all the components on the Digital Board.

Step 2: Connect the LEDs and Pushbutton on the Breadboard.

LED:

Step 3: First, connect the positive leg of the LED to a digital output pin on the Arduino and the negative leg to a GND pin

Step 4: Then, connect one end of the button to a digital input pin on the Arduino and the other end to a GND pin.

Pushbutton:

Step 5: Connect the one terminal of it to the 5V pin of the Arduino at its Analog Side.

Step 6: Connect the second Terminal of it to the 2-number pin of the Arduino.

Step 7: Connect the third Terminal of it to the resistor’s one end and the second end of the resistor to the GND pin of the Arduino using a black colored wire.

Source Code

int state = 0;
void setup()
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(2, INPUT);
}
void loop()
{
state = digitalRead(2);


if (state == LOW)
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
}
else
{
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
delay (100);


digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
delay (100);
}
}

Explanation Of The Code

1. Firslty we have initialized a state variable to 0.

2. Next, we used the setup function to configure pin numbers 11, 12 & 13 for output purposes and pin number 2 for input purposes.

3. In the loop function, we are reading the input from pin number 2.

4. After that, we use the if-else statement, which will decide how the LED will glow when we click on the button and what will happen if we do not click it.

5. So, on clicking it, the LEDs will blink as per the time set by the delay function.

6. We have used a delay() function, which takes time in milliseconds(ms).

Output

On clicking the button, LEDs will start blinking one after the other in a loop.

How To Blink LED Using A Button In Arduino

Overall, to blink LED using a button in Arduino is a simple process that requires just a few connections and a bit of code. This Aduino project can be useful starting point for learning how to use buttons and LEDs with Arduino.

 

You May Also Like To Create…

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *