Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Raspberry Pi 4B and ADS1115 Based Battery Monitoring System

View and Edit Project In Cirkit DesignerImage of Raspberry Pi 4B and ADS1115 Based Battery Monitoring System

Circuit Documentation

Summary of the Circuit

This circuit is designed to interface an ADS1115 analog-to-digital converter (ADC) with a Raspberry Pi 4B microcontroller. The ADS1115 is used to measure the voltage of a 5s lipo battery through a voltage divider composed of two resistors. The Raspberry Pi reads the ADC values via I2C communication and calculates the battery's voltage and percentage charge. The circuit is powered by the 5V supply from the Raspberry Pi, and the ground is shared across all components.

Component List

ADS1115

Resistor 1

Resistor 2

Raspberry Pi 4B

5s Lipo Battery

Wiring Details

ADS1115

Resistor 1 (65kΩ)

Resistor 2 (333kΩ)

Raspberry Pi 4B

5s Lipo Battery

Documented Code

Raspberry Pi 4B Code (Python)

import time
import board 
import busio 
import adafruit_ads1x15.ads1115 as ADS

i2c = busio.I2C(board.SCL,board.SDA)

ads = ADS.ADS1115(i2c,address=0x48)
R2 = 66000.0  
R1 = 331000.0  

min_adcBatteryVoltage=15.0  
max_adcBatteryVoltage=21.0  
max_adc_adc_voltage=4.096
max_adc_adc_value=32767

def readbattery():
    try:
        adc_value=ads.read(0)
        print(adc_value)
        adc_value=adc_value+9000
        if adc_value==0 or adc_value==32767:
            return None,None,True
        
        measured_voltage=adc_value*(max_adc_adc_voltage/max_adc_adc_value)  
        voltage=measured_voltage*((R1+R2)/R2)  

        battery_percentage = ((voltage - min_adcBatteryVoltage) / (max_adcBatteryVoltage - min_adcBatteryVoltage)) * 100.0
        battery_percentage = max(0, min(battery_percentage, 100))

        return voltage,battery_percentage,False
    except Exception as e:
        print(f"Error reading battery status:{e}")
        return 'check connections'

def getbatterystatus():
    while True:
        battery_voltage=None
        battery_percentage=None
        error=True

        max_adc_attempts=5
        attempts=0
        success=False

        while attempts<max_adc_attempts and not success:
            battery_voltage,battery_percentage,error=readbattery()

            if not error:
                success=True

                print(f"Battery Voltage:{battery_voltage:.2f}V")
                print(f"Battery Percentage:{battery_percentage:.2f}%")

                if battery_percentage<20.0:
                    print("Warning: Battery level is below 20%")
                    return 0
                elif battery_percentage<50 and battery_percentage>20:
                    print('battry less than 50%')
                    return 1
                else:
                    print('good to go')
                    return 2
            else:
                
                attempts+=1
                if attempts<max_adc_attempts:
                    time.sleep(3)  
                break

        if not success:
            
            print("Error: Unable to read battery status. Please check the connection.")

        time.sleep(5)  

def get_battery_percentage():
    battery_voltage,battery_percentage,error=readbattery()
    if not error:
        return battery_percentage
    else:
        return error

ADS1115 Code (Arduino)

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Note: The Arduino code provided for the ADS1115 is empty and does not include any functionality. It is likely that the ADS1115 is being used with its default settings and controlled directly by the Raspberry Pi via I2C, hence no additional Arduino code is necessary.