Button State Change Detection using Arduino

Once you’ve got a pushbutton working, you often want to do some action based on how many times the button is pushed. To do this, you need to know when the button changes state from off to on, and count how many times this change of state happens. This is called state change detection or edge detection.

Button State Change Detection using Arduino

Circuit

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Connect three wires to the Arduino board. The first goes from one leg of the pushbutton through a pull-down resistor (here 10 KOhms) to ground. The second goes from the corresponding leg of the pushbutton to the 5 volt supply. The third connects to a digital i/o pin (here pin 2) which reads the button’s state.

When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to voltage, so that we read a HIGH. (The pin is still connected to ground, but the resistor resists the flow of current, so the path of least resistance is to +5V.)

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is “floating” – that is, not connected to either voltage or ground. It will more or less randomly return either HIGH or LOW. That’s why you need a pull-down resistor in the circuit.

Schematic

Button State Change Detection using Arduino Schematic

The sketch below continually reads the button’s state. It then compares the button’s state to its state the last time through the main loop. If the current button state is different from the last button state and the current button state is high, then the button changed from off to on. The sketch then increments a button push counter.

The sketch also checks the button push counter’s value, and if it’s an even multiple of four, it turns the LED on pin 13 ON. Otherwise, it turns it off.

Code

/*
State change detection (edge detection)Often, you don’t need to know the state of a digital input all the time,
but you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON.  This is called
state change detection, or edge detection.

[box color=”#985D00″ bg=”#FFF8CB” font=”verdana” fontsize=”14 ” radius=”20 ” border=”#985D12″ float=”right” head=”Major Components in Project” headbg=”#FFEB70″ headcolor=”#985D00″]

Hardware Required

  • Arduino Board
  • momentary button or switch
  • 10K ohm resistor
  • breadboard
  • hook-up wire

[/box]


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

Scroll to Top