pixpi
Posts: 6
Joined: Mon Sep 04, 2017 6:47 pm

Average measurement reading in python

Sun Apr 29, 2018 3:59 am

I'm reading values from an ADC and I have noticed variation between the first few readings, so I want to average 10 sensor reads for a more reliable output. Logically it would look something like this in other languages I know, but this doesn't work in python:

Code: Select all

tmp=0
for(i in 1:10){
tmp1 = mcp.read_adc(7)
tmp = tmp1+tmp
time.sleep(1)
}
print(tmp/10)
Right now I am doing it like this and I hate it, it is lazy code, but I can't find an example like this on stack overflow to work into my use case.

Code: Select all

import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008

# Software SPI configuration:
CLK  = 18
MISO = 23
MOSI = 24
CS   = 25
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)
 
tmp1 = mcp.read_adc(7)
time.sleep(1)
tmp2 = mcp.read_adc(7)
time.sleep(1)
tmp3 = mcp.read_adc(7)
time.sleep(1)
tmp4 = mcp.read_adc(7)
time.sleep(1)
tmp5 = mcp.read_adc(7)
time.sleep(1)
tmp6 = mcp.read_adc(7)
time.sleep(1)
tmp7 = mcp.read_adc(7)
time.sleep(1)
tmp8 = mcp.read_adc(7)
time.sleep(1)
tmp9 = mcp.read_adc(7)
time.sleep(1)
tmp10 = mcp.read_adc(7)
time.sleep(1)

(tmp1+tmp2+tmp3+tmp4+tmp5+tmp6+tmp7+tmp8+tmp9+tmp10)/10

Thank in advance!

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Average measurement reading in python

Sun Apr 29, 2018 4:17 am

_A_______________________________________________
pixpi wrote: I have noticed variation between the first few readings
well, you could ignore the first 5 readings.
_B_______________________________________________
also it might be good to use a easy filter:
viewtopic.php?f=91&t=211126#p1303491
but i must admit that if the start value is bad you see same effects there too.

you can make a FILO array average but that is not better as this filter
_C_______________________________________________
? for loop in python ?

Code: Select all

#!/usr/bin/env python3
# easy for loop
for N in range ( 10,-5,-2 ):    # (start,end(excluded),step)
        print(N)

""" makes:
./py3_forloop.py
10
8
6
4
2
0
-2
-4
"""
_D_______________________________________________
so combine the
* ignore first 5
* read and filter

Code: Select all

#!/usr/bin/env python3
# py3_mini_filter.py
import time
# requires:
#git clone https://github.com/adafruit/Adafruit_Python_GPIO.git
#cd Adafruit_Python_GPIO
#sudo python3 setup.py install
import Adafruit_GPIO.SPI as SPI
# requires:
#git clone https://github.com/adafruit/Adafruit_Python_MCP3008.git
#cd Adafruit_Python_MCP3008
#sudo python3 setup.py install
import Adafruit_MCP3008

# Software SPI configuration:
CLK  = 18
MISO = 23
MOSI = 24
CS   = 25
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

A = 0.1
B = 1.0 - A

F = 50.0
Fin = 0.0

# ignore first 5 readings

for N in range (1,6):
        Fin = mcp.read_adc(7)
        print(N, Fin)

# set good start:
F =  mcp.read_adc(7)

# and read forever
while (True):
        Fin =  mcp.read_adc(7)
        F = A * Fin + B * F
        print(F,Fin)
        time.sleep(1.0)
i do not have that hardware , so code not tested with it, but it seems to run without it.

pixpi
Posts: 6
Joined: Mon Sep 04, 2017 6:47 pm

Re: Average measurement reading in python

Sun Apr 29, 2018 12:15 pm

Hey thank,
That was really insightful, it helped me figure out what was wrong. I needed to add range(1,11) to get the loop to run 10 times, and I don't need to use return in a for loop in python. https://stackoverflow.com/questions/586 ... -for-loops.

Anyway this was my final code:

Code: Select all

import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
import numpy

CLK  = 18
MISO = 23
MOSI = 24
CS   = 25
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

sum_reads=[0]*10
for i in range(0,10):
	sum_reads[i] = mcp.read_adc(7)
	time.sleep(.5)

np.mean(sum_reads)
Thanks again!

Return to “Beginners”