Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Hwo to add up analogue inputs to average them?

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!

1 Answer

Relevance
  • oyubir
    Lv 6
    3 weeks ago

    It depends on the rest of your project.

    From your way of describe (in a very digital way: take 20 measures, sum the read, divide total by 20), I assume that you have a microcontroler somewhere.

    Like an ESP32, ESP8266 or an arduino, with an analog input reading (beware of the logic voltage. The acs vcc should be 5v, and thus its output should be between 0V (for -10 amp) and +5V (for +10 amp; assuming that we are talking about de 10 amp version of the acs)

    ESP logic is 3.3V. So, you can't read over that.

    So, if you are measuring currents in both direction, you should either sacrifice part of the range (with a 10A acs, you measure only in the range [-10, 3.2]), or use a set of resistors to remap the voltage on [0,5V]. If you are measuring only one direction direct current, then you can just measure in opposite way (so current between -10A and 0A, mapped from 0V to 2.5V). It needs a 0 calibration, but you should do that anyway. 

    With arduino, input logic is 5V, so you just need a wire.

    Then, it is just a matter of coding exactly what you said.

    Using micropython:

    import machine

    acs = machine.ADC(machine.Pin(0))

    def readAvg(n):

       s=0

       for i in range(n):

          s += acs.read()

       return s/n

    And then do whatever you want with that value.

    You could also use a moving average.

    import machine

    avgWin=20

    avgBuf=[0]*avgWin

    avgIdx=0

    def readMovingAvg():

       global avgIdx, avgBuf

       avgBuf[avgIdx] = acs.read()

       avgIdx = (avgIdx+1)%avgWin

       return sum(avgBuf)/avgWin

    #The 19 first read would be wrong that #way, so you should init it, by reading 19 dummy values

    for i in range(avgWin-1): readMovingAvg()

    And then you have the accuracy of the 20-average, but updated at each reading, not every 20 reading (it still use the computing power of 20 sum for each step tho. But that usually not the problem).

Still have questions? Get your answers by asking now.