float Battery_Voltage() {
// Read the battery voltage
float batteryVoltage = ((float)analogRead(34) / 4095.0) * 2.0 * 3.3 * (1100 / 1000.0);
return batteryVoltage;
}
int Battery_Level() {
float batteryVoltage = Battery_Voltage();
// Define the minimum and maximum voltage readings
float minVoltage = 3.78; // Example minimum voltage
float maxVoltage = 4.35; // Example maximum voltage
// Map the battery voltage to the percentage range (0 to 100)
int percentage = map(batteryVoltage, minVoltage, maxVoltage, 0, 100);
percentage = constrain(percentage, 0, 100); // Ensure percentage is within 0 to 100 range
return percentage;
}
bool Battery_Charging() {
// Check if the battery is charging based on voltage threshold
float batteryVoltage = Battery_Voltage(); // Get the current battery voltage
if (batteryVoltage > 4.35) {
return true;
} else {
return false;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
Serial.print("Voltage: ");
Serial.print(Battery_Voltage());
Serial.println("V");
Serial.print("Level: ");
Serial.print(Battery_Level());
Serial.println("%");
Serial.print("Charging:");
Serial.println(Battery_Charging());
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}