LilyGO / TTGO-T7-Demo

40 stars 11 forks source link

Battery voltage level reading? #10

Open marc-gist opened 3 years ago

marc-gist commented 3 years ago

Is there a way to detect the battery voltage level so one can see if the battery needs to be charged? if not, please add circuit to next version of this wonderful board. Thank you.

glowingkitty commented 3 years ago

I wnted to ask the same... does anyone know how to get an estimate of the battery charge?

levycarneiro commented 2 years ago

Check this:

https://github.com/levycarneiro/ESP32-Utils/blob/main/FindBatteryPin.ino

Gaai commented 1 year ago

Hi, In that code, I see the comment:

    // On a TTGO T7 v1.5, only pin 35 shows within the 3-5v range with 4.07v, 
    // so probably the Voltage pin. I will confirm this when I attach a battery to it, 
    // and connect a display to display the results.

But checking the schematic: https://github.com/LilyGO/TTGO-T7-Demo/blob/master/t7_v1.5.pdf I don't think I see any resistor divider or something connected to pin 35. Can you confirm that on battery power this pin operates as battery voltage meter and therefor is unusable as adc for other analog voltage readings?

Grey-Lancaster commented 4 months ago

running that code show 4.2 volts on pin 35 with or without a battery

Grey-Lancaster commented 4 months ago

Okay I got this code below. Has web page that show batt voltage when on or off usb. You need a web page as serial monitor will not work on battery :-) As I said above it reads even without the battery. That said off usb power seems fairly arcuate. My problem is it reboots when switching to battery which is useless for capturing uptime. Also in this code is on battery on or not, which does not work. I think if I cared it could be changed to charging or not.

#include <WiFi.h>
#include <WebServer.h>
#include <driver/adc.h>
#include <vector>

// WiFi credentials
const char* ssid = "your_ssid";
const char* password = "your_password";

// Create a web server on port 80
WebServer server(80);

// Store last 10 voltage readings
std::vector<float> voltageReadings;

void setup() {
  Serial.begin(115200);
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  // Initialize ADC for GPIO35 (ADC1_CHANNEL_7)
  adc1_config_width(ADC_WIDTH_BIT_12);
  adc1_config_channel_atten(ADC1_CHANNEL_7, ADC_ATTEN_DB_11);

  // Define the handling function for the root directory
  server.on("/", HTTP_GET, handleRoot);
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient(); // Handle client requests
  delay(1000); // Adjust or remove delay depending on your needs
}

bool isOnBattery(float voltage) {
  // Assume USB power provides a higher voltage close to 5V, battery power is typically below 4.5V
  return voltage < 4.5;
}

void handleRoot() {
  int val = adc1_get_raw(ADC1_CHANNEL_7);
  float measuredVoltage = val * (3.3 / 4095.0);
  float correctedVoltage = measuredVoltage * 2.10; // Apply observational correction factor

  // Update voltage readings
  if (voltageReadings.size() >= 10) {
    voltageReadings.erase(voltageReadings.begin()); // Remove oldest reading
  }
  voltageReadings.push_back(correctedVoltage); // Add new reading

  // Determine power source
  String powerSource = isOnBattery(correctedVoltage) ? "Battery" : "USB Power";

  String html = "<!DOCTYPE html><html><head><meta http-equiv='refresh' content='10'><title>Battery Voltage</title></head><body>";
  html += "<h1>Power Source: " + powerSource + "</h1>";
  html += "<h2>Battery Voltage Monitor</h2>";
  html += "<p>Latest Battery Voltages:</p><ul>";

  // Reverse iterate through the voltageReadings to display the newest first
  for (auto it = voltageReadings.rbegin(); it != voltageReadings.rend(); ++it) {
    html += "<li>" + String(*it, 3) + " V</li>";
  }

  html += "</ul></body></html>";

  server.send(200, "text/html", html); // Send the response to the client
}
Gaai commented 4 months ago

Schematic_update_battery.png

Schematic_update_battery2.png

A while back I emailed them to update the schematic. They never really did. But they confirmed it with these images. Still some mistakes in there. But there is a voltage divider on pin 35 for sure.

Grey-Lancaster commented 4 months ago

I agree pin 35 does something. But it is not really useful if it shows voltage with or without a battery

Gaai commented 4 months ago

It is. But if you want to detect if a battery is connected you need to add some circuitry. Measuring battery level through voltage divider on pin 35 only works on battery power. Because of the charging / protection circuit. It's a while that I really looked at it. Measuring battery voltage worked pretty well for me. Best to take an average. And look at calibration if you want it to be more exact.

Ahmedzafar7600 commented 1 month ago

i got 4.152 V when controller connected with b type port but and about 4.35 something when controller connected with batter but my issue is i have to determine remaning battery as we have in andorid whether its charging or not, remaining batter etc