ARDUINO ADC 1/3: TRIMMED MEAN

 

Here are some notes on how to properly scale and read an analog value; the Arduino code on the right is used as an example and is taken from a test sketch i had written to try out some MPXV7025 pressure sensors.

  1. 1023 or 1024? the way i usually scale an adc reading is (adcvalue+.5)/1024)*Vref, where Vref is the analog reference voltage and adcvalue is the 0 to 1023 reading of the Arduino Uno 10 bit ADC. Note that in this case the program is working with floats.
  2. Multiple samples are taken and a simple truncated mean is calculated to filter out some noise: the for cycle is used to collect a total of, in this case, 7 readings and to compare them recursively in order to store the highest and lowest value recordered (i had to do this in order to mitigate some periodic fluctuations).
  3. The trimmedmean=(trimmedmean-mi-ma)/(samples-2) line finally averages the values and excludes the mentioned min and max ones.
  4. Finally the transfer function of the sensor is used to output pressure values in kPa (the -0.298 is just a zeroing feature for the differential type sensor). 

#define samples 7

#define adcchannel A0

#define del 2

#define s_offset -.298

 

int adc=0;

int mi=0;

int ma=0;

uint8_t cnt=0;

float trimmedmean;

void setup() {Serial.begin(115200);}

void loop() {

mi=2000;

ma=0;

trimmedmean=0 

for (cnt=0; cnt<samples; cnt++){

adc=analogRead(adcchannel);

mi=min(mi,adc);

ma=max(ma,adc);

trimmedmean=trimmedmean+adc;

delay(del);}

trimmedmean=(trimmedmean-mi-ma)/(samples-2); 

Serial.print("                  ");

Serial.println((((trimmedmean+0.5)/1024.0)-0.5)/0.018 + s_offset ,3);

}



#define samples_no 7

#define analog_ch 0

#define readings_delay 50

#define baudrate 9600

#define ref_select INTERNAL

#define ref_voltage 1.1

#define precision 3

#define sensor1_samples_no 7

#define sensor1_ch 0

#define sensor1_delay 50

int sample_buf[samples_no];

 

void setup() {

analogReference(ref_select);  

Serial.begin(baudrate);}

 

void loop() {

update_buf(sample_buf,samples_no,analog_ch);

Serial.println(read_mv(sample_buf,samples_no), precision);

delay(readings_delay);}

 

void update_buf(int buf[], int smpls, uint8_t ch) {

for (int cnt=0; cnt<smpls-1; cnt++) {buf[cnt]=buf[cnt+1];}

buf[smpls-1]=analogRead(ch);

}

 

float read_mv(int buf[], int smpls) {

float mean=0;

for (uint8_t cnt=0; cnt<smpls; cnt++) {mean=mean+sample_buf[cnt];}

mean=mean/samples_no; 

return ((mean+0.5)/1024)*ref_voltage;

}

 

ARDUINO ADC 2/3: MOVING AVERAGE

 

The code on the left performs a moving average using the buffer sample_buf.

 

  1. update_buf shifts the buffer and updates the last value
  2. read_mv averages the buffer and converts counts to mv


ARDUINO 3/3: EMA

 

On the right a function extracted from a project in which a PID is used to control the heater of a thermostated flow cell. The temperature is measured using an LM35, ADC and an EMA which weights the new input with a 1/samples_no constant and the previous average with the complimentary (samples_no-1)/samples_no weight.

 

The exponential moving average is of simple implementation and is also an effective filter, yielding a weighted combination of the previous output (in this case the average value of the sensor) with the last input value, the sum of the weights being 1 so that the steady state output matches the input:

 

out = a * out_previous + (1-a) * val

 

where val represents the currently measured value, or in this case the last ADC reading and a is a constant between 0 and 1 (generally around 0.8-0.99). If the function is called (and the ADC is sampled) at fixed intervals (temp_reading_interval) the constant a can be calculated once and hard coded using the following:

 

a = exp (-T/t)

 

where t is the filter time constant.

 

Let's assume samples_no=10 and thus a=0.9. If the function is called every 800 ms then the time constant of the filter is around 7500 ms.

 

void Update_temperature() {

 

if (millis()-temp_last_time<temp_reading_interval) {return;} 

 

adc=((samples_no-1)*adc+analogRead(lm35_pin))/samples_no;

temp=(100*a_ref*((adc+0.5)/adc_fullscale_counts)-lm35_offset);

 

}

 





Comments: 30 (Discussion closed)
  • #30

    danny (Friday, 22 January 2021 14:23)

    good afternoon. Your ESP32-CAM project is splendid. Well done. Would you be able and willing to share the code. Much appreciated. email: danny.mcalbu@gmail.com. Thanks

  • #29

    moses (Wednesday, 30 December 2020 09:49)

    hello the project is very interesting. I would be very grateful if you could share the code?
    my email: rabbiwest@gmail.com

  • #28

    Tony Le (Sunday, 09 August 2020 04:14)

    hi, I like this 3D printed precision peristaltic pump and want to download this file printing. Can you show to me ? Thanks !
    https://www.youtube.com/watch?v=zLDlSd56vko

  • #27

    Ari (Tuesday, 05 May 2020 17:16)

    Hello Sir, im in interest for make this project , is possible that you share the code or at least could you give me an example, i got some trouble when use universaltelegrambot in esp32 is still cant find solution my email arirehan10@gmail.com , really happy if i get you attention .

    best regard

  • #26

    Javi Morales (Sunday, 26 April 2020)

    Hi,
    This model looks very interesting, congratulations. Would it be possible to share the 3D printer files and also the arduino code with the libraries you used, pleas! Thanks.
    J.M.

  • #25

    antonio couto (Friday, 24 April 2020 02:35)

    Hi,
    Would it be possible to share the arduino code for this project. I would like to implement it on my 3d printer My e-mail is antoniompcouto@gmail.com
    tks

  • #24

    Andre Geyser (Sunday, 12 April 2020 12:57)

    Hi,
    Would it be possible to share the arduino code for this project. I would like to implement it on my ESP32-CAM? My e-mail address: andre.geyser@telkomsa.net

  • #23

    Yanwar (Friday, 06 March 2020 08:23)

    Hello Sir, im in interest for make this project , is possible that you share the code or at least could you give me an example, i got some trouble when use universaltelegrambot in esp32 is still cant find solution.my email yanwareko.m@gmail.com, really happy if i get you attention .

    best regard

  • #22

    tornado64@gmail.com (Friday, 28 February 2020 01:36)

    Hello.
    I like it. Is possible that you share the code?
    my email is: tornado64@gmail.com
    Thanks

  • #21

    bruno martineze (Sunday, 23 February 2020 17:44)

    Congratulations! Im looking for a project like this. Can you share de code? brunomarcio.agl@gmail.com

    best regards,

  • #20

    Reham (Thursday, 26 December 2019 17:18)

    Please, can you send me the CAD files for this design?

  • #19

    Toyran (Thursday, 31 October 2019 10:21)

    Thank you for your quick response !
    I mean the Modular Chemistry Analyzer project,
    just the part of making multiport selector valve.
    I can not understand the mechanism purely.

  • #18

    dptechnology.jimdo.com (Wednesday, 30 October 2019 18:35)

    Dear Toyran,
    this guestbook is about the whole website. Which project are you referring to?

    dp

  • #17

    Toyran (Wednesday, 30 October 2019 18:26)

    Hi, I am trying to make the valve for my project but I can not understand the mechanism by looking at photos . Can you send me the method to make this please ?

    email: e.toyran_@Hotmail.com

  • #16

    David Coronado Ecos (Wednesday, 31 July 2019 16:28)

    hi, your work impresed me , i would like to probe it with my 3d printer , could you please send me the cad files?
    my email: davicoecos@gmail.com

  • #15

    Shawn Alfaro (Sunday, 28 October 2018 14:08)

    Do you offer .stl files for printing? If so, my email issalfaroart@gmail.com

  • #14

    Be (Saturday, 27 October 2018)

    b,van,hattem@xmsnet.nl

  • #13

    Be (Saturday, 27 October 2018 18:27)

    I wil also make this multi port valve for my project.Can you send me the method to make this.Thanks you

  • #12

    SANDEEP KUMAR (Friday, 28 September 2018 10:19)

    I want to make this multi port valve for my project.Can you send me the method to make this.Thanks you
    Email-
    sandeep.ec72@gmail.com

  • #11

    dpt (Saturday, 10 September 2016 17:58)

    Caro Pasquale,
    l'esp8266 è stato programmato usando l'IDE di arduino (https://github.com/esp8266/Arduino). Per quanto riguarda il codice che ho scritto e le librerie che ho usato per i bot, dovrei controllare sul computer, ora non ricordo... Se ti servono mandami una email alla sezione contatti e te le allego appena ne ho l'occasione.
    saluti, dp

  • #10

    Pasquale (Friday, 09 September 2016 13:43)

    Scisa, non avevo capito.
    Mi riferisco al progetto
    IoT: control esp8266 from Telegram Bot for telemetry and domestics applications
    Sto cercando di utilizzare un ESP-01 interfacciata con Arduino per utilizzare I Telegram BOT ma sono in difficolta' perche, mi pare di aver capito, che tutte le librerie disponibili non gestiscono I telegram BOT utilizzando I comandi seriali di ESP-01 ma utilizzano ESP-01 programmato tramite LUA.

    Darei quindi interessato alla tua soluzione.

    Grazie.

    P.S. Molto cortese nella sollecita risposta.

  • #9

    dpt (Friday, 09 September 2016 12:57)

    Ciao Pasquale,
    a quale progetto ti riferisci?
    Il libro dei commenti riguarda tutto il sito!
    dp

  • #8

    Pasquale (Friday, 09 September 2016 11:51)

    Progetto molto interessante.
    E' possibile avere I dettagli ed eventualmente il codice SW?

    Grazie

  • #7

    Marc (Saturday, 27 August 2016 07:59)

    It’s my fortune to go to at this blog and realize out my required stuff that is also in the quality.

  • #6

    dpt (Thursday, 26 May 2016 13:42)

    Hi Anibal,
    contact me tomorrow at info.dptechnology@gmail.com, i will provide you with the libraries used in the project. Regards

  • #5

    Anibal Marcos De Simony (Thursday, 26 May 2016 03:48)


    Caro , come è possibile accedere al progetto software : " ESP8266 e Telegram Bot ". migliori saluti.
    Anibal Marcos
    anibaldesimony@gmail.com

  • #4

    dpt (Tuesday, 05 January 2016 13:38)

    Hi markc,
    a preliminary hydraulic schematic was added yesterday :)

  • #3

    markc (Tuesday, 15 September 2015 16:05)

    When will you add the schematics of the automatic titrator?
    thanks

  • #2

    Federico (Monday, 14 September 2015 18:26)

    What about creating a community around the most ambitious projects? :D

  • #1

    Federico (Monday, 14 September 2015 18:14)

    Well done! This is smth we did need in Rome :)