(solved) ads1115 with smoothing can't read the readings

hi everyone i got ads1115 works fine reading by it self but trying smooth out readings
in serial output you see V1 that's output of analog 0 on ads1115,
i have tried 3 different smoothing codes all get same output 1v

project dual voltage and current meter and with rtc and sd card datalogging , on mega

here's code im testing with

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

Adafruit_ADS1115 ads(0x48);
#define NUM_SAMPLES 64

int sum = 0;                    // sum of samples taken
unsigned char sample_count = 0; // current sample number
float Voltage = 0.0;            // calculated voltage
float batteryVoltage = 0.0;
float V1 = 0.0;



void setup(void) 
{
  Serial.begin(9600);  
  ads.begin();
}

void loop(void) 
{
  int16_t adc0;  // we read from the ADC, we have a sixteen bit integer as a result

  adc0 = ads.readADC_SingleEnded(0);
    V1 = (adc0 * 0.1875)/1000;


 // take a number of analog samples and add them up
    while (sample_count < NUM_SAMPLES) {
        sum += V1 ;
        sample_count++;
        delay(100);
    }
  Voltage = ((float)sum / (float)NUM_SAMPLES);
    batteryVoltage = Voltage * 9.98;
    

  
  Serial.print("AIN0: "); 
  Serial.print(adc0);
  Serial.print("\tVoltage: ");
  Serial.println(Voltage, 7);
  Serial.print("\tV1: ");
  Serial.println(V1, 7);   
  Serial.println();
    Serial.println(batteryVoltage, 7);   
  Serial.println();
  
  delay(200);
}

serial output

AIN0: 7459	Voltage: 1.0000000
	V1: 1.3985625

9.9799995

AIN0: 7459	Voltage: 1.0000000
	V1: 1.3985625

9.9799995
 // take a number of analog samples and add them up
    while (sample_count < NUM_SAMPLES) {
        sum += V1 ;
        sample_count++;
        delay(100);
    }

That code part does not what the comment implies. It sums 64 times the same measurement value with a complete delay of 6.4s. I think you simply forgot to read the other measurements... :slight_smile:

int sum = 0; // holds only a 15-bit positive value

unsigned int sum; // holds 16-bit

to hold a sum of 64 16-bit positive samples, you need

unsigned long sum;

Leo..

pylon:
I think you simply forgot to read the other measurements... :slight_smile:

im not sure what you mean

i tried Wawa code still no change

change output so it simpler to read

AIN0: 7464	Sum 
avg: 1.0000000  \\avg of 64samples
V1: 1.3995000   \\what avg meant to be
Battery Voltage9.9799995 \\avg is wrong so math fails 13.9v

if im using wrong type of code , can recommend any?

V1 = (adc0 * 0.1875)/1000; // here you take a single A/D reading, and multiply it by 0.0001875

while (sample_count < NUM_SAMPLES) { // here you add that single reading 64x to the value "sum"
    sum += V1 ;
    sample_count++;
    delay(100);
}

64 readings divided by 64 is the average of 64 readings.
One reading added 64x to a total, and divided by 64 is still one reading.
Leo..

You take only one reading - move that first line inside the loop and you take NUM_SAMPES readings.

tied moving code inside loop i think, still get same output 1v

 int16_t adc0;  // we read from the ADC, we have a sixteen bit integer as a result

  adc0 = ads.readADC_SingleEnded(0);
    


 // take a number of analog samples and add them up
    while (sample_count < NUM_SAMPLES) {
      V1 = (adc0 * 0.1875)/1000
        sum += V1 ;
        sample_count++;
        delay(100);

int16_t adc0; // we read from the ADC, we have a sixteen bit integer as a result
Are you sure about that comment.

ghost_reaper:
tied moving code inside loop i think, still get same output 1v

 int16_t adc0;  // we read from the ADC, we have a sixteen bit integer as a result

adc0 = ads.readADC_SingleEnded(0);

// take a number of analog samples and add them up
    while (sample_count < NUM_SAMPLES) {
      V1 = (adc0 * 0.1875)/1000
        sum += V1 ;
        sample_count++;
        delay(100);



[/code]

Post the actual code you ran, that is both incomplete and has obvious syntax errors.

No, the read is not inside the loop, its before the loop. It must be inside the loop.
If you loop for a fixed number of times then use a for-loop, not a while-loop, its
more readable.

hi i do thanks for taking your time to help me, but im just abit lost right now sorry
didn't think be this hard to make output more stable

here's full code

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

Adafruit_ADS1115 ads(0x48);
#define NUM_SAMPLES 64
unsigned long sum = 0;                    // sum of samples taken
unsigned char sample_count = 0; // current sample number
float Voltage = 0.0;            // calculated voltage
float batteryVoltage = 0.0;
float V1 = 0.0;



void setup(void) 
{
  Serial.begin(9600);  
  ads.begin();
}

void loop(void) 
{
  int16_t adc0;  // we read from the ADC, we have a sixteen bit integer as a result

  adc0 = ads.readADC_SingleEnded(0);
    


 // take a number of analog samples and add them up
    while (sample_count < NUM_SAMPLES) {
      V1 = (adc0 * 0.1875)/1000
        sum += V1 ;
        sample_count++;
        delay(100);
    }
  Voltage = ((float)sum / (float)NUM_SAMPLES);
    batteryVoltage = Voltage * 9.98;
    

  
  Serial.print("AIN0: "); 
  Serial.print(adc0);
  Serial.print("\tSum avg: ");
  Serial.println(Voltage, 7);
  Serial.print("\tV1: ");
  Serial.println(V1, 7);   
  Serial.print("Battery Voltage");
    Serial.println(batteryVoltage, 7);   
  Serial.println();
  
  delay(500);
}

What you posted is for sure not your actual code. This doesn't compile, there's a syntax error in it.

In this code (if you would fix it and actually start using it - pretending it's the real thing) you're still taking a single reading from our adc, then start to work on that single reading in the while loop. Take you readings inside the loop.

 // take a number of analog samples and add them up
double sum = 0.0; // set the proper type & initialise the variable - that was another error in your code.
int adc0;

for (int i = 0; i < NUM_SAMPLES; i++) { // You ignored the advice to use a for loop!
  adc0 = ads.readADC_SingleEnded(0); // take a sample
  sum += (adc0 * 0.1875)/1000; // Add them all up - this are floating point numbers, won't work in the integer type in your code.
   delay(100); // Doing this 64 times means this loop blocks the device for 6.4 seconds!
}
voltage = sum/NUM_SAMPLES; // Take the average. Note that it's customary to not start variable names with a capital. Style makes your code readable.

That should do.
It's really not hard to take an average of 64 readings, as long as you take 64 readings and not just one!

wvmarle:
What you posted is for sure not your actual code. This doesn't compile, there's a syntax error in it.

sorry didn't notice it failed to upload and and found problem one line i add was missing ;

i tried your code and made all changes to code but avg increases each time

eg..

AIN0: 7461 Sum avg: 1.3988709
 V1: 0.0000000
Battery Voltage13.9607315

AIN0: 7461 Sum avg: 2.7978088
 V1: 0.0000000
Battery Voltage27.9221305

AIN0: 7461 Sum avg: 4.1967382
 V1: 0.0000000
Battery Voltage41.8834457

AIN0: 7460 Sum avg: 5.5956554
 V1: 0.0000000
Battery Voltage55.8446388

just before starts talking samples i add sum = 0.0;
now it works thanks very much :slight_smile: & if i could i give you gold star :slight_smile:

here's code for any one else

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

Adafruit_ADS1115 ads(0x48);
#define NUM_SAMPLES 64
double sum = 0.0;                    // sum of samples taken
unsigned char sample_count = 0; // current sample number
float voltage = 0.0;            // calculated voltage
float batteryVoltage = 0.0;
float V1 = 0.0;
int adc0;


void setup(void) 
{
  Serial.begin(9600);  
  ads.begin();
}

void loop(void) 
{
  int16_t adc0;  // we read from the ADC, we have a sixteen bit integer as a result

sum = 0.0;

 // take a number of analog samples and add them up
   for (int i = 0; i < NUM_SAMPLES; i++) { 
  adc0 = ads.readADC_SingleEnded(0); // take a sample
  sum += (adc0 * 0.1875)/1000; // Add them all up - this are floating point numbers, won't work in the integer type in your code.
   delay(10); // delay * num_samples = 0.64
}
voltage = sum/NUM_SAMPLES; // Take the average. Note that it's customary to not start variable names with a capital. Style makes your code readable.
    
    batteryVoltage = voltage * 9.935; //converts to dc volt's
    
  Serial.print("AIN0: "); 
   Serial.print(adc0);
  Serial.print("\tSum avg: ");
   Serial.println(voltage, 4);  
  Serial.print("Battery Voltage:         ");
    Serial.println(batteryVoltage, 2);   
  Serial.println();
  
  delay(500);
}