Theory Lecture Week 4M MES DR Rukonuzzaman - V - 2 - 0
Theory Lecture Week 4M MES DR Rukonuzzaman - V - 2 - 0
Switch Debouncing
When the state of a mechanical switch is changed from open to closed and vice versa, there is often a
short period of time when the input voltage will jump between HIGH and LOW levels a couple of
times before it settles at the applied value. These transitions are called bouncing and getting rid of the
effect of the transitions is called debouncing. If we want to input a manual switch signal into a digital
circuit, we will need to debounce the signal, so a single press doesn't appear like multiple presses.
Alternatively, when a mechanical switch is flipped/pressed, the metal contacts inside may not open
or close cleanly. In the microseconds before the switch achieves a good solid connection, the
switch's contacts may "bounce" against each other, turning the switch on and off in rapid
succession.
Pull-down
Resistor
• The bouncing phenomenon is associated with the Single Pole Single Throw (SPST), Single
Pole Double Throw (SPDT), and similar other types of toggle switches. The bouncing case
data may be analyzed to see how long the switch bounce persists, how wide the individual
bounce pulses are, and how many bounces are observed.
• It is essential that the technique is not fooled by the occasional noise “glitch” or “spike”
caused by-
Crosstalk,
EMI (electromagnetic interference)
RFI (radio frequency interference)
ESD (electrostatic discharge).
• When people push a mechanical switch button, they expect one reaction per push. As the buttons
tend to bounce around when pressed and released, this will mess up the signal from them.
• For example, we have a button that is connected between a voltage supply and the output probe. We
intend to get an output voltage of 5 V (logic 1) when the switch is pressed, and 0 V (logic 0) when it
is not pressed. If we probed the output signal coming from the button during the transition from
pushing it down to letting go, we expect an immediate and clean transition 0 → 1 → 0. What we
end up seeing instead is the picture above. Before the signal settles to a fixed 5 V, it bounces
between the two logic states several times.
• The digital functions in general and microcontrollers don’t like to see input signals wandering
around in the undefined region between a “good” logic 0 and a “good” logic 1. ,
• In 1934, a young graduate student named Otto Herbert Schmitt invented a circuit known as the
Schmitt trigger (this invention was a result of Otto’s study into the propagation of neural
impulses in the nerves of squids- a kind of sea fish). The best way to describe this is by means of a
figure as illustrated in the next slide.
Hysteresis
▪ A standard buffer has only a single switching threshold, which means that a noisy signal
including a signal with a bit of ripple on it, can result in multiple transitions at the output.
▪ In comparison, a Schmitt trigger buffer that is, a buffer with a Schmitt trigger input, has
two thresholds. The output changes only when the input crosses the upper threshold or
the lower threshold. This dual-threshold action is called hysteresis (the dependence of
the state of a system on its history), which implies that the trigger possesses memory.
In the RC network, we use a simple SPST switch debounce circuit and would add a Schmitt
trigger buffer between the RC network and the microcontroller, as illustrated below.
• Bouncing often causes problems in the application, where pressing the switch once should
theoretically cause only one transition.
• If we connect the switch button to a pin with an external interrupt enabled, we will get several
interrupts from pressing the button just once. This behavior is normally not wanted and can cause
multiple false button presses.
• Example: Imagine using a button in a TV remote controller for the selection of a channel. If the
button is not being debounced, one press can cause the remote to skip one or more channels.
• We can remedy this problem by debouncing the switch. There are many ways to do
this. There are two general ways of getting rid of the bounces:
▫ Hardware debouncing: adding circuitry that actually gets rid of the unwanted
transitions.
▫ Software debouncing: adding code/program that causes the application to ignore
the bounces.
▫ Digital Switch Debouncing: achieved in the same way as the software approaches
▫ Switch Debouncer using VHDL: The VHDL entity description is used.
There is one problem: the capacitor exhibits a curve when charging, and parts of this curve will fall
in the region where a logic chip is undecided whether to treat it as a HIGH or LOW. To fix this, a
Schmitt trigger is added to the output.
Remember that the 7414 Schmitt trigger will invert the signal.
Course Teacher: Dr. Md. Rukonuzzaman 17 April 2025 17
Hardware Debouncing
Circuit Design using the 7414 is a Schmitt trigger (inverting type buffer) with the input hysteresis:
𝑉𝑐 = 𝑉𝑓𝑖𝑛𝑎𝑙 1 − 𝑒 −𝑡Τ𝑅𝐶
𝑉𝑓𝑖𝑛𝑎𝑙 is the voltage towards where the capacitor will reach after the capacitor is completely
charged. This will be 𝑉𝐶𝐶 if diode is not used. If diode is used, then 𝑉𝑓𝑖𝑛𝑎𝑙 will be (𝑉𝐶𝐶 −
𝑣𝑜𝑙𝑡𝑎𝑔𝑒 𝑑𝑟𝑜𝑝 𝑎𝑐𝑟𝑜𝑠𝑠 𝑑𝑖𝑜𝑑𝑒).
Numerical Example:
The AHCT version has a lower-threshold voltage of 0.9 V (VTH_L). Let us try with a 0.1 μF capacitor,
since this is small, cheap, and solve for the conditions in which the switch is closed. The capacitor
discharges through R2. If the power supply is 5 V, Vfinal = 5 - 0.7 = 4.3 V, considering voltage drop in
20×10−3
diode of 0.7 V), then 𝑅2 = 0.9 = 128 kΩ. Since a resistor with this value is not available, so we
0.1×10−6 𝑙𝑛
4.3
use 130 kΩ with 5% tolerance.
But the analysis ignores the gate’s input leakage current. A CMOS device like 74AHCT14 dribbles about
1 μA from the inputs. That 130 kΩ resistor will bias the input up to 0.13 V (𝑉 = 𝐼𝑅 = 1 × 10−6 × 130 ×
103 = 0.13V), uncomfortably close to the gate’s best-case switching point of 0.5 V. Therefore, change
the capacitor’s capacitance to 1 μF (0.1 x10 μF) instead of 0.1 μF and use (130 kΩ /10) = 13 kΩ for R2.
𝑉𝑐 = 𝑉𝑓𝑖𝑛𝑎𝑙 1 − 𝑒 −𝑡Τ𝑅𝐶
𝑡
Solving for R1: 𝑅1 = −
𝑉𝑡ℎ
𝐶𝑙𝑛 1 −
𝑉𝑓𝑖𝑛𝑎𝑙
Vfinal is the final charged value of the (5-0.7) = 4.3 V power supply. Vth is the upper threshold voltage (VTH_H) of
20×10−3
the hysteresis comparator (Schmitt trigger), which for our 74AHCT14 is 1.4 So, 𝑅1 = 1.4 = 50.77kΩ
1×10−6 𝑙𝑛 1−4.3
As 51kΩ is the standard value with 5% tolerance, choose R1 = 51 kΩ.
• Debouncing in hardware may give rise to additional cost, and it is more difficult to determine a
good debouncing for all the push button switches that will be used. So, it may be preferable to
debounce the switch in software.
• Debouncing a switch in software is very simple. The basic idea is to sample the switch signal at a
regular interval and filter out any glitches. There are a couple of approaches to achieving this. These
approaches assume a switch circuit like that shown in the explanation of switch bounce- a simple
push switch with a pull-up resistor.
• While numerous algorithms exist to perform the debouncing function, we are going to limit
ourselves to implementing two during the lab. Both approaches use a timer.
Approach 2
The second approach is similar to the first but uses a shift register instead of a counter. The algorithm assumes an
unsigned 8-bit register value, such as that found in 8-bit microcontrollers.
• In the first technique, we wait for a switch closure, then test the switch again after a short delay (15
milliseconds or so). If it is still closed, we determine that the switch has changed state.
• In the second technique, we test the switch periodically to see if it has changed its state.
• When working with microcontrollers, we can deal with switch bounce in a different way that
will save both hardware space and money. Some programmers do not care much about bouncing
switches and just add a 50 ms delay after the first bounce. This will force the microcontroller to
wait 50 ms for the bouncing to stop, and then continue with the program. This is actually not a
good practice, as it keeps the microcontroller occupied with waiting out the delay.
• Another way is to use an interrupt for handling the switch bounce. Be aware that the interrupt
might be fired on both the rising and falling edge, and some microcontrollers might stack up one
waiting interrupt.
void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
digitalWrite(outPin, current_state); // setup the Output LED for initial state
}
Course Teacher: Dr. Md. Rukonuzzaman 17 April 2025 30
void loop()
{
// If we have gone on to the next millisecond millis () function:
if(millis() != time) This function is used to return the number of
{ milliseconds at the time, the Arduino board begins
reading = digitalRead(inPin);
running the current program, that is, this Arduino
if(reading == current_state && counter > 0) function returns the present time in milliseconds
{ from the moment the Arduino board is powered on
counter--;
}
or reset. The return value of millis is the number of
if(reading != current_state) milliseconds through an unsigned long variable
{ since the program in Arduino started.
counter++;
}
// If the Input has shown the same value for long enough let's switch it This number overflows i.e., goes back to zero after
if(counter >= debounce_count) approximately 50 days.
{ millis() function Syntax. millis(); This function
counter = 0;
current_state = reading;
returns milliseconds from the start of the program.
digitalWrite(outPin, current_state);
}
time = millis();
}
}
Course Teacher: Dr. Md. Rukonuzzaman 17 April 2025 31
Software Debouncing
• The following program toggles two LEDs connected to a PIC microcontroller.
// INCLUDES
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
// CONFIG
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
#pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = ON // Low-Voltage Programming Enable bit (RB4/PGM pin has PGM function, low-voltage programming enabled)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
// DEFINITIONS
#define _XTAL_FREQ 4000000
#define LED1 PORTBbits.RB3
#define LED2 PORTBbits.RB2
#define BTN PORTBbits.RB5
// MAIN PROGRAM
int main(int argc, char** argv) {
// Comparators off
CMCON = 0x07;
}
return (EXIT_SUCCESS);
}