This function, `readBatteryADS`, reads the battery voltage using an ADS1115...

August 29, 2025 at 03:54 PM

float readBatteryADS() { int16_t adc0 = ads.readADC_SingleEnded(0); float voltage = adc0 * 0.1875 / 1000.0; // ADS1115 default gain = 2/3 => 0.1875 mV/bit float batteryVoltage = voltage * 2.0; // Account for 100k/100k divider return batteryVoltage; }

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:

  1. 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.

  2. 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 of 2/3 on the ADS1115 (equal to a full-scale range of ±6.144V). Dividing by 1000.0 converts millivolts to volts.

  3. 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.

  4. 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.
Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node