How to avoid the debouncing when reading a digital input. Library Tools40 Digital Filter

How to avoid the debouncing when reading a digital input. Library Tools40 Digital Filter

In this post it will be shown how to ensure the reading of a digital input using our library Tools40. This library has been developed by Industrial Shields. For more information, check the information of this library in the following post:

Tools40 library, useful tool using our equipments

First of all it is important to know the purpose of our digital filter. Basically it filters the debounces when a digital input changes from HIGH to LOW or vice versa. When the state of the pin is changing, during this change from HIGH to LOW for example the signal makes some debounces, so it can be read a wrong value that can provoke errors as you are reading a data that is not correct. The implementation of this library is based on a simple timer which ignores all the signals inside a period of time once he has read a different input than before. If we put a 200ms period of time and we read a LOW state, having before a HIGH state, the filter ignores during 200ms the input signal, after these 200ms it reads again and if it still a LOW state, the state of the input is changed to LOW.  It is the exactly same idea for when it is changing from LOW to HIGH.

 

Finally it will be shown an example that filters the I0.8 digital input:

#include <Filter.h>

// Create a filter
// Filtering time: 200ms
DigitalFilter<500> filter;

const int pin = I0_8;

void setup() 
{
  // Init Serial port
  Serial.begin(9600L);

  // Set pin as INPUT pin
  pinMode(pin, INPUT);
}

void loop() 
{
  // Read pin value
  int value = digitalRead(pin);

  // Filter it
  int filtered = filter.update(value);

  // Print the filtered value
  Serial.println(filtered);
}

 

 

Comments are closed.