Button Interfacing With Arduino

ARDUINO Tutorial Series

Button Interfacing with Arduino

Can Arduino See Buttons ON/Off State..??

Contributed by:- Bhavesh Bhagat

-------------------------------------------------------------------

Button basics

A button is a two terminal Bi-polar device also known as the SPST (Single Pole & Single Throw) as shown in figure :

 

 

Connection of Button:-

To use the Button - Both of its terminals should have different logic levels i.e. we have set different Logics on both terminals to ON the button and Observe its ON State.   

 

There are two Ways to connect the button with Arduino Uno:-

Method 1:

If one of the two terminal of button is connected to the ground (0v or Logic 0) then it is compulsory to have the different logic i.e. Vcc (5v or Logic 1) on the other terminal of button to ON the button otherwise it will be in OFF state.

 

Method 2:

If one of the two terminal of button is connected to the Vcc   (5v or Logic 1) then it is compulsory to have the different logic i.e. Ground (0v or Logic 0) on the other terminal of button to ON the button otherwise it will be in OFF state.

 

Concept of Pull-Up & Pull-Down state:-

 

Initially all the pins of arduino Uno are in floating state i.e. neither Logic 0 nor Logic 1 on pins, it also known as the State of Confusion.

 

To ON/OFF the button we have to set the pins state to either logic 1 or logic 0 i.e. Pull-up or Pull-down the button.

 

In the Pull-up state, If we set one of the two terminal of button to the ground (0v or Logic 0) then it is compulsory to have the different logic i.e. Vcc ( 5v or Logic 1) on the other terminal of button with register of 10 KOhm connected in series with Vcc to ON the button otherwise it will be in OFF state.

 

In the Pull-down state, If we set one of the two terminal of button to the Vcc ( 5v or Logic 1) then it is compulsory to have the different logic i.e. Ground( 0v or Logic 0) on the other terminal of button with register of 10 k ohm connected in series with ground to ON the button otherwise it will be in OFF state.

Note:- we generally prefer Pull-up state for a Pin in Floating State.

 

Task: Write a program to blink an LED using the button in infinite loop connected with P1 of Arduino Uno.

Compiler:-  Arduino IDE 1.8.6

Source code

#define btn1on !(digitalRead(0))

void setup()

{

 pinMode(0,INPUT_PULLUP);

pinMode(1,1);

}

void loop()

{

 if(btn1on)

{

  digitalWrite(1,1);

 delay(100);

digitalWrite(1,0);

delay(100);

}

else

     digitalWrite(1,0);

}

 

 

CLICK HERE TO VIEW / DOWNLOAD ARDUINO TRAINING SYLLABUS

 

CLICK HERE TO REGISTER NOW FOR ARDUINO TRAINING

 

 

Top