In compliance with high industry standards

Browse 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/analog 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 (Using a Digital Filter):

#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); }

 This other example of the Filter.h library shows how to filter an analog input signal ( I0_10 in this example). As you can see, you must define the number of filters and also the sample period (in ms):

#include <Filter.h>

// Create a filter
// Number of samples: 10
// Sample period: 5ms (setting it to 0, samples are taken as fast as possible)
AnalogFilter<10, 5> filter;

void setup() {
  // Init Serial port

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

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

  // Print the filtered value

Looking for your ideal PLC?

Take a look at this product comparison with other industrial controllers Arduino.

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.

Industrial PLC controller comparison >>

Do you want more information?

Just fill the form!

Tell me more!