Home | pfodApps/pfodDevices | WebStringTemplates | Java/J2EE | Unix | Torches | Superannuation | | About Us
 

Forward Logo (image)      

Debouncing Switches in Arduino

by Matthew Ford 4th October 2021 (originally posted 10th Aug 2014)
© Forward Computing and Control Pty. Ltd. NSW Australia
All rights reserved.

How get a clean switch reading in Arduino

Update 4th October 2018 – V3.3 fixed compiler warnings
Update 24th September 2018 – V3.2 removed _BV macro for non-AVR micros.
Update 28th October 2016 – V3.1 reduces AC switch delay.

Introduction

Mechanical switches do not switch cleanly. The contacts bounce as they open and close. See “A Guide to Debouncing” for examples.

This library, DebouncedSwitch V3.3 will debounce a switch connected from an Arduino digital input to GND. It has separate debounce timings for closing and opening. These are set initially to 50mS in the DebouncedSwitch.cpp file. Increase them if your switch needs it.

The library adds about 350bytes of program and for each switch uses 6 bytes of RAM, so it is light weight.

As well as giving a debounced switch status, the library also indicates when the switch changes state. See the example below.

Installation

Download DebouncedSwitch.zip to your computer, move it to your desktop or some other folder you can easily find and then use Arduino 1.5.5 IDE menu option Sketch → Import Library → Add Library to install it. If you are not using Arduino 1.5.5 IDE, or if you are updating a previously installed library, you have to manually unzip the zip file to your arduino/libaries directory. (Open the Arduino IDE File->preferences window to see where your local Arduino directory is).

Usage

The examples are included with the library.

Here is a simple sketch that turns the LED on when the switch is DOWN and off when the switch is UP (not connected to GND)

#include <DebouncedSwitch.h>
int led = 13;
int D4 = 4; // give the pin a name

DebouncedSwitch sw(D4); // monitor a switch on input D4

void setup() {
  pinMode(D4,INPUT_PULLUP); // input is high when not grounded by the switch
  pinMode(led, OUTPUT); // initially low (OFF)
}

void loop() {
  sw.update(); // call this every loop to update switch state
  
  if (sw.isDown()) { // debounced switch is down
    digitalWrite(led, HIGH);
  } else {
    digitalWrite(led, LOW);      
  }
}



Here is a simple sketch that toggles the LED when the normally open momentary push button is released.

#include <DebouncedSwitch.h>
int led = 13;
int D4 = 4; // give the pin a name

DebouncedSwitch sw(D4); // monitor a switch connected between input D4 and GND

void setup() {
  pinMode(D4,INPUT_PULLUP); // input is high when not grounded by the switch
  pinMode(led, OUTPUT); // initially low (OFF)
}

void loop() {
  sw.update(); // call this every loop to update switch state
  if (sw.isChanged()) { // debounced switch changed state Up or Down
    // isChanged() is only true for one loop(), cleared when update() called again
    if (!sw.isDown()) { // switch was just released
      // toggle the led
      // read current value and set opposite one
      digitalWrite(led, !digitalRead(led));
    }
  }
}

The update() method has to be called for each switch every loop to update the status. The isChanged() method only returns TRUE when the switch changes state from UP to DOWN or from DOWN to UP. The next call to update() clears the isChanged() flag so it is only TRUE for one loop when the switch changes state.

For use of the Arduino name see http://arduino.cc/en/Main/FAQ


The General Purpose Android/Arduino Control App.
pfodDevice™ and pfodApp™ are trade marks of Forward Computing and Control Pty. Ltd.


Forward home page link (image)

Contact Forward Computing and Control by
©Copyright 1996-2020 Forward Computing and Control Pty. Ltd. ACN 003 669 994