How To Interface Push Button With Arduino Uno

A Embedded system basically consists of three parts-


  1. Input
  2. Processing Unit
  3. Output

Input section takes input data from the external environment it can be switch, tempreture senser, pressure senser, light senser etc. After this the processing unit like Arduino Uno process the data gives desired output. You can get the output with the help of led, lcd, motor etc.

So here we will discuss how to interface switch with Arduino Uno and we will get output on led.
There are two method to interface the switch with Uno one is using pull up resistor and another is using pull down resistor. Pull up or pull down resistor ensures that input to the controller settle at desired logic level. Pull up and pull down resistor keeps input to Arduino Uno at logic 1 and logic 0 respectively by default. You can use the pull up and pull down method using hardware. In the hardware method just connect one end of 10k resistance with switch and another end to ground or power to pull down or pull up the input respectively.






In the software method you can just enable the pull up resistor using instruction pinMode(4,INPUT_PULLUP); where 4 is input pin in that case you do not have to connect 10k resistor in hardware.

So here i am discussing only hardware method. I have connected the switch at 4th pin of Arduino Uno and led at 13th pin. When the switch is press then led will glow otherwise it will be off  by default. Below is the sketch-

Code

int i=4; /// switch is connected at 4th pin
int j=13; // led is connected at 13th pin

void setup() {
pinMode(i,INPUT);
pinMode(j,OUTPUT);

}

void loop() {
  int k=digitalRead(i);
   if(k==1)   // if switch is press it will give logic 1 at input pin 4
   {
     digitalWrite(j,1); // it will set the led at logic 1 
    
    
    }
    else
    {
      
        digitalWrite(j,0); // Led will remain at logic 0 by default
      }

}



digitalRead function reads the logic at respective pin of Arduino Uno.If you are not familiar with the Arduino IDE and sketch then click here.

Below is the circuit diagram of interfacing push button with Arduino Uno.

Circuit diagram-










Comments