# read and debounces a mechanical switch

#### anukalp

Joined Jul 28, 2018
146
I am looking help to write code for switch

as I know , we read and debounce a mechanical switch as follow

1. if switch is not pressed, return switch not pressed
2. if switch is pressed, wait for the debounces period
Then
a. if switch is no longer preesed , return switch not pressed
b. if switch is still pressed , return switch pressed

I am converting this logic into code

Code:
void main(void)
{

while(1)
{

}
}

int switch_get_input (int debounces_period )
{
return_value = switch_not_pressed;

if (switch_pin == 0) // switch is pressed
{

debounces_time(debounces_period);

if (switch_pin ==0)
{

return_value = switch_pressed;
}
}
return return_value;
}
My question is : What would be the best way to do this in coding

#### AlbertHall

Joined Jun 4, 2014
9,999
Be aware that switches bounce when opening as well as closing.

#### AlbertHall

Joined Jun 4, 2014
9,999
The really simple way is described in the last answer here.

#### John P

Joined Oct 14, 2008
1,808

#### anukalp

Joined Jul 28, 2018
146
No need to go outside AAC. The issue was very well covered in this thread, especially (ahem) in my own contributions.

But experts agree: the way to debounce a switch is simply to read it at intervals longer than the contact bounce time, but faster than a human can press it. Thirty milliseconds seems to be about right.
I have looked into this thread and I don't understand coding in the post

i have made my attempt to understand in my first.. Is it correct logic

Joined Mar 10, 2018
4,057
I have measured crappy switches > 100 mS bouncing. I generally use 200 mS
for this reason.

Above basic approach. One can modify to debounce out before action is
performed.

Regards, Dana.

#### BobaMosfet

Joined Jul 1, 2009
1,117
@anukalp

If you want to debounce a switch, the first thing you should do is look at the voltage of the switch, how it behaves with an oscilloscope. Look at the period it takes the switch to settle, then you're not guessing. And also, you want to know how long the switch will be closed for so you can determine your test-period length.

Then, take that knowledge and collect several samples in a row of the switch- usually 3 to 100 in most cases, within the amount of time at least as long as the switch debounce time requires. Then you check all values and make sure they are all the same. If they are the same, then a state was maintained for a long enough time to indicate a stable state. Make your decision based on state. If the values are not the same, restart the count at zero.

200ms is a lot of time, that is 1/5th of a second. Fastest human response recorded is 10ms, in case your switch is to be human-activated, you want to be aware of such ergonomics. Engineering is about evaluating limits and finding a compromise between them that is robust and reliable.

Last edited:

#### jpanhalt

Joined Jan 18, 2008
9,659
An alternative to software and discrete hardware debouncing is a specialized debouncing IC. I use the MAX6816/6817/6818 and MC14490 chips. MAX is generally cheaper. Simple, costs between $1.50 and about$3.00 each, and doesn't waste MCU resources. The MAX6818 provides debouncing for up to 8 switches.

Debouncing the main on/off switch can present its own issues, since the MCU is not running at that time. The MAX16054 is push on/push off and debounce specifically for starting on/off.

As for writing code, the MAX6816/7/8 and MC14490 datasheets may give you some insight. Of course, you could also use a dedicated MCU.

Joined Mar 10, 2018
4,057
@anukalp

If you want to debounce a switch, the first thing you should do is look at the voltage of the switch, how it behaves with an oscilloscope. Look at the period it takes the switch to settle, then you're not guessing. And also, you want to know how long the switch will be closed for so you can determine your test-period length.

Then, take that knowledge and collect several samples in a row of the switch- usually 3 to 100 in most cases, within the amount of time at least as long as the switch debounce time requires. Then you check all values and make sure they are all the same. If they are the same, then a state was maintained for a long enough time to indicate a stable state. Make your decision based on state. If the values are not the same, restart the count at zero.

200ms is a lot of time, that is 1/5th of a second. Fastest human response recorded is 10ms, in case your switch is to be human-activated, you want to be aware of such ergonomics. Engineering is about evaluating limits and finding a compromise between them that is robust and reliable.
I concur 200 mS is a long time, but the switch I was working with was
indeed that crappy. I did not have an option to replace switch, but re-
corded mentally 200 mS was the new worst case problem. Until such
time as I find one that days days to settle.

Regards, Dana.

#### jpanhalt

Joined Jan 18, 2008
9,659
The IC chips mentioned all use about 40 ms. One advantage to a longer delay is if one has some simultaneous, two-switch routines (like to enter a set-up mode).

#### ErnieM

Joined Apr 24, 2011
8,045
No need to go outside AAC. The issue was very well covered in this thread, especially (ahem) in my own contributions.

But experts agree: the way to debounce a switch is simply to read it at intervals longer than the contact bounce time, but faster than a human can press it. Thirty milliseconds seems to be about right.
I can't argue with a thread where I have the last word. ;-)

#### nsaspook

Joined Aug 27, 2009
7,332
The IC chips mentioned all use about 40 ms. One advantage to a longer delay is if one has some simultaneous, two-switch routines (like to enter a set-up mode).
I've used the same set of chips. I've found that separation of debounce to a external chip to be a robust solution in high static/EMI environments.

#### Teljkon

Joined Jan 24, 2019
142
As another resident newbee. I was curious pre ICS and micro controllers what was the analog solution for switch bounce? My very limited understanding would look to an op amp? Like the question said though limited understanding, I expect that I am wrong.

#### jpanhalt

Joined Jan 18, 2008
9,659
As another resident newbee. I was curious pre ICS and micro controllers what was the analog solution for switch bounce? My very limited understanding would look to an op amp? Like the question said though limited understanding, I expect that I am wrong.
Capacitors, resistors and optionally diodes can be used. See: http://www.ganssle.com/debouncing.pdf

However the need for debouncing increased with the advent of logic devices. A few milliseconds of bounce is more than enough time for a short program to execute more than once.

#### MrChips

Joined Oct 2, 2009
21,278
The simple hw solution is to use a SPDT switch and an S-R flip-flop.

#### anukalp

Joined Jul 28, 2018
146
The simple hw solution is to use a SPDT switch and an S-R flip-flop.
I asked function to read and debounce a mechanical switch but most suggestion are hardware related

I have shown my function to read the switch. How do you code to read and debounce a mechanical switch ?

#### anukalp

Joined Jul 28, 2018
146
I have measured crappy switches > 100 mS bouncing. I generally use 200 mS
for this reason.

View attachment 188284

Above basic approach. One can modify to debounce out before action is
performed.

Regards, Dana.
I think your flow chart is same as I have shown basic function in my first post

#### jpanhalt

Joined Jan 18, 2008
9,659
I have shown my function to read the switch. How do you code to read and debounce a mechanical switch ?
Have you tried writing your own code?

EDIT:
Here's a blog with complete code that may help: https://dannyelectronics.wordpress.com/2016/06/05/a-generic-software-debouncer/

NB: It is called "generic." As the author points out, when one is writing code for debouncing, it is usually tailored to the hardware available and the rest of the program. For example, one author may want the switches to cause an interrupt, another will use polling after a timer/interrupt, and another may just use polling without any interrupt.

Last edited:

#### MrChips

Joined Oct 2, 2009
21,278
Exactly.

It depends on your application and what you are trying to do.
In a simple application, polling and blocking software timer may be acceptable. In other applications, you may want non-blocking routines.
In RTOS, you want switches and timers to be all interrupt driven.

A simple software/hardware solution is to use a hardware timer that interrupts every 30ms at which time you read the switches.
Do not accept a switch state as valid unless you detect the same state twice in succession.