-->
Page 1 of 4

How to stabilize sensor readings?

PostPosted: Mon Mar 12, 2018 12:50 pm
by Zim
I am working with a temp sensor and would like to do a live average of the last 10 samples to give me a more stable reading. Is this possible in basic? I can't find any examples of how to buffer the output.
Thanks

Re: How to stabilize sensor readings?

PostPosted: Mon Mar 12, 2018 2:54 pm
by Alberto_2
Hi Zim, you can easily accumulate 10 reading in one variable and hence devide it by 10.

Here a snippet:


tval = 0
For i = 1 to 10
tval = tval + sensorValue
Next i

mean = tval/10

Alberto

Re: How to stabilize sensor readings?

PostPosted: Mon Mar 12, 2018 3:18 pm
by rudy
I don't think he was looking to average 10 readings. Instead to average the last 10 readings. And when a new reading is taken it is averaged with the previous 9, with the oldest one being discarded.

I would put the readings into an array. Then average them. When a new reading is read then shift the contents of the array. There are more efficient ways to deal with this but a simple brute force approach will work.

I'm not familiar with Basic so I don't know how to code it.

Re: How to stabilize sensor readings?

PostPosted: Mon Mar 12, 2018 4:03 pm
by lethe
I'd suggest using an exponential filter instead, as it's easy to implement, does not waste memory and easier to tune.
See https://www.megunolink.com/articles/3-m ... surements/ for a nice explanation & comparision of a couple different filters.