Button Debouncing using PIC16F84 Microcontroller

I apologise for the tardiness of this article; for the next little while, I will probably have to scale them back to once every two weeks or so. The reason is that I am proof reading the β€œGalley Proofs” of my new book, β€œPC PhD”. This book discusses the PC’s hardware and how to develop interfaces (along with the required software) for it. I’ll be putting more information about it on my web page as the August release date comes up.

In my last article, I presented you with a simple circuit as a first application. I want to continue with this circuit to demonstrate five different methods of software based β€œdebouncing”.

A typical switch connection used in a digital application is:

Button Debouncing

In this circuit, the Microcontroller input pin is held high until the button is pressed (the switch is β€œclosed”). When the switch is closed, then a direct path to ground is made with the Microcontroller Pin being β€œpulled” to Ground.

β€œBouncing” is the term used to describe what happens when the switch is closed or opened. Instead of a single, square edge as you may expect, a switch change consists of many short spikes which are a result of the button’s contacts β€œbouncing” during the transition. This is shown in the diagram below:

These spikes can cause a microcontroller to detect many button pressing events. As is shown in the diagram above, when β€œButton Pressed”, the signal actually received by the microcontroller’s input pin looks like the series of β€œspikes” that I have shown. These spikes are characteristic to the button and are generally present for about 10 milliseconds.

In the diagram above, I have shown the Logic β€œHigh”, β€œLow” and β€œThreshold” levels. The β€œHigh” and β€œLow” values should be easy to understand, but I wanted to spend a moment on the β€œThreshold” level. This is the voltage level in which the microcontroller detects the difference between a β€œHigh” or a β€œLow” input. For TTL circuits, this value is generally about 1.4 Volts while for CMOS circuits (as are used in the PICMicro), the threshold is normally one half Vcc, or 2.4 to 2.6 Volts.

While 10 milliseconds is quite short for us humans, it is actually quite long for a digital circuit and the period between spikes is often enough for the application to respond to the request and set up to wait for another button press, which comes as another keystroke.

To the user, it will seem as if the Microcontroller is responding to multiple button presses for each individual one. From the Microcontroller’s perspective, this is exactly what is happening.

To prevent this rapid repeat of button press operations a β€œdebounce” routine like the one shown in psuedo-code below is used to wait for the button transitions to stop for 20 msecs before acknowledging that a button has been pressed:

 

For more detail: Button Debouncing using PIC16F84 Microcontroller


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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.