logo1
logo2
Contact Us Curdridge Observatory channel on youtube google plus

How to debounce a switch on Arduino in hardware

Just a short note, so I don’t forget how to debounce switches attached to the hardware interrupts on the Arduino. Here is an Arduino sketch showing the problem. On my Arduino Mega 2560 I connect a normally open microswitch between ground and pin 18

#define INTERRUPT_PIN_NUMBER 18
#define INTERRUPT_NUMBER 5
volatile unsigned long counter=0;
void setup()
{

	Serial.begin(57600);
	pinMode(INTERRUPT_PIN_NUMBER, INPUT);
	digitalWrite(INTERRUPT_PIN_NUMBER, HIGH);
	delay(1000);
	attachInterrupt(INTERRUPT_NUMBER, switchISR, FALLING);
}

void loop()
{
	Serial.println(counter);
	delay(1000);
}

void switchISR()
{
	counter++;
}

This code setups pin 18 (interrupt number 5) on the Mega 2560 as an input pin, and turns on the internal pull-up resistor. The function switchISR is attached to this interrupt on falling. i.e. the interrupt will fire when pin 18 is grounded, causing the variable counter to increase.

The loop() procedure just keeps outputting the value of counter so we can see it.

With no hardware debouncing , pressing the switch down once will usually cause counter to increase by about 10 or 20, due to the bouncy nature of the switch.

Debouncing switches in software is a dreadful kludge in my view. The microcontroller is still getting loaded by 20 dips into the interrupt service routine instead of just the one.

Simple hardware debouncing consists of

You turn on the Arduino and the capacitor gets charged up via the internal pull-up and the interrupt is pulled high. Press the switch and the capacitor will drain via our resistor (being much lower than the internal pullup value). Obviously the switch bouncing will cause a few delays in this draining, but generally the voltage on the input pin will fall. Any tiny rises due to bouncing on the way down will get filtered out by the Schmitt trigger built into the Arduino input.

Fiddling with the values will create different behaviour. The internal pullup resistor is between 20k and 50k which means the capacitor will take between 20 milliseconds and 50 milliseconds to recharge enough the toggle the Schmitt trigger back to high. So to be safe the component values above are only good for switching at a couple of Hz. Microswitches are usually fired in isolated instances (i.e. an end stop) and therefore these values are good.

If you are trying to count a 100kHz encoder signal… well, you should not be using an Arduino – there are far better dedicated ICs out there.

Arduino Mega 2560 & 1280 interrupt pin numbers

Here is a list of the interrupt pins on the Arduino Mega 2560. The pin number is the actual number printed on the header, the interrupt number is the actual number used in the attachInterrupt command parameters.

Pin Interrupt
2 0
3 1
21 2
20 3
19 4
18 5

G182.4+4.3 Supernova Remnant
G182.4+4.3 Supernova Remnant
G156.2+5.7 Supernova Remnant
G156.2+5.7 Supernova Remnant
IC443 Jellyfish Nebula SH-248
IC443 Jellyfish Nebula SH-248
CTA1 G119.5+10.2 Supernova Remnant
CTA1 G119.5+10.2 Supernova Remnant
SH-284 Sharpless 284, Do25, Do 23
SH-284 Sharpless 284, Do25, Do 23
SH-232 Sharpless sh-232, sh-231, sh-233, sh-235
SH-232 Sharpless sh-232, sh-231, sh-233, sh-235
SH2-217 Sharpless 217
SH2-217 Sharpless 217
NGC2174 Monkey Head Nebula, Sharpless 252, Sh2-252
NGC2174 Monkey Head Nebula, Sharpless 252, Sh2-252
SH2-282 Sharpless 282 Sh-282 Sh2-282
SH2-282 Sharpless 282 Sh-282 Sh2-282
NGC1491 Sharpless Sh2-206
NGC1491 Sharpless Sh2-206
HDW3 Hartl-Dengel-Weinberger 3 PN G149.4-09.2
HDW3 Hartl-Dengel-Weinberger 3 PN G149.4-09.2
SH-263 Sharpless 263
SH-263 Sharpless 263
IC410 Sharpless 236, NGC1893
IC410 Sharpless 236, NGC1893
SH-198 Sharpless 198, Soul Nebula, IC1848
SH-198 Sharpless 198, Soul Nebula, IC1848
SH2-212 Sharpless 212, NGC 1624
SH2-212 Sharpless 212, NGC 1624
B33 Horsehead Nebula
B33 Horsehead Nebula
SH2-210 Sharpless 210
SH2-210 Sharpless 210
SH2-200 Sharpless 200
SH2-200 Sharpless 200
IC63 IC59 gamma Cas nebula
IC63 IC59 gamma Cas nebula
SH2-187 Sharpless 187
SH2-187 Sharpless 187
NGC281 IC11, Sharpless Sh2-184, Pacman Nebula
NGC281 IC11, Sharpless Sh2-184, Pacman Nebula
SH2-176 Sharpless 176
SH2-176 Sharpless 176
NGC281 IC11, Sharpless Sh2-184, Pacman Nebula
NGC281 IC11, Sharpless Sh2-184, Pacman Nebula
SH2-173 SH2-173 Phantom of the Opera LBN 593
SH2-173 SH2-173 Phantom of the Opera LBN 593
SH2-69 Sharpless 69, LBN 99
SH2-69 Sharpless 69, LBN 99
SH2-155 Cave Nebuka, Caldwell 9
SH2-155 Cave Nebuka, Caldwell 9
NGC7635 Bubble nebula Sh2-162
NGC7635 Bubble nebula    Sh2-162
SH2-140 Sharpless 140
SH2-140 Sharpless 140
SH2-101 Sharpless 101 Tulip Nebula
SH2-101 Sharpless 101 Tulip Nebula
NGC6888 Crescent nebula Sh2-105
NGC6888 Crescent nebula Sh2-105
IC1396
IC1396
M51 Whirlpool galaxy
M51 Whirlpool galaxy
SH2-224 Supernova Remnant
SH2-224 Supernova Remnant
NGC7293 Helix nebula
NGC7293 Helix nebula
IC1805 Heart Nebula
IC1805 Heart Nebula
SH2-170 Sharpless 170
SH2-170 Sharpless 170
CTB1 Supernova remnant
CTB1 Supernova remnant
SH2-168 Sharpless HII regions. Sh2-169 and Sh2-168
SH2-168 Sharpless HII regions. Sh2-169 and Sh2-168
NGC7380 Wizard Nebula Sh2-142
NGC7380 Wizard Nebula Sh2-142
PN G75.5+1 Soap Bubble Nebula
PN G75.5+1 Soap Bubble Nebula
IC5146 Cocoon Nebula, Caldwell 19, Sh 2-125
IC5146 Cocoon Nebula, Caldwell 19, Sh 2-125
M16 Eagle nebula NGC 6611 sh2-49
M16 Eagle nebula NGC 6611 sh2-49