Look for in our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

How to ensure the reading of a digital input library tools 40 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:

What is Tools40 ? 


Ethernet or 20 I/Os PLC:      Ethernet PLC     20 I/Os PLC    

 Industrial Shields boards:   Industrial Shields Boards

 Tools40 IS library:                 Tools40 Github repository


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;

void setup(){ // Init Serial port Serial.begin(9600L); } void loop(){ // Read pin value int value = digitalRead(I0_8); // Filter it int filtered = filter.update(value); // Print the filtered value Serial.println(filtered); }