Button Interface -I

About The Project

In this project, we will learn how to interface an Arduino Uno board with a button switch and display the result in the Serial Monitor.

Button Switch

A button switch works by allowing or interrupting the flow of electricity in a circuit.

  1. Open State (Not Pressed):
    • When the button is not pressed, the circuit is open, meaning no current flows through it.
  1. Closed State (Pressed):
    • When the button is pressed, it closes the circuit, allowing current to flow through.

Circuit Wiring

Program Code

/*

 wwww.matthewtechub.com

 Basic button interface

 */

const int BUTTON_PIN = 7;      

int lastState = LOW;  // the previous state from the input pin

int currentState;     // the current reading from the input pin

void setup() {

  // initialize serial communication at 9600 bits per second:

  Serial.begin(9600);

  // initialize the pushbutton pin as an pull-up input

  // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.

  pinMode(BUTTON_PIN, INPUT_PULLUP);

}

void loop()

{

  // read the state of the switch/button:

  currentState = digitalRead(BUTTON_PIN);

  if(lastState == HIGH && currentState == LOW)

    Serial.println(“The button is pressed”);

  else if(lastState == LOW && currentState == HIGH)

    Serial.println(“The button is released”);

  // save the the last state

  lastState = currentState;

}

Code Explanation

const int BUTTON_PIN = 7;      

int lastState = LOW;  // the previous state from the input pin

int currentState;     // the current reading from the input pin

  • BUTTON_PIN: The pin number where the button is connected.
  • lastState: This variable holds the state of the button from the previous loop iteration (initially set to LOW).
  • currentState: This variable holds the state of the button during the current loop iteration.

Serial.begin(9600);

  // initialize the pushbutton pin as an pull-up input

  // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.

  pinMode(BUTTON_PIN, INPUT_PULLUP);

  • Serial.begin(9600): Initializes serial communication so you can see the output in the Serial Monitor.
  • pinMode(BUTTON_PIN, INPUT_PULLUP): Sets the button pin as an input with an internal pull-up resistor. This means the pin will be HIGH when the button is not pressed and LOW when the button is pressed.

void loop()

{

  // read the state of the switch/button:

  currentState = digitalRead(BUTTON_PIN);

  if(lastState == HIGH && currentState == LOW)

    Serial.println(“The button is pressed”);

  else if(lastState == LOW && currentState == HIGH)

    Serial.println(“The button is released”);

  // save the the last state

  lastState = currentState;

}

  • currentState = digitalRead(BUTTON_PIN): Reads the current state of the button pin (HIGH or LOW).
  • if (lastState == HIGH && currentState == LOW): Checks if the button was previously not pressed (HIGH) and is now pressed (LOW). If true, it prints “The button is pressed”.
  • else if (lastState == LOW && currentState == HIGH): Checks if the button was previously pressed (LOW) and is now released (HIGH). If true, it prints “The button is released”.
  • lastState = currentState: Updates lastState to the current state to use it in the next iteration of the loop.

Try Yourself

Read the program code shown below. Visualize the output. Then copy the code and upload it to the Arduino Uno board. Is there any change from your expected result? If your expectation and actual result are different, you need a little more attention to your programming.

/*

Testing……..

www.matthewtechub.com

*/

const int BUTTON_PIN = 7;      

int currentState;     // the current reading from the input pin

void setup() {

  // initialize serial communication at 9600 bits per second:

  Serial.begin(9600);

  // initialize the pushbutton pin as an pull-up input

  // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.

  pinMode(BUTTON_PIN, INPUT_PULLUP);

}

void loop() {

  // read the state of the switch/button:

  currentState = digitalRead(BUTTON_PIN);

  if(currentState == LOW)

    Serial.println(“The button is pressed”);

  else if(currentState == HIGH)

    Serial.println(“The button is released”);

}

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!