A HITI Signal Filter is a variable which lets you filter your data on the fly while you receive them. It is ideal to filter a continuous data stream, and it is particularly useful for filtering data from your sensors to remove electrical noise or to smooth low-resolution data.
To illustrate this, let’s use a Signal Filter to average and smooth data from a TMP36 temperature sensor. Any analog input on an Arduino Uno has a resolution of 1024 points which gives a temperature resolution of 0.488°C for this sensor. The average filter will enable to increase this resolution.
Code
Upload this sketch : 1_SmoothingSensorData
We start our code by including the HC_SignalFilter.h library and by creating a HITI Signal Filter variable (HC_SignalFilter filter).
We also assign several HITI Data to control and monitor different parameters from HITIPanel :
- Temperature (in V and in °C)
- Virtual switch to activate the filtering
#include <HITIComm.h> #include <HC_SignalFilter.h> // pins assignment const int pin_TemperatureSensor = A1; // Analog Data assignment const int ad_Temperature_voltage = 0; // Temperature reading (converted in V) const int ad_Temperature_celsius = 1; // Temperature reading (converted in °C) // Digital Data assignment: const int dd_ActivateFilter = 0; // Virtual Switch // HITI Signal Filter HC_SignalFilter filter; void setup() { // initialize library HC_begin(); // increase the buffer size to improve filtering (default: 10, max: 255) filter.setBufferSize(50); }
Inside the setup(), we increase the filter buffer size to improve filtering. By default, the filter uses the last 10 input data to calculate its output. Here we ask to use the last 50 input data.
Inside the loop(), we start by reading the raw sensor values (0 - 1023) and we then convert them in volt assuming the temperature sensor is powered with +5V. If you are using a different power supply value, please adapt the code accordingly.
Following this first conversion, we perform a second conversion in degrees Celsius based on the values in volt. The formula uses parameter values for a TMP36 sensor : this sensor provides a voltage of 750mV at 25°C which varies by 10mV/°C. If you are using a different temperature sensor, please have a look at its datasheet to find the equivalent parameters.
Afterwards, we apply an average filter on the incoming temperature data (in °C) when the “ACTIVATE FILTER” virtual switch is activated from HITIPanel.
Finally, we display the temperature values (in V and in °C) in HITIPanel.
void loop() { // communicate with HITIPanel HC_communicate(); // read sensor raw values int rawData = analogRead(pin_TemperatureSensor); // convert to voltage V (using 5V power supply) float voltage = ((float)rawData / 1024.0) * 5.0; // convert to degrees °C (TMP36 sensor => +10mV/°C, 750mV at 25°C) float celsius = (voltage - 0.5) * 100; // if the Virtual Switch is activated from HITIPanel if (HC_digitalDataRead(dd_ActivateFilter) == HIGH) { // filter value celsius = filter.average(celsius); } // display values in HITIPanel HC_analogDataWrite(ad_Temperature_voltage, voltage); HC_analogDataWrite(ad_Temperature_celsius, celsius); }
Control Panels
1) Display the DATA Control Panels.
2) The temperature data are displayed. In our case, the room temperature is 28.613°C, and the corresponding voltage on Analog Input 1 is 0.786V.
Chart
1) Display the Chart window.
2) Start data acquisition. New data are added every 10ms.
3) In the DATA Control Panels, click on “ACTIVATE FILTER” to activate the average filtering of the temperature.
4) Stop data acquisition and zoom in to have a closer look at the temperature values. As expected, the filter has increased the resolution of the measured data.
5) Clear data and perform a new data acquisition. This time, heat the sensor with your fingers. Then check the effect of the filter. Data quality and reading are clearly improved by the filter.
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article