Why is Switch Bounce BAD? Learn what it is, and How To Fix it! - In order to fix it, you Have to Know Why it happens, and its effect on microcontroller inputs. Find out Everything you Need to Know Here.



Why is switch bounce bad?
  • Understand the effect of switch bounce on a microcontroller input.

  • Find out why a microcontroller may crash when attaching a button to an input.

  • Find out the real reason for switch bounce.

  • Learn three different solutions to switch bounce.

Why is switch bounce bad? To answer that first you have to know what switch bounce is and what causes it.

switch bounce waveform

The reason for a bouncing switch

Whey you hit any button, on any interface, with any computer you are interacting with a mechanical device. Mechanical interfaces always have unforeseen actions such as lag, inertia or even too fast i.e. they don't operate in exactly the way you thought.

When you push the button down, the button automatically goes back up using a spring that is always pushing back on your pushing of that button.

The spring causes the switch bounce bad problem. As you push down the button the metal contact completes the circuit but a the same time the spring pushes the button back up.

When you hit the button, energy is put into the spring so it can contract and expand really fast and while the button contact is not quite fully pushed down the gap allows the contact spring to disconnect from the circuit.

This causes the push button to generate multiple connections and disconnections to your circuit i.e. it oscillates and causes what is known as switch bounce.

Why is switch bounce bad?: Why you don't think there's a problem

The thing is that if you attached a light (e.g. an LED) in circuit with the switch, you would see the light go on when the button is pushed, and go off when the button is released.

It's all fine and operates exactly as you would expect.

Except that it isn't - but only when you take a closer look at the operation of the switch over a smaller time frame.

Effects of a switch bounce problem

Instead of attaching an LED to the switch, if you attach the switch to the input of a microcontroller and display the readings from that input pin over time you would see a logic level something like:

    111111111110010101101001010010000000000000

In other words you would see a signal that starts inactive at '1' (input pulled high) and active '0' (switch pushed pulling input low), but in between there would be multiple jumps between the initial state '1' and the final resting state '0'.

This can happen over a period of 100s  of milliseconds (millisecond = 1e-3s or 1/1000th of a second).

The problem is that a microcontroller can operate over a period of microseconds (microsecond = 1e-6s or 1/1000000th ).

What happens to a micocontroller when it gets a switch bounce input?

A microcontroller is a dumb device - it does exactly what you tell it to do. So if you set the microcontroller to read the input pin and take action when the input is active there will be problems.

If the input pin oscillated as above then the microcontroller has no way of  knowing that the input is bouncing - it just assumes that you are pushing and releasing the button really fast!

It means you get multiple inputs from a pin when you only pushed it once!

Why is switch bounce bad?: Keypad problem

For the case of an LED output it does not really matter but for everything else it matters a lot. e.g. what if the button was used as a number input from a keypad - it is impossible to enter the correct value.

Why is switch bounce bad?: Interrupt problem

The above example is fairly benign as you would have to finalise the input by pressing another key, and so could start again.

A far worse problem is if the input is attached to an interrupt pin. For example perhaps you want an emergency stop button that reacts quickly to the push button - the interrupt pin is a logical choice.

The problem here is that the microcontroller could be locked up in servicing the interrupt so that it never executes main code i.e. the whole system crashes and stops working altogether!

Why are some switches worse than others?

All input buttons cause switch bounce but some cause more than others. The reason is the physical construction of the push button and the strength of the spring.

pushbuttons red and 6x6

How to solve the switch bounce bad problem.

Why is switch bounce bad?: First observe the switch bouncing.

There are several techniques to solve switch bounce but you should observe the bouncing signal on an oscilloscope to optimise the bounce solution for your particular button.

The following buttons have associated bounce screenshots below.

pushbuttons red and 6x6

From the screen shots below you can see that switch bounce varies with the type of switch you use:

A micro button (only really bounces at the rising edge).

smd switch bounce

The time scale is 1 millisecond.

A physically larger button:

switch
                  bounce example DSO

The time here is 20ms per major graticule so bouncing lasts ~170 milliseconds!


Why is switch bounce bad?: Choose a solution

There are several solutions to switch bounce and you need to choose which you use based on the type of switch bounce you observe.

  • A filter capacitor RC pair can remove the very fast switch bouncing but you have to add a capacitor and resistor the the switch output.

  • Use a software only delay function. You can see that the main bouncing events occur near the beginning and end of the 'push'. So using a delay to ignore the initial bounce does work. In fact this method is used a great deal.

  • There as also a very unusual shift register debounce method that usesa purely software shift register - this also works very well and means you don't need to add any extra hardware.

All these methods are explored in much more detail (hardware setup and  programming and explanation) in this page: Easy Switch Debounce.




Comments

Have your say about what you just read! Leave me a comment in the box below.

Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.



Privacy Policy | Contact | About Me

Site Map | Terms of Use