Understanding and Using Button Switches

Mastering the short comings of simple button switches. The fundamentals everyone should know.

Oct 27, 2020

18311 views

8 respects

Components and supplies

1

Arduino UNO

1

Tactile Switch, Top Actuated

4

Jumper wires (generic)

1

Resistor 10k ohm

Project description

Code

Button Switches -configurable dual circuit design, Example Sketch

c_cpp

An Arduino sketch that supports two commonly used wiring circuits for button switches and which incorporate two different methods for debouncing the switch. This is the supporting code for the tutorial.

Comments

Only logged in users can leave comments

Anonymous user

2 years ago

Good tutorial, be it clearly from a software engineer viewpoint. Buttons/switches can be easily debounced electrically by adding a 100nF capacitor parallel over the pins. This means you no longer need the debounce routine, making the code cleaner and smaller. How does electrical debounce work? First, an important side-step on microcontrollers and logic signals. Ideally, a logic 0 (zero) is actually 0V and a logic 1 is the same as the power supply voltage (5V on most Arduino models, 3V3 on others). In the real world, it's not that simple and variations occur. Therefore, a logic 0 is seen as a voltage below 0.7V and logic 1 anything above 3.5V (for 5V systems) or 2V (for 3V3 systems). Keep that in mind for a minute. A capacitor can store a finite amount of electricity inside and will charge instantly when connected to a power supply and discharge instantly when connected to a load. Especially on large capacitors the inrush of current on charging can be so high the power supply sees it as a short circuit and shuts down. To prevent this, a resistor is used to limit the current into the capacitor. The loading curve is not linear, but the exact details are beyond the scope of this reply. Because of the lower current, charging a capacitor takes time. How much time depends on the values of both resistor and capacitor. Again, the particulars are not for this reply, use your preferred search engine on the web for an explanation. Let's evaluate circuit C1 above, with a capacitor in parallel over the button. When the circuit is powered up, the resistor will slowly charge the capacitor, meaning the input pin will get pulled down to 0V. On the button press, the capacitor is completely short circuited and the electricity stored instantly discharged. While the contacts are bouncing inside the button, the capacitor is charged and discharged repeatedly, but the charging is too slow, due to the resistor, to cause the input signal to drop below 0.7V or even 3.5 or 2V. So the microcontroller sees a prolonged positive pulse, or logic 1. On circuit C2 a similar thing happens: the capacitor is discharged and charging through the internal resistor is too slow for the input voltage to rise above 2 or 3.5V while the mechanical contacts in the button are bouncing. So the voltage on the input pin stay below 0.7V and the microcontroller sees a prolonged logic 0 pulse on the input. Placement: for best results, place the capacitor near the input pin, although in small circuits it usually doesn't matter too much. If you can't place it near the microcontroller, put it near the button instead. If you have long cables between button and input pin, put the capacitor close to the microcontroller, as this helps combatting EMF influences (noise). Cost: small capacitors can be bought cheap in bulk from various sites like AliExpress and others. And because they're small, they don't take up much space on a PCB, especially if you choose the even smaller SMD sizes,

Anonymous user

2 years ago

And now we have two solutions, one hardware, the other software. Great job guys! Being a belt and suspenders type of guy, I will use both for critical systems like the 'get me a beer and pour it system'.

ronbentley1

2 years ago

True and true, yes it is from precisely the view point you observe - it is a software solution to the problem. This was my objective from the outset, to try and bring together the rationale and methods to address by software design. I am grateful for your comments, it is helpful to appreciate the problem from a hardware perspective. If at the end of the tutorial readers go away with a better understanding then job done! Many thanks

Anonymous user

2 years ago

What code editor do you use for your C code. I used to have a fantastic editor back in the day but can not remember it's name. I am fairly sure it would be available in Mac and Linux varieties so would fit well for me now. I will be giving PlatformIO a try but at first glance didn't see a lot to attract me. One show stopper feature is the ability to hide/show blocks of code down to a single line but generally more like then/else blocks. I call it drilling down.

Anonymous user

2 years ago

Found it, SlickEdit.

sonofcy

3 years ago

What code editor do you use for your C code. I used to have a fantastic editor back in the day but can not remember it's name. I am fairly sure it would be available in Mac and Linux varieties so would fit well for me now. I will be giving PlatformIO a try but at first glance didn't see a lot to attract me. One show stopper feature is the ability to hide/show blocks of code down to a single line but generally more like then/else blocks. I call it drilling down.

sonofcy

2 years ago

Found it, SlickEdit.

Anonymous user

4 years ago

Good tutorial, be it clearly from a software engineer viewpoint. Buttons/switches can be easily debounced electrically by adding a 100nF capacitor parallel over the pins. This means you no longer need the debounce routine, making the code cleaner and smaller. How does electrical debounce work? First, an important side-step on microcontrollers and logic signals. Ideally, a logic 0 (zero) is actually 0V and a logic 1 is the same as the power supply voltage (5V on most Arduino models, 3V3 on others). In the real world, it's not that simple and variations occur. Therefore, a logic 0 is seen as a voltage below 0.7V and logic 1 anything above 3.5V (for 5V systems) or 2V (for 3V3 systems). Keep that in mind for a minute. A capacitor can store a finite amount of electricity inside and will charge instantly when connected to a power supply and discharge instantly when connected to a load. Especially on large capacitors the inrush of current on charging can be so high the power supply sees it as a short circuit and shuts down. To prevent this, a resistor is used to limit the current into the capacitor. The loading curve is not linear, but the exact details are beyond the scope of this reply. Because of the lower current, charging a capacitor takes time. How much time depends on the values of both resistor and capacitor. Again, the particulars are not for this reply, use your preferred search engine on the web for an explanation. Let's evaluate circuit C1 above, with a capacitor in parallel over the button. When the circuit is powered up, the resistor will slowly charge the capacitor, meaning the input pin will get pulled down to 0V. On the button press, the capacitor is completely short circuited and the electricity stored instantly discharged. While the contacts are bouncing inside the button, the capacitor is charged and discharged repeatedly, but the charging is too slow, due to the resistor, to cause the input signal to drop below 0.7V or even 3.5 or 2V. So the microcontroller sees a prolonged positive pulse, or logic 1. On circuit C2 a similar thing happens: the capacitor is discharged and charging through the internal resistor is too slow for the input voltage to rise above 2 or 3.5V while the mechanical contacts in the button are bouncing. So the voltage on the input pin stay below 0.7V and the microcontroller sees a prolonged logic 0 pulse on the input. Placement: for best results, place the capacitor near the input pin, although in small circuits it usually doesn't matter too much. If you can't place it near the microcontroller, put it near the button instead. If you have long cables between button and input pin, put the capacitor close to the microcontroller, as this helps combatting EMF influences (noise). Cost: small capacitors can be bought cheap in bulk from various sites like AliExpress and others. And because they're small, they don't take up much space on a PCB, especially if you choose the even smaller SMD sizes,

sonofcy

2 years ago

And now we have two solutions, one hardware, the other software. Great job guys! Being a belt and suspenders type of guy, I will use both for critical systems like the 'get me a beer and pour it system'.

ronbentley1

2 years ago

Dear dutch_master Your comments are really helpful and I wonder if you would consider submitting these as a publication on this platform. It would be very helpful to the community to see and appreciate things from both points of view. Please do consider. Many thanks

ronbentley1

2 years ago

True and true, yes it is from precisely the view point you observe - it is a software solution to the problem. This was my objective from the outset, to try and bring together the rationale and methods to address by software design. I am grateful for your comments, it is helpful to appreciate the problem from a hardware perspective. If at the end of the tutorial readers go away with a better understanding then job done! Many thanks

ronbentley1

5 Followers

28 Projects

8

11