Debouncing switches

This forum is dedicated to the author Joe Pardue as he teaches you about AVR C Programming in his workshop series.

Moderator: joepardue

Post Reply
Posts: 1
Joined: Sat Feb 04, 2012 8:04 am

Debouncing switches

Post by wb8nbs » Mon Feb 06, 2012 7:28 pm

I'm reading Smiley #43, especially the part on Switch Bounce. I cannot find nav.c in the subversion repository.

I did an Arduino project last summer and had debounce problems. Thought I would share my fix. I soldered a 0.1 ufd capacitor across the problem switch. Then with a 10k pullup resistor you see an integrated ramp instead of a series of logic level glitches. Connect the switch to one of the Arduino analog pins (Bpin) and this code does the debounce:

Code: Select all

/***** function to read analog pins as debounced digital inputs ******/
// has 33 percent hysteresis
boolean Button(int Bpin) {
  int level = 600;                   // middle of dead zone
  while(level > 400 && level < 800) {// in dead zone
    level = analogRead(Bpin);        // try again
  if(level >= 800) {
    return HIGH;                     // in upper third
  else {
    return LOW;                      // in lower third
You can make the hysteresis anything you want by changing the constants.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests