Push Button with Arduino

1. Introduction

Welcome to this lesson on push buttons! In this lesson, we will be learning about the basics of push buttons. we will learn how to program and interface a push button with an Arduino, using digital inputs. By the end of this lesson, you will have a solid understanding of how to use and implement push buttons in your projects. So, let's get started!

2. Definition

A push button is a simple mechanical switch that is typically used to initiate an action or event when pressed. It is often used as a user interface element in electronic devices, such as phones, computers, and appliances, to control various functions. Push buttons come in different shapes, sizes, and types, such as momentary, latching, and tactile buttons, and are activated by physical force.


Push Button

They are typically made of plastic or metal and can be found in a wide range of applications from elevators, home appliances, or industrial equipment.

3. Pull-up resistor

A pull-up resistor is an electronic component that is used to prevent a digital input pin on an Arduino, from floating.

A floating input pin can result in unpredictable and unstable behavior. A pull-up resistor is connected between the input pin and a positive voltage source (usually Vcc) and it allows the input pin to read a logical high voltage (1) when the switch is open or not pressed. When the switch is closed or the button is pressed, the input pin is connected to the ground (GND) and it will read a logical low voltage (0).

Pull-up resistor


The pull-up resistor value is chosen such that it provides enough current to the input pin to ensure a stable high voltage when the switch is open, but not so much that it causes current to flow when the switch is closed.

4. Pull-down resistor

A pull-down resistor is an electronic component that is used to prevent a digital input pin on an Arduino, from floating.

A floating input pin can result in unpredictable and unstable behavior. A pull-down resistor is connected between the input pin and the ground (GND), and it allows the input pin to read a logical low voltage (0) when the switch is open or not pressed. When the switch is closed or the button is pressed, the input pin is connected to Vcc and it will read a logical high voltage (1).

Pull-down resistor


The pull-down resistor value is chosen such that it provides enough current to the input pin to ensure a stable low voltage when the switch is open, but not so much that it causes current to flow when the switch is closed. This way it "pulls" the voltage down to a known state when the switch is open.

Note: It is important to note that pull-up and pull-down resistors are used in different situations, and the choice of which one to use depends on the circuit and the desired behavior.

5. Connect Arduino with the push button and led

To connect an Arduino with a push button and a LED, you will need to do as in the diagram below :

Connect Arduino with the push button and led


In your Arduino code, set the digital pin as an “INPUT”  for the button and another digital pin as an “OUTPUT”  for the LED

    // initialize the pushbutton pin as an input:

    pinMode(11, INPUT);

    // initialize the LED pin as an output:

    pinMode(7, OUTPUT);

 

Use the digitalRead() function to detect the button press.

digitalRead(11),

Use an if statement to check if the button is pressed, and if it is, use the digitalWrite() function to turn on the LED.

    if (digitalRead(11) == HIGH) {

      // the button is pressed

      digitalWrite(7, HIGH);

    }

Use another if statement to check if the button is not pressed, and if it is not, use the digitalWrite() function to turn off the LED.

    if (digitalRead(11) == LOW) {

       // the button is pressed

      digitalWrite(7, LOW);

   }

It's important to use a pull-up or pull-down resistor to prevent the floating state of the input pin.

The final code is as below :

  void setup() {

    // initialize the pushbutton pin as an input:

    pinMode(11, INPUT);

    // initialize the LED pin as an output:

    pinMode(7, OUTPUT);

  }

  void loop() {

    if (digitalRead(11) == HIGH) {

      // the button is pressed

      digitalWrite(7, HIGH);

    } else {

      // the button is not pressed

      digitalWrite(7, LOW);

    }

  }

lighting a led with push button


This is a basic example, you can add more functionality and complexity to the code and circuit as per your requirement.

Comments

Popular posts from this blog

Arduino LED Blink

Arduino Uno Board