Battery percentage display using metro mini and pimoroni ads1015

PROJECT DETAILS:
BOARD 1: ADAFRUIT METRO MINI V2
BOARD 2: PIMORONI ADS1015
DISPLAY : ADAFRUIT ILI9341
BATTERY: 12.6V

Connections:
BOARD 2:
5V SUPPLY
BATTERY +VE ON A0 PIN OF ADS1015
GROUND: GROUND

BOARD 2:
A4: SDA OF ADS1015
A5: SCL OF ADS1015
5V: EXTERNAL POWER SUPPLY
GND: BATTERY GND
3V: DISPLAY LED PIN
VIN: VCC DISPLAY
GND: GND DISPLAY
PIN 13,12,11,10,9,8, CONNECTED WITH SCK, SDO, SDI, CS, RESET, DC OF THE DISPLAY

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

#define TFT_CS     10
#define TFT_RST    9
#define TFT_DC     8

Adafruit_ADS1015 ads(0x48); // Create ADS1015 object with I2C address 0x48
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

const float ADC_MAX = 4095.0; // Maximum ADC reading for 12-bit resolution
const float VOLTAGE_MAX = 12.6; // Maximum battery voltage

void setup() {
  Serial.begin(9600);
  while (!Serial);

  // Initialize ADC
  ads.begin();

  // Initialize display
  tft.begin();
  tft.setRotation(3);
  tft.fillScreen(ILI9341_BLACK);
  tft.setTextSize(2);
  tft.setCursor(0, 0);

  Serial.println("Initializing ADC...");
  Serial.println("Initializing Display...");
  Serial.println("Display Initialized.");
  Serial.println("Starting loop...");
}

void loop() {
  // Read raw ADC value
  int16_t adc_reading = ads.readADC_SingleEnded(0);

  // Convert ADC reading to voltage
  float voltage = (adc_reading / ADC_MAX) * VOLTAGE_MAX;

  // Calculate battery percentage
  int battery_percentage = map(voltage, 0, VOLTAGE_MAX, 0, 100);

  // Print raw ADC reading, voltage, and percentage
  Serial.print("Raw ADC Reading: ");
  Serial.print(adc_reading);
  Serial.print(", Voltage: ");
  Serial.print(voltage);
  Serial.print("V, Percentage: ");
  Serial.print(battery_percentage);
  Serial.println("%");

  // Display on TFT
  tft.fillScreen(ILI9341_BLACK);
  tft.setCursor(0, 0);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(2);
  tft.print("Raw ADC Reading: ");
  tft.println(adc_reading);
  tft.print("Voltage: ");
  tft.print(voltage);
  tft.println("V");
  tft.print("Percentage: ");
  tft.print(battery_percentage);
  tft.println("%");

  delay(1000);
}

THE BATTERY MEASURED VOLTAGE IS 12.6 BUT I'M UNABLE TO GET CORRECT VOLTAGE AND BATTERY PERCENTAGE ?
PLEASE HELP ME TO GET CORRECT % AND VOLTAGE AT MY DISPLAY

Have you verified your address jumper?

1 Like

Sorry I do not do word problems but I do read annotated schematics, posting that would help. When typing in upper case that is akin to screaming, why are you screaming? Have you read the forum guidelines? Have you run the I2C scanner and what were the results?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.