UnexpectedMaker / tinypico-helper

MIT License
4 stars 2 forks source link

IsChargingBattery boolean stays true even if USB is disconnected #2

Closed ronaldvdmeer closed 3 years ago

ronaldvdmeer commented 3 years ago

Perhaps i'm not understanding this but IsChargingBattery does not seem to change to false (0) when I disconnect USB. At that point the TinyPICO (ESP32-PICO-D4) is completely running from battery. I'm using the VBAT and GND pin for the battery.

https://github.com/UnexpectedMaker/tinypico-helper/blob/f6f992d3f36738796c21d6c7825b8ef641a5f31b/src/TinyPICO.cpp#L197

To give you an idea. A snippet of the script below.

// Deep Sleep configuration
const unsigned long long int uS_TO_S_FACTOR            = 1000000;
int                          TIME_TO_SLEEP             = 900;    // In seconds
int                          DEEP_SLEEP_WHEN_CONNECTED = 60;     // In seconds

TinyPICO tp = TinyPICO();
bool IsChargingBattery = tp.IsChargingBattery();

// When connected to USB we'll change the DEEPSLEEP to X seconds  
if (IsChargingBattery == 1) {
  TIME_TO_SLEEP = DEEP_SLEEP_WHEN_CONNECTED;
} else {
  TIME_TO_SLEEP = TIME_TO_SLEEP;
}

esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_deep_sleep_start(); 
ArgoHG commented 3 years ago

I am having exactly the same issue (using Arduino IDE).

FYI: GetBatteryVoltage works fine.

UnexpectedMaker commented 3 years ago

That's the correct behaviour - it's designed to tell if the battery is being charged or not WHEN USB is connected, not if the USB is connected or disconnected. Sorry.

My latest TinyPICO's have the ability to check isa USB is present or not by reading IO9 - All TinyPICO USB-C have this, and my Micro-B also has it now, but not all stock out in the wild is the latest versions.

if you have an older TinyPICO you can put a voltage divider on the 5V pin and connect that to a spare IO to read to see if there is a 5V source present or not.

ArgoHG commented 3 years ago

Thank UM, I (incorrectly) assumed it was reflecting the Battery Charge LED.

As my wild TP is USBC, I'll read IO9 as you suggest :)

UnexpectedMaker commented 3 years ago

It does, but the STAT pin on the PMIC requires 5V to work ;) So if no 5V is present, it means the PMIC can't do anything, so it won't change STAT state.

ronaldvdmeer commented 3 years ago

That's the correct behaviour - it's designed to tell if the battery is being charged or not WHEN USB is connected, not if the USB is connected or disconnected. Sorry.

Thank you. That explains.

ArgoHG commented 3 years ago

A little snip of Arduino code that determines if USB is connected and if USB is connected and charging... (just to say it - needs pinMode and tp to be declared)

bool charging = tp.IsChargingBattery(); bool plugged = digitalRead(9); if (plugged == 1) { // If USB is plugged in (battery able to charge) Serial.println("USB is plugged in."); } if (plugged == 1 && charging == 1) { // If USB is connected & Battery is charging battCharg = "Yes"; } else { battCharg = "No"; } Serial.print("Battery charging: ");
Serial.println(battCharg);