Re: Noise on Voltage/Current analog read


Howard Dutton
 

To do this well you'd probably want to change some code from WebAjax:

#if STAT_DC_CURRENT_ANALOG != OFF
  f = toDCAmps(analogRead(STAT_DC_CURRENT_ANALOG));
  strcpy_P(temp1,htmlInnerStatusDCA);     dtostrf(f,6,1,ws1); strcat(ws1,"A"); if (f==invalid) strcpy(ws1,"Invalid"); sprintf(temp,temp1,ws1); client->print(temp);
#endif

...so it takes the analog sample in the main loop, averages, then drops it in a global variable.  The "(99 + 1) / 100" can be "(999 + 1) / 1000" or "(24 + 1) / 25", etc.  The depth of averaging gives the frequency response vs. noise tradeoff.  This is referred to as a rolling average and has the advantage of not requiring a massive array of values like a normal average does.

// global scope
float currentReading = 0.0;

// in the main loop somewhere
static int currentReadingRaw = 0;
currentReadingRaw = analogRead(STAT_DC_CURRENT_ANALOG);
currentReading = (currentReading*99.0 + currentReadingRaw) / 100.0;

........................

// new code for WebAjax.ino
#if STAT_DC_CURRENT_ANALOG != OFF

  f = toDCAmps(round(currentReading));
  strcpy_P(temp1,htmlInnerStatusDCA);     dtostrf(f,6,1,ws1); strcat(ws1,"A"); if (f==invalid) strcpy(ws1,"Invalid"); sprintf(temp,temp1,ws1); client->print(temp);
#endif

Join onstep-ocs@onstep.groups.io to automatically receive all group messages.