Using ADS1115 instead of Arduino Nano built in ADC

You can operate the ADC with full scale 1.1V (analogReference(INTERNAL);) to lower down the %error to 3.3% as has been shown in post #18.

If 16-bit ADS1115 ADC is used at fullscale -0.256V to +0.256V, gain 16, and error +/- 2LSB, ther %error would be: ?

when my input is 0 to 5V, how can I use a full scale of 1.1V?

Are you expecting 75 amp to flow at the source line? If yes, then you have to choose FS = 5V. In such case, you use ADS1115 at gain 2/3, FS = 12.228, +/-2LSB error. The %error would be:
-- left for you.

Unfortunately only one user @cattledog asked for details of the current range I need.
what other details can I provide to get a solution? I am willing to post the whole code if necessary but the remaining code is about

  1. calculating battery percentage and displaying in percentage and bar graph,
  2. remaining capacity in aH,
  3. calculating the speed of the wheel using interrupt
  4. the odometer displaying the distance travelled.

How did you get the 75 amps figure?

ACS712-30A output = 66 mV/Amp
5V = (5V -2.5)/ 0.066 = 37 Amps
2.5 V = 0 amps
max current to measure is 20 amps one direction and 2 amps other direction
So I need to measure 
2.368 V to 3.82 V

When you have chosen 5V as a full scale, it means you are expecting to flow maximum 5000 mV/66 mV ~= 75 amp current.

With 30A rating of your sensor, you are expecting to get 1980 mV. In that case, you can choose 3.3V as FS of the ADC with this command: analogReference(EXTERNAL); and connect 3.3V-point of NANO at the AREF-pin with 0.1 uF filter capacitor. %error will be: 3.2%.

ACS712 does not work that way.

For 0 current flowing through it input pins say 1&2 the output is 2.5V
If current is flowing from pin 1 to 2 the output increase from 2.5V to +66mV/Amp
If current is flowing from pin 2 to 1 the output decrease from 2.5V to -66mV/Amp

How much is that current?

the 30 Amps version ACS712-30A supports +/- 30 Amps.
In my case when I am using the Battery max 20 amps flows in one direction and when I am charging the battery its 2 Amps in the other direction. When there is a discharge, it only discharges. When charging than only charging

So, you design for max 20A. In this case, the output of ACS712 would be: 20x66 mV = 1320 mV. At 0 current, you get 2.5V which is the inactive logic value and be ignored.

I would say post an schematic, even done with paper/pencil, of the 3 elements and all the connections, including power, Cf, filter...

Regarding the software, I think that the main problem should be there. I see small problems, like the timing, you read current in a later time than voltage. But I imagine they change slowly.
Also, in the if/else you are missing when a is exactly 510.

But, regarding this:

If you look at the datasheet:

Rise time is the time needed by the sensor to settle, I think.
So, 1µF is too high. And for 10nF it will still need more than 30µs, so maybe you are sampling too fast.

Actually I think that you don't need to sample 100 times. Start with just one sample with a known value, for both voltage and current, and see how does it look. Let >1 ms between readings, and more to change reading from one pin to the other.
Then you can think about optimizations, samples and filters.

If results are still bad, build a simple circuit with just the nano and a voltage divider or potentiometer, and test the adc. Verify the result with a multimeter.

20x66 mV = 1320 mV, so that would be 2.5 + 1.32 = 3.82 V

You measure with a Volt Meter. If it is eally 3.82 V, then subtract 2.5V in software to get the actual reading.

Time required by the Arduino Nano internal ADC for one conversion/reading (source ChatGPT)

In an Arduino Nano, which uses the ATmega328P microcontroller, the Analog-to-Digital Converter (ADC) has a maximum conversion time dependent on several factors, primarily the ADC clock speed and the prescaler settings.

The ADC conversion time can be calculated with the following formula:

\[ \text{Conversion Time} = \frac{13 \times \text{ADC Clock Period}}{\text{Prescaler}} \]

Here's a breakdown of the process:

1. **ADC Clock Speed**: The ADC operates on a clock that is derived from the system clock. For the ATmega328P running at 16 MHz, the ADC clock is typically set to 1/16th of the system clock frequency by default, giving an ADC clock of 1 MHz.

2. **ADC Prescaler**: The prescaler determines the division factor of the system clock to get the ADC clock. Common prescaler settings are 2, 4, 8, 16, 32, 64, and 128.

For example, with a prescaler of 64:
- The ADC clock speed would be \( \frac{16 \text{ MHz}}{64} = 250 \text{ kHz} \).
- The ADC conversion time would then be \( \frac{13 \text{ cycles}}{250 \text{ kHz}} = 52 \text{ µs} \).

If you use the default prescaler of 128:
- The ADC clock speed would be \( \frac{16 \text{ MHz}}{128} = 125 \text{ kHz} \).
- The ADC conversion time would then be \( \frac{13 \text{ cycles}}{125 \text{ kHz}} = 104 \text{ µs} \).

So, the maximum time required for one ADC reading in an Arduino Nano is around 104 microseconds with the default settings. Adjusting the prescaler can affect this time, but 104 µs is a good estimate for the default configuration.

The voltage reading is not very important as its the voltage across the main battery which is mostly steady and without throttle/load the voltage readout on the lcd display is ok.

vadc = analogRead(A0);               // Read A0 for voltage
volts += (float)(vadc * 4.89);

The current reading is very important as it measures the current drawn from the battery over time. The current drawn from the battery will be varying depending on the motor speed. The 1 reading and the 100 readings from ADC A3 was once every 200 mSec.
Initially the default external filter capacitor of 1nF was used. Even that was giving the error so increased it to 1uF.
Initially I was taking only 1 reading then read taking multiple readings and averaging it reduces noise.
Discharge current would vary from 0 to 20 amps but charging current would be steady @2 amps so

a = analogRead(A3);          // Read A3 for current
    // ACS712-30A=6.6,20A=10
    if (a >= 511){           // Battery Discharging @ 0 to 20 amps
      current = (float)(current + (((a - 512) * 4.89) / 6.6));     
    }
    else if (a < 510){      // Battery Charging @ 2 amps
      current = (float)(current + (((512 - a) * 4.89) / 6.6));
    }

that is exactly what I am doing

a = analogRead(A3);                 // Read A3 for current
    // ACS712-30A=6.6,20A=10
   // ADC reading of 512 = 512 * 4.89 = 2503 mV
    if (a >= 511){                 // Battery Discharging
      current = (float)(current + (((a - 512) * 4.89) / 6.6));      
    }
    else if (a < 510){         // Battery Charging
      current = (float)(current + (((512 - a) * 4.89) / 6.6));
    }

I think I said that at least two times already.
See post 4, 6, 9, 13

I know you said that but I still need help with the code to incorporate the ads1115 in my code.

Then, I don't understand why you want to use it but it's your choice.
Well maybe someone who has actually used the ADS1115 will come along and provide assistance.

Do you the ADS1115 ADC? If yes, please, give me the following data:

1. When the charging current x1 amp (say 10 or 15 amp), how much DC voltage you have at the output of ACS712?

2. When the charging current x12 amp (say 10 or 5 amp), how much DC voltage you have at the output of ACS712?

Yes, but I'm talking about the time needed by the ACS712 to settle. When nano ADC starts reading each sample, the value should be stable at the output of the ACS712. So > 30µs after the end of the previous sample.

If the ADC works fine for the voltage, then it works also fine for the other signal. It's not the ADC itself.
The voltage signal in the ADC input is an stable signal, but the current signal... is what you have to find out.

To identify if the error is in the ACS712 output, in the noise, or in the ADC+sotfware, try something:

  • Put Cf = 1nF. And maybe remove the D2 zener, for the test.
  • read the current 10 times with a delay of 1millisecond between each sample.
  • print the raw ADC output and your calculation, for each of the 10 samples.
  • Delay 2 seconds and repeat all.

The you will see the noise if the numbers go up and down, or a constant error, or whatever is deviating 15% of the expected result.

EDIT: you could also try to add a 1nF capacitor between the A3 pin and GND, very close to the pin.