Battery Voltage Measurement with watchX

Declare Variables:

// Battery variables
static byte bPin = A11;
static byte battEn = 4;
// Battery range 3.4 - 4.22v
// Voltage divider resistor values.
float R1 = 10000;
float R2 = 10000;
float vDivider;
float voltage;
int percent;

Put in Setup:

pinMode(battEn, OUTPUT);

Use in Loop:

// Battery measurements and calculations
vDivider = (R2 / (R1 + R2));
digitalWrite(battEn, HIGH);
delay(50);
voltage = analogRead(bPin);
voltage = (voltage / 1024) * 3.35;
voltage = voltage / vDivider;
delay(50);
digitalWrite(battEn, LOW);
percent = (voltage - 3.4) / 0.008;

if (percent > 100) {
  percent = 100;
}

For those who doesn’t want linear percentage calculation and looking for non-linear battery percent estimation:

Check the watchX library code I have created. In the source get_batteryPercentance() function returns battery percent with %5 precision.

Here is the thread of the library
Here is the Github Link

PS 1: I did not copy and pasted the relevant part of the code in-case of future upgrade or calibration.

Here is the code:

Serial.println(i);
Serial.println(vraw);
Serial.println(_batteryPercentance[i]);
Serial.println();

Here is a sample output:

11
580
45999

According to code battery percentage is 45999, does it mean %45,999? I am confused.

@mtulu
The usage for get_batteryPercentance() should be like this:

yourVariableInYourCode = get_batteryPercent();

After getting this percent value in your own code (in the named above yourVariableInYourCode) you can do what ever want: print into serial, or oled display or etc. Also make sure that yourVariableInYourCode is int type.

The serial.print lines you were posted was actually for logging purpose. I was thinking about logging functionality into the library but could not develop and evolve the idea yet. So for now; I have just commented out the lines to prevent future ambiguity.