How to ensure the reading of a digital/analog input library Filter digital filter

January 16, 2019 by
How to ensure the reading of a digital/analog input library Filter digital filter
Alejandro Jabalquinto

Introduction

This post, it will be shown how to ensure the reading of a digital input using our library Filter. This library has been developed by Industrial Shields.​

Requirements

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

 Industrial Shields boards:   Industrial Shields Boards

 Filter IS library:                      Filter Github library

Description

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.


Example

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
  Serial.begin(9600L);
 }

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

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

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

​Search in our Blog

How to ensure the reading of a digital/analog input library Filter digital filter
Alejandro Jabalquinto January 16, 2019

Looking for your ideal Programmable Logic Controller?

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

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


Industrial PLC comparison >>>