This function, `readBatteryADS`, reads the battery voltage using an ADS1115...
This function, readBatteryADS
, reads the battery voltage using an ADS1115 analog-to-digital converter and returns the measured voltage as a float
. Here's a breakdown of what it does:
-
Read ADC Value:
int16_t adc0 = ads.readADC_SingleEnded(0);
It reads the analog-to-digital conversion from channel 0 of the ADS1115 using the
readADC_SingleEnded
method. This returns a raw digital value (adc0
) corresponding to the input voltage. -
Convert ADC to Voltage:
float voltage = adc0 * 0.1875 / 1000.0;
The raw ADC value is converted to voltage. The constant
0.1875
represents the millivolts per bit for the default gain of2/3
on the ADS1115 (equal to a full-scale range of ±6.144V). Dividing by1000.0
converts millivolts to volts. -
Account for Voltage Divider:
float batteryVoltage = voltage * 2.0;
The measured voltage (
voltage
) is multiplied by 2 to compensate for a voltage divider circuit used in hardware. A resistor divider, consisting of two resistors (e.g., 100kΩ each), reduces the battery voltage by half before it is input to the ADC. Therefore, the result is scaled back up by 2. -
Return Result:
return batteryVoltage;
Finally, the computed battery voltage is returned.
Summary:
This function:
- Reads a single-ended voltage measurement via channel 0 of an ADS1115.
- Converts the ADC value to a voltage.
- Adjusts for a 2:1 voltage divider to calculate the actual battery voltage.
- Returns the calculated battery voltage.