Hi,
I would like to read a sensor (acs712) a number of times (20), add them all up, then divide them by the number of reads. So I can get an average. to give me a sort of analogue "debounce". Am I guessing the sum function? I sincerely appreciate any advice you can all give me, I'm incredibly excited about learning all of this!
To use a sum function you would need to store the readings with some digital processing.
If you want the the average without any digital processing, then you could use an analog low-pass filter, which gives a running average.
The lower the filter corner frequency, the longer the averaging time.
How long to take the 20 readings?
Why not simply read via a low-pass RC filter, or use the bandwidth setting pin on the device to slow down its response so it directly produces a time-averaged value?
Why not simply read via a low-pass RC filter, or use the bandwidth setting pin on the device to slow down its response so it directly produces a time-averaged value?
You would need to collect 20 values before you get the first one. You may want the first reading to be A(1),
the second (a(1)+a(2))/2, the third to be [a(1)+a(2)+a(3)]/3 etc until you hit 20; then do the moving average.
I can even provide an example of this in assembler to run on a PIC16F18446 if the OP is interested. This code averages the last 16 reading. It works by writing the readings into a ring buffer. The location that the next reading is written to is just incremented from the last one. when the end location is reached the next reading is written into the first location of the buffer. Actually each location is two bytes so the address pointer is incremented twice for each reading.
One way of having a sort of moving average with a lot less memory is this:-
Have an accumulator, which needs to have 4 more bits than the signal.
Each time there is a reading, divide the accumulator value by 16 and subtract that from the accumulator (leaving the accumulator at 15/16 of its value)
Add the new reading to the accumulator.
The accumulator will end up an average of 16 times the reading.
That will work with any divide, as long as the accumulator is large enough. Power of 2 divides are simple in assembly and there is only one accumulator needed.
It's quite easy to use this technique with to give time constants of many seconds. Also there is no need for the analog components and the accumulator can be preset to certain values.
Components cost money, and take up PCB space - software is free and takes no space - and in this case is extremely trivial anyway. Many modern PIC's even do it in their hardware.
Components cost money, and take up PCB space - software is free and takes no space - and in this case is extremely trivial anyway. Many modern PIC's even do it in their hardware.
Because he now has to measure the voltage 16X faster he needs a faster ADC, and because the data takes 16x more "space" he needs a micro with more RAM, and because the math is more complicated he must upgrade to the next faster CPU, and that new CPU only cones in the next bigger package, and the code just ran off the end of PROM, and now none of it fits on the old PCB, and free is not free.
Whatever ZOE313 want to use he need something to Display the measurement.
The easyest would be an analog panel Meter with Pointer.
That needle is dampded so there should be no average.
With an digital Panalmeter there has to be a Low Pass filter inserted - Like said a few posts before.
Additional it had to be calibrated e.g. with Voltage dividers to show the real Values.
When the knowledge is present, I would use a Microcontroller too.
The measurement can be done by A/D Converter ( Most Microcontroller has them integrated ).
The averaging can be done by simply Additions.
And when make it clever with simple shift operations.
The measured Voltages must be computed to the real measuring values.
You can use the averaged results to feed the Computing of real values without any additional step.
To have the results always present, I would measuring always.
Expected the start, the ring buffer is always filled with actual data.
So You never had to wait for the averageing.
Additional the actual data can be weightened.
The actual Data can be written more than 1 time into the ring buffer.
That will make the masurement faster and give the last measurement more influence.
When Space is the Problem You can add more Measurements in one register and divide it with the numbers of Additions.
To display Your data You have nearly free Choice.
For a lot of Displays You can find free librarys in the Internet.
My favorites for now and that solutions are OLED Displays.
I think the most suitable method depends VERY MUCH in the time between the samples We have no idea if the samples a 1 us apart or 1 year apart.
The OP needs to define the question better.