#define BATTERY_PIN 35 // Battery sensor of ESP32-PoE #define VCC_PIN 39 // 5V Present sensor of ESP32-PoE float battery_read() { unsigned long sum = 0; // sum of samples taken float voltage = 0.0; // calculated voltage float output = 0.0; // output value // Make 200 measures to avoid error. for (int i = 0; i < 200; i++) { sum += analogRead(BATTERY_PIN); delayMicroseconds(1000); } // calculate the voltage voltage = sum / (float)200; // Max value for 3.6V is 2275 (ADC value / 470k + 470k bridge) output=map(voltage, 0.0f, 2275.0f, 0, 100); if (output < 100) return output; else return 100.0f; } bool external_powered() { // This can used to calculate the 5V level but we will return powered or not unsigned long sum = 0; float voltage = 0.0; float output = 0.0; // Make 100 measures to avoid error. for (int i = 0; i < 100; i++) { sum += analogRead(VCC_PIN); delayMicroseconds(1000); } // Calculate the voltage voltage = sum / (float)100; // Max value for 3.6V is 4095 (ADC value / 470k + 470k bridge) output=map(voltage, 0.0f, 4095.0f, 0, 100); if (output < 100) //return output; return false; else //return 100.0f; return true; } void setup() { Serial.begin(115200); } void loop() { Serial.print("Battery Level: "); Serial.print(battery_read(), 2); Serial.println(" %"); if(external_powered()) Serial.println("Powered by 5V"); else Serial.println("Powered by Battery"); delay(1000); }