ADS1115 - combined Single Ended & Differential Reading - Solved #4

I am using a ADS1115 module from Adafruit to read the current sensor SCT013 and also using the code on this page. The relevant article is this.

I am also using the channel 2 to read a battery voltage in single ended fashion .

The relevant code below is working but the problem is the current readings are all over the place. But when I probe the output from the sensor at the ADS1115 pins A0 and A1 i get a rock steady 0.128V AC and this tallies well with the actual current of about 3.9 A.

I am an hence inclined to think that the code is the culprit. The relevant code to read the ADC is as below :

void readBattVolt()                // Read the battery volt in single ended mode..
{
  ra_adc2.clear();
  for (byte count = 0; count < 25; count++)
  {
   adc2 = ads.readADC_SingleEnded(2);
   ra_adc2.addValue(adc2);
   adcBatt = ra_adc2.getAverage();        
   delay(5);
  }
  Serial.print("Battery % = ");
  Serial.println(adcBatt);
}

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

float getAmpere()                 // Read the current sensor SCT013 in differential mode 
{
  float voltage;
  float ampere ;
  float sum = 0;
  long startMillis = millis();
  int counter = 0;

  while ( millis() - startMillis < 1000 )
  {
    voltage = ads.readADC_Differential_0_1() * multiplier ;
    ampere  = voltage * factor ;
    ampere /= 1000.0;
    sum += sq(ampere);
    counter = counter + 1;
  }
  ampere = sqrt(sum / counter);
  return (ampere);
}

Both the read functions are called once every 5 seconds. Is anything amiss in the procedure ?

And what is the logic of summing the square of the readings and finally extracting the square root of the ( sum / counts)

Mogaraghu:
And what is the logic of summing the square of the readings and finally extracting the square root of the ( sum / counts)

That is called Root Mean Square. It is one way of calculating a mean value.
RMS is used to calculate a mean for AC current which oscillates between a negative and positive peak value. The value calculated is equivalent to a DC current of the same value.
The AC values of power grids are always given as RMS.

Mogaraghu:
The relevant code below is working but the problem is the current readings are all over the place. But when I probe the output from the sensor at the ADS1115 pins A0 and A1 i get a rock steady 0.128V AC and this tallies well with the actual current of about 3.9 A.

AC - Alternating current means all over the place because you are sampling an oscillating signal. The ADC measures the value of a single sample.
Maybe the issue comes from the RMS calculation or sampling.
Can you print the counter value at the end of the getAmpere function? Let this run for a while and see what the min and max are.
What Arduino do you have e.g. how much RAM? If you have enough RAM you could store and send the data to your PC and see if you get a nice sin wave pattern.

@klauss_k

Thanks for the point on RMS. Yes I am aware of it. But my point was on the way it was used here.

Since the data acquiring while loop will run for 1000ms and each acquisition will take about 8ms, we will end up with 125 samples.

We first square each sample and then sum it.

Finally we take the sum and divide by 125 and then extract the square root. ( Method A)

I was wondering if the same end result will be achieved by directly taking a mean of the sum of samples
!! ( Method B )

But afterwards I ran some sample calculations with Random numbers in Excel and find that both methods are not the same. Method A always returns a higher value number.

Ok coming to my problem .... The ADS1115 connects to a ESP32 Adafruit Feather and I nor have to check if its got something to do with the memory related stuff as you suggested.

PS : Are you by chance Mr Kai Klass - someone I know on the 8052.com !!

The problem got solved after I observed the sum and counter in Serial monitor after feeding a known AC signal from a FG.

It turns out that a delay of 5ms between conversions helps a lot. And to maintain the sample size I increased the sampling time to twice that of earlier ... now its 2 sec.

Actually the ADS read is set to take a reading in 8ms as per the Adafruit library and I dont see why it needs this additional time to settle down. Anyway it solved the issue and the current readings are fine now.

Thanks Klauss_K for the tip to watch the variable. Maybe a basic tip but a reminder in right time !!

the code that worked :

float getAmpere()                 // Read the current sensor SCT013 in differential mode 
{
  float voltage;
  float ampere ;
  float sum = 0;
  long startMillis = millis();
  int counter = 0;

  while ( millis() - startMillis < 2000 )   // Was 1000 earlier
  {
    voltage = ads.readADC_Differential_0_1() * multiplier ;
    ampere  = voltage * factor ;
    ampere /= 1000.0;
    sum += sq(ampere);
    counter = counter + 1;
    delay(5);                                       // Intentional delay between acquisitions 
  }
  ampere = sqrt(sum / counter);
  Serial.print(sum);
  Serial.print(" , ");

  Serial.println(counter); 
  return (ampere);
}

Mogaraghu:
Thanks Klauss_K for the tip to watch the variable. Maybe a basic tip but a reminder in right time !!

Your welcome.

Mogaraghu:
But afterwards I ran some sample calculations with Random numbers in Excel and find that both methods are not the same. Method A always returns a higher value number.

Well spotted. There are even more ways to calculate a mean. They all have different use cases and computation requirements. Most non-technical people only learn the Arithmetic Mean because it is easy and useful for dividing food. :slight_smile:

Mogaraghu:
check if its got something to do with the memory related stuff as you suggested.

That was only to see whether you could create a buffer to store all samples from your ADC for debug purpose. You solved the issue, so we do not need that option.

Mogaraghu:
PS : Are you by chance Mr Kai Klass - someone I know on the 8052.com !!

Nope. My name is Klaus.

While RMS was not a knew never got in depth to grasp it as now.

Thanks Klaus for taking the time to reply in detail !!