MicroLib_ADC
MicroLib_ADC

If you want to connect sensors with analog output to your Arduino, you should try this library!

With this MicroLib your Arduino board will be able to read data from sensors with analog output. (?!) - Of course your Arduino can already do that out of the box, leveraging the analogRead(), but you are using simple sensors like a photo-resistor, a microphone or a gas-sensor you will quickly discover that the raw values you read from those devices are fluctuating and sometimes there are rogue results.

One way to reduce the fluctuations is to read a couple of values from the sensor and then calculate the average (correct would be the "arithmetic mean") over all values which still is very sensitive to outlier values.

A very good and easy way to truncate outliers is the so called moving average where the arithmetic mean over the last n values is calculated. Its like sliding a window over the current and the past n values so that one single outlier has very little influence on the result.

To use this library in your program, simply include the file MicroLib_ADC.h .

[Library API] [LDR example] Photoresistor@WikipediA Arithmetic mean@WikipediA Moving average@WikipediA
Library API

[Constants] [Types] [Sub-Routines]
[ADC_GetAverageSample] [ADC_MovingAverage]

Constant definitions
  • Sample window-size
    • ADC_WINDOWSIZE - Defines the size of the window for the moving average calculation, in other words how many past values are being stored for the calculation.
      Careful: This value determines how much memory is allocated in the RAM. If not defined in your program, the default value of 10 will be used.
    Quick-Links
    Type definitions
    • struct t_SampleWindowData - This is the data structure used to store the last n values for the calculation of the moving average. The values are organized in a ring-buffer with a wraparound pointer into the buffer-array. (So no expensive memory-shifting operations must be performed); Elements:
      • int16_t Values[ADC_WINDOWSIZE] - The array that stores the values.
      • int8_t ValPtr = -1 - The wraparound pointer into the value array.
      Variables of the type t_SampleWindowData must be defined as static to ensure that the ValPtr is only initialized during the program start.
    Quick-Links
    Sub-routines and pre-processor macros

    • void ADC_GetAverageSample; read n raw values from the analog input and calculate the average, parameters:
      • uint8_t p_Pin - The analog input to be read.
      • uint8_t p_Samples - The number of raw values to be read for calculating the average. You not should read more than 31 values, otherwise an integer overflow may occur.
      Example: v_Value = ADC_GetAverageSample(LDR_PIN,LDR_SAMPLES); // get the average over LDR_SAMPLES values from the ADC
      Quick-Links

    • int16_t ADC_MovingAverage; Calculate the moving average over the last n raw-values, parameters:
      • t_SampleWindowData p_RawData - The address of the structure containing the sample data.
      • int16_t p_NewValue - The new value that is added to the end of the sample-window.
      Example: Serial.println(ADC_MovingAverage(&vs_RawData,v_Value)); // calculate the moving average over the last values and print
      Quick-Links
LDR example program

This simple example program assumes that there is a photo-resistor-bridge connected to an analog input of the arduino. The program reads a number of raw-values from the analog input and calulates the arithmetic mean by using the function ADC_GetAverageSample. Then it submits the averaged sensor value to the function ADC_MovingAverage to calculate the moving average over the last n values. Both results are being printed to the serial monitor, an example output can be seen to the right.

It was a sunny day here in Texas, as can be seen in the output so most of the averaged sensor values were around 800 except for the two rogue values, marked in yellow (i waved my hand over the sensor). You can see how much the moving average has compensated the influence of the outliers: A single drop in the raw values of 240 is reflected by a drop of just 24 in the moving average.

Back to the top of the document.