Bookmark this thread in your browser, or click on your icon a couple of times till you come to a page with “Summary” and “Activity” on it, where some/all your posts are listed, and find it that way.

If you’re away for a while, I’ll post my version in full here. It’s not tested, but I’m bound to lose it if I don’t.

#include <ESP8266WiFi.h>
#include <Wire.h>
#include <Adafruit_ADS1015.h>

Adafruit_ADS1115 ads;  

double offsetI;
double filteredI;
double sqI,sumI;
int16_t sampleI;
double Irms;
double squareRoot(double fg)  
{
  double n = fg / 2.0;
  double lstX = 0.0;
  while (n != lstX)
  {
    lstX = n;
    n = (n + fg / n) / 2.0;
  }
  return n;
}

double calcIrms(unsigned int Number_of_Samples, byte inputChannel)
// A byte is unsigned, 8 bits wide. You'll only ever need 2 bits here (inputs 0-3)
{
  float multiplier = 0.02F;    
  for (unsigned int n = 0; n < Number_of_Samples; n++)
  {
    sampleI = ads.readADC_SingleEnded(inputChannel);

    //  Digital low pass filter extracts the 2.5 V or 1.65 V dc offset, 
    //  then subtract this - signal is now centered on 0 counts.
    offsetI = (offsetI + (sampleI-offsetI)/1024);
    filteredI = sampleI - offsetI;
    
    // Root-mean-square method current
    // 1) square current values
    sqI = filteredI * filteredI;
    // 2) sum 
    sumI += sqI;
  }
  
  Irms = squareRoot(sumI / Number_of_Samples)*multiplier; 

  //  Reset accumulators
  sumI = 0;
  //--------------------------------------------------------------------------------------       
 
  return Irms;
}

void setup() {
  Serial.begin(9600);
  delay(10);
  ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  ads.begin();
}

void loop() {
     
  double IrmsA = calcIrms(1480, 0);
  double IrmsB = calcIrms(1480, 1);     // I'm guessing this is the second channel
  Serial.print(IrmsA); 
  Serial.print("  ");
  Serial.print(IrmsB); 
  Serial.println(" "); 
}

/**********************************************************************
 
On a practical level, you will need to change "1480" to a value which gives you 
the number of samples as close as possible to a whole number of mains cycles, 
and about 200 ms worth, because if you have an "overhang" of say ¼-cycle, you'll 
have an error which varies according to where you catch the mains cycle - unless 
you have so many cycles that the error is negligible.

**********************************************************************/

I’ve got a list of books & on-line resources.

1 Like