Monday, July 20, 2020

How to connect a Push Button to Arduino UNO (Method 1) - Arduino Tutorials #12

In this post, I will show you how to properly connect a push button to an Arduino UNO.

If you prefer video mode you can click here to watch it on YouTube

First, connect the circuit according to the circuit diagram.

After making the connection connect the circuit to a Laptop or a Desktop

Circuit Explanation

The circuit shown is constructed based on the Pull Down Resistor configuration. This configuration helps prevent any unnecessary inputs when the button is not in use. In our case, The Pull Down resistor ensures the voltage is low when the button is not pressed. Always make sure that you use a pull-down resistor configuration when using a push button. For the resistor value, you can use anything between 1k to 10k Ohm.

Next, select all the appropriate board, port, and the programmer and upload the sketch Button which is under File > Examples > Digital

After uploading the sketch when you press the Push Button you should get a blink on the Built-in LED


Sketch Start

const int buttonPin = 2;
const int ledPin =  13;

int buttonState = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);

Sketch End

Sketch Explanation - Button

const int buttonPin = 2; - Assigning the Pin Number 2 to the Button
const int ledPin =  13;Assigning the Pin Number 13 to the LED
int buttonState = 0; - Holds the sate of the LED (High or Low)
void setup() {} - Setup function runs when you power the board or press reset button. 
pinMode(ledPin, OUTPUT); - declaring the built in LED pin to Output
pinMode(buttonPinINPUT); - declaring the Pin Number 2 as Input
void loop() {} - Loop function runs statements inside it over and over again forever
buttonState = digitalRead(buttonPin);This statement checks if the button is pressed or not and stores the value HIGH or LOW
digitalWrite(ledPin, HIGH); - This turns the LED ON (Voltage = +5V)
digitalWrite(ledPin, LOW); - This turns the LED OFF (Voltage = 0V)
delay(1000); - delays execution of next statement by 1second(1second = 1000milli Second)

  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
} - This is a basic IF statement. When the button state is high it turns on the LED and when the button state is low it turns off the LED.

Affiliate Links:
    Arduino UNO
Thank You For Reading !!!

No comments:

Post a Comment

Hi, Thanks for Commenting !!!