Measuring current over time w/ Random generating circuit

I'm designing a product that will be using a battery and solar panel (to charge the battery) and I need to know how much current that circuit will draw over a period of time - say an hour. If my circuit drew a constant current, it would be easy math to figure out mAh, but it doesn't because it is very random in it current draw. Here is a plot of what the current draw looks like over time:

So, I have a INA260 in series with my supply and I'm sampling the current average using "RunningAverage" over a minute and also an hour. I have it take a sample every time I do an analogWrite to my LEDs (which is the highest contributor to current draw in my circuit). I then take the numbers generated from my Serial Monitor and paste them into a Excel spreadsheet and then do a sum average on them. I'm getting approximately 586 ma over a minute's time and although I have not run it for an hour and summed those values, I'm assuming that it would be close to the 586 ma.

Here is my circuit:

/* torch sketch  - Series of leds driven by array table of values to produce flame effect
    written by Mark biasotti
*/

#include <Adafruit_INA260.h>                       // to measure current 
#include "RunningAverage.h"                        // to average current over time

Adafruit_INA260 ina260 = Adafruit_INA260();

RunningAverage raMinute(60);
RunningAverage raHour(60);

int samples = 0;                                    // for runningaverage

#define DOWN 0
#define UP  1
#define HOLD 2
#define OFF 0
#define ON 1

int LEDpin = 6;                                     // The PWM pin the for the LED.
#define blower_pin 11
int minbright = 10;                                 // minimum LED brightness.
int maxbright = 255;                                // max LED brightness not used in sketch
int fadespeed = 35;                                 // controls speed of brightness change of LED
int fademultiplier = 2;
unsigned int triggerLEDtime;                        // speed to which switching to next row in the array
int sinestep = 0;
byte array_index;                                   // current row in array

int cumulativema;

int long currentmillis;                             // for non blocking
int long fadespeedprev;
int long LEDholdprev;
int long triggerLEDprev;
int long motorchangeprev;
int long readaverageminprev;
int long readaveragehrprev;

int LEDstatus;
int motorspeedchange;
int motorspeed;

int randomfadetimes[8] = {600, 100, 400, 100, 200, 500, 700, 900};
int randommotorspeed[5] = {60, 130, 80, 60, 120};
// 3 rows 150 values per row - sinepulse plots to emulate flame
int sinesteparray[3][150] = { {10, 11, 14, 20, 30, 49, 80, 160, 209, 230, 244, 255, 255, 255, 253, 247, 238, 221, 184, 106, 66, 47, 37, 29, 22, 21, 21, 23, 26, 31, 39, 49, 66, 94, 147, 212, 236, 246, 251, 254, 255, 255, 255, 255, 255, 254, 254, 253, 252, 249, 246, 241, 235, 227, 215, 200, 181, 159, 138, 119, 102, 89, 77, 68, 60, 53, 48, 43, 40, 37, 35, 33, 31, 30, 30, 30, 31, 32, 33, 35, 38, 41, 46, 51, 57, 65, 74, 87, 102, 121, 144, 169, 193, 213, 227, 237, 244, 249, 253, 255, 255, 254, 252, 248, 242, 234, 224, 210, 191, 167, 138, 107, 79, 56, 41, 31, 24, 21, 21, 24, 29, 37, 49, 65, 85, 107, 133, 159, 185, 208, 228, 241, 251, 255, 254, 247, 233, 211, 178, 140, 106, 79, 59, 43, 32, 23, 17, 13, 11, 10},
  {10, 10, 12, 16, 22, 31, 46, 67, 99, 143, 181, 208, 225, 236, 244, 250, 253, 255, 255, 253, 250, 244, 236, 224, 206, 178, 145, 115, 92, 76, 64, 55, 47, 41, 37, 33, 29, 27, 25, 23, 22, 21, 21, 21, 21, 22, 23, 25, 27, 29, 33, 36, 40, 45, 51, 57, 65, 74, 84, 96, 108, 124, 138, 154, 169, 183, 196, 207, 217, 225, 232, 237, 242, 246, 249, 252, 254, 255, 255, 255, 255, 254, 252, 249, 244, 239, 232, 221, 203, 167, 86, 54, 40, 32, 27, 23, 21, 21, 22, 26, 31, 40, 57, 166, 211, 226, 236, 243, 248, 252, 255, 255, 255, 255, 255, 254, 252, 249, 245, 239, 233, 225, 215, 202, 184, 164, 142, 121, 104, 89, 77, 67, 58, 51, 44, 39, 34, 30, 27, 23, 21, 18, 16, 15, 13, 12, 11, 11, 10, 10},
  {10, 11, 12, 15, 20, 25, 32, 40, 49, 59, 71, 84, 97, 112, 126, 141, 156, 171, 186, 200, 213, 224, 233, 242, 249, 253, 255, 255, 255, 251, 242, 229, 208, 176, 134, 92, 62, 43, 32, 24, 21, 21, 25, 34, 49, 85, 156, 207, 230, 243, 250, 254, 255, 255, 250, 248, 232, 213, 179, 119, 62, 40, 31, 32, 47, 109, 193, 221, 235, 244, 250, 253, 255, 253, 250, 246, 239, 231, 221, 208, 192, 173, 153, 133, 113, 94, 78, 65, 54, 44, 37, 32, 27, 24, 21, 20, 19, 19, 20, 22, 24, 27, 32, 36, 41, 47, 54, 62, 70, 79, 88, 99, 109, 120, 131, 143, 154, 166, 178, 189, 200, 211, 221, 230, 238, 245, 250, 254, 255, 255, 251, 243, 230, 208, 176, 140, 109, 86, 68, 54, 43, 35, 28, 22, 18, 15, 13, 11, 10, 10}
};



void setup() {
  Serial.begin(19200);
  while (!Serial) {                                     // Wait until serial port is opened
    delay(10);
  }
  Serial.println("Adafruit INA260 Test");
  if (!ina260.begin()) {
    Serial.println("Couldn't find INA260 chip");
    while (1);
  }
  Serial.println("Found INA260 chip");

  raHour.clear();                                     // clear buffer for Runningaverage
  raMinute.clear();

  pinMode(LEDpin, OUTPUT);
  fadespeedprev = 0;
  triggerLEDprev = 0;
  motorchangeprev = 0;
  readaverageminprev = 0;
  readaveragehrprev = 0;
  LEDstatus = OFF;
  TCCR2B = TCCR2B & B11111000 | B00000111;            // for motor PWM frequency of 30.64 Hz for D3 and D11
  analogWrite(blower_pin, 150);                       // start blower motor
  delay(4000);                                        // get filament upright to start

}


void sinepulse() {                                                          // per fadespeed advances value in array to determine led brightness
  if ((currentmillis - fadespeedprev >= fadespeed) && (LEDstatus == ON)) {
    if (sinestep > 148) {
      LEDstatus = OFF;
      triggerLEDprev = currentmillis;
      sinestep = 0;
      analogWrite(LEDpin, minbright);
      //Serial.println("led is off");
    }
    else {
      int brightness = sinesteparray [array_index] [sinestep];
      analogWrite(LEDpin, brightness);
      cumulativema = ina260.readCurrent();                                  // get ma value
      sinestep++;

    }
    fadespeedprev = currentmillis;
  }
}

void motorchangetime() {
  if (currentmillis - motorchangeprev >= motorspeedchange) {                // at random intervals change motor speed
    motorspeed  =  randommotorspeed[random(0, 4)];
    analogWrite(blower_pin, motorspeed);
    motorspeedchange = random(2, 6) * 1000;
    motorchangeprev = currentmillis;
  }
}

void loop() {                                                                 // Main loop
  currentmillis = millis();

  sinepulse();
  motorchangetime();

  if (LEDstatus == OFF) {
    LEDstatus = ON;
    //Serial.println("led is on");
    //triggerLEDtime = randomfadetimes[random(0, 7)];
    array_index = random(0, 2);
  }
  
  {
  long rn = (cumulativema);
  raMinute.addValue(rn);
  samples++;
  
  if (samples % 60 == 0) raHour.addValue(raMinute.getAverage());
  
  Serial.print("  raMinute: ");
  Serial.print(raMinute.getAverage(), 1);
  Serial.print("  raHour: ");
  Serial.println(raHour.getAverage(), 1);
}
}

So the question to this forum: is it a fair assumption to assume that if I'm averaging about 586 ma an hour in my circuit that if I want to run this circuit for 4 hours then I need a battery with a capacity of approx. 2500mAh?

I question this because what I really need to know from my circuit is how many milliamps are consumed in a one-hour period. Not sure if just getting the average ma per hour is the same thing?

As far as the battery is concerned, yes. Derate the stated battery capacity by at least a factor of two (or connect two in parallel) to take into account failure of the solar cell to fully charge it, battery aging, etc.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.