How to control LED with button using Arduino

11,472 views

In this tutorial, I am going to show you how to control an Led with the help of push-button using an Arduino. In the Arduino Led Button project, we use the push-button to control the turning on and off actions of Led. While pressing a button Led turns on and pressing it twice Led turns off. This is the foremost project to start up learning an Arduino for the one who has just bought an Arduino so let’s get started.

Hardware Components

Following are the necessary hardware items required for Control LED with Arduino:

S.NOComponentValueQty
1.Arduino Uno R31
2.USB Cable A/B1
3.Breadboard1
4.Connecting Wires1
5.Push Button1
6.LED5mm1
7.Resistors10k,470 ohm1,1

Working Explanation

This project is the basic step and it has numerous other applications and it is used in almost every project as an indication of errors or power failures so it’s a good way, to begin with. Turning an Led on and off with the help of push-button using an Arduino is a similar method of turning any device on and off. Once you created the code to switch on and off an Led, you can write a code to turn anything on and off.

The push-button is a device that connects two points in a circuit when pressed which is the way to turn on an Led. It can be connected with a pull-up resistor to 5volts, to the ground and in between depending on the state you want to use. Follow the steps below to make it.

Connection

  • STEP # 1 ( Make Push Button Connections )
  • Put Resistor 10k B/w Pin1 of Push Button and Gnd of Arduino
  • Pin2 of Push Button to 5V of Arduino
  • STEP # 2 ( Make LED Connections )
  • +VE Of LED To D13 of Arduino.
  • Resistor 470 B/w -VE Of LED & then Gnd of Arduino.
  • STEP # 3 ( Upload Code )

Application

  • It can be used to turn on and off any device
  • It can be used as an indication of power, errors, etc
  • To start and stop the action in games

Circuit Diagram

led control arduino

Arduino LED Button Code

   
const int buttonPin = 7;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);


  if (buttonState == HIGH) 
  {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  }
  else 
  {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}