I using the standard example of smoothing:


  Reads repeatedly from an analog input, calculating a running average and
  printing it to the computer. Keeps ten readings in an array and continually
  averages them.

  The circuit:
  - analog sensor (potentiometer will do) attached to analog input 0

  created 22 Apr 2007
  by David A. Mellis  <[email protected]>
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.


// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;

void setup() {
  // initialize serial communication with computer:
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;

void loop() {
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = analogRead(inputPin);
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;

  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
  delay(1);        // delay in between reads for stability

to calculate power consumption for a load which changes it's value frequently (sometimes it stops and the consumption goes down to couple of watts).

My only modification of the above code is to pass a calculated power (V*I) value to it instead of the analogRead(inputPin);. The input value for the power calculation works fine because without this code I get the exact good values for power. I have also increased the numreadings from 10->100 and probably will needed to be increased a lot more since I need the average for 1 minute.

There are 2 major issues with this code:

1, When a constant load is attached, which let's say draws 500W the value generated by the code is floating around 312W and never reaches 500.

2, When the load is disconnected it and the current power consumption is 1-2watts standby this code jumps to -312W. I could cast it to be an unsigned integer on the first place but that's not really a good solution.

Anyone knows how to fix this?


  • Consider the size of your array, the size of the Arduino RAM, and the number of items you will need in the array if you want to use this to generate an average over a minute or more. Will it all fit?
    – JRE
    Commented Apr 11, 2019 at 9:18
  • Are your power readings int or float?
    – JRE
    Commented Apr 11, 2019 at 9:19
  • Ten analogRead values summed together can easily overflow int total. Commented May 12, 2019 at 21:17
  • @AnT how did you figure the sum of ten analogReads can overflow an integer? Even if they were the maximum (1023) that would surely easily fit into an integer- even if it were a 16 bit int. I am a bit confused by your comment..
    – GMc
    Commented Jun 12, 2019 at 0:42
  • @GMc: Frankly, I have no idea why I said that. A sum of ten should fit into a 16-bit int. Commented Jun 12, 2019 at 1:31

1 Answer 1


Yes, change the variable type to something that can hold larger numbers, because now adding up 100 measurements will overflow your variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.