• nithya7rns

How to control an LED using Push button and Arduino?

Updated: Dec 9, 2020

We all know that #LED can be controlled by various methods, based on Light intensity readings from LDR, based on readings from Motion sensors, controlling through Telegram App, etc. For all this, we need to understand the basics of controlling an LED through a switch or a Push Button.


This article describes how to control an LED using a #Push button and Arduino code. So let's get started, first with the Hardware Components required.



Hardware requirements


The image here shows all the components required to build the circuit.


All Hardware components required
All Hardware components required


  1. Arduino UNO Board


Arduino UNO
Arduino UNO

#Arduino board is a microcontroller that is used to accept inputs from sensors connected and provide an output action on the desired device connected to it. The sensor inputs can be from light-detecting sensors, motion sensors (Ultrasonic or IR), temperature sensors, etc. The output function done on the devices can be turning on an LED, the sound of a buzzer, or a Serial monitor display.

Buy Arduino UNO from here.



2. Jumper Wires


Jumper Wires
Jumper Wires


These are the main components that are used to establish the connections between different devices of the circuit.

Buy Jumper Wires from here.


3. LED



LED
LED

Light Emitting Diode is a commonly used light source. It is a semiconductor. LED emits light when current flows through it.


Buy LED from here.




4. Breadboard


Breadboard
Breadboard


The breadboard is the basic component of any circuit building process. All components, be it input sensors or output display devices are connected to the #Arduino or any other microcontrollers using wired connections through a breadboard. The holes in the breadboard are in series. There are various sizes like full-sized, half-sized, and mini breadboards.


Buy Breadboard from here.



5. Push-button.


PUSH Button
PUSH Button

PUSH Buttons are simple devices used for switching control. It is easy to use and starts or stops the function when connected in a #circuit.


Buy Pushbuttons from here.



Circuit Connections



Circuit Diagram
Circuit Diagram

The circuit connection, on the whole, can be divided into two parts.


First the connection between the Push Button and the Arduino board. One terminal of the Pushbutton is connected to PIN 2 of the Arduino board and the other terminal can be connected to the Ground (GND) pin of the UNO board or to the common Ground in the Breadboard.


Secondly, the Connections between the LED and the Arduino board. The positive terminal of the LED is connected to PIN 12 of the UNO board and the negative terminal is connected directly to the Ground (GND) of the board or to the common ground in the breadboard.


Finally, the Arduino Board is interfaced with the system in which the #IDE is present and the code is written.


Code


The code is simple and easy to understand.

void setup() {
    pinMode(2, INPUT_PULLUP);

The setup( ) function defines all the necessary functions inside it needed for the components and the circuit to work accordingly. First, the inbuilt function pinMode( ) is used to declare the particular pin of the Arduino Board as Input/Output. Pin 2 is declared as an input pin here.

 pinMode(10, OUTPUT);
}

Similarly, pin 10 of the Arduino is declared or assigned to be an Output pin and the setup( ) function ends.

void loop() {  
  int pusshed = digitalRead(2);

Inside the loop( ) function, which is used to run the particular code repeatedly, an integer "pusshed" is declared, and the function digitalRead( ) is assigned to it which takes in the value from the Pin 2 of Arduino. As we have seen previously, pin 2 is the input pin.

if(pusshed == LOW){
    digitalWrite(10, HIGH);}

An "if" condition is written to check the value of the variable "pusshed". If the value of "pusshed" is 0(LOW), the Output pin 10 gets the value HIGH(1).

else{
    digitalWrite(10, LOW);}
    }

Following the "if" condition, an else condition is written to check the is the values of "pusshed" is HIGH. In that case, the output pin 10 is assigned the value LOW(0). The loop( ) function ends here.


COMPLETE CODE


void setup() {
  
  pinMode(2, INPUT_PULLUP);// define pin two as input for push button
  pinMode(10, OUTPUT);// defind pin 10 as output
}

void loop() {
  
  int pusshed = digitalRead(2);
  if(pusshed == LOW){
    digitalWrite(10, HIGH);// if pushed is equal LOW, turn the pin 10 HIGH (give it 5v)
  }else{
    digitalWrite(10, LOW);// else set pin 10 to low or zero volt
  }
}

For the complete code for this program, download the following document.

code
.txt
TXT • 392B

Note: Please change the file format to .ino(Arduino IDE) format before you upload the code to the Arduino board.



Working


The working of this circuit is simple. The LED receives the Supply voltage from the Arduino board. Initially, LED is OFF and is controlled using a Switch/Pushbutton.


When the circuit is OFF.
When the circuit is OFF.

The Pushbutton is the element that establishes the connection between the LED and Power supply through the Arduino board indirectly. That is, on pressing the Pushbutton, the switch is closed, the voltage (5V) is supplied, and the LED is switched ON. On taking the hand off the Pushbutton, the LED is OFF. The switch is open. Hence the switching ON and OFF of the LED can be controlled using Pushbutton and Arduino.


When the circuit is ON.
When the circuit is ON.

Watch this video practical explanation of the circuit and code.


Project Video By - Abhishek Goud

Take the first step today to design your circuit, learn to control the LED using #Pushbutton. With the help of this article and circuit, build your own circuit, develop it further with different and creative #code combinations.


SEE ALSO:

140 views