# Calculate battery (18650 Li-ion) voltage using ESP8266-12E

i am trying to calculate the battery voltage using ESP-12E module. I am using a voltage divider circuit to calculate the same. Here is how my circuit looks like: And here is how my code looks like:

``````float volt =0.0;
float temp=0.0;
float r1=320000.0;
float r2=100000.0;

void setup(){
Serial.begin(115200);
Serial.println(String(VoltageCalculator()).c_str());
}

float VoltageCalculator(){
}
for(int i = 0;i<=10; i++){
// advance to the next position in the array:

// if we're at the end of the array...
// ...wrap around to the beginning:
}

// calculate the average:
float analogvalue = total / numReadings;

temp = (analogvalue * 3.3) / 1024.0;
volt = temp / (r2/(r1+r2));
}

if(DEBUG_ENABLE){
Serial.println();
Serial.print("Actual Voltage : ");
Serial.print(volt);
Serial.println();
}

float volt_percent;

if(volt == 3.1){
volt_percent = 0;
} else{
float volt_diff = volt - 3.1;
volt_percent = (volt_diff/1.1)*100;
if(DEBUG_ENABLE){
Serial.println();
Serial.print("Voltage percentage : ");
Serial.print(volt_percent);
Serial.println();
}
}
return volt_percent;
}
``````

When i use this circuit with NodeMCU it works very fine, but when it comes to ESP-12E module it do not show the proper voltage. on `Serial.println(analogRead(A0));` always shows 1024.

Any suggestions why this is happening and how can i do this to achieve the most accurate value?

Everything was correct and my circuit is also good. The only difference i found is to this below line:

`temp = (analogvalue * 3.3) / 1024.0; `

Which should be

`temp = (analogvalue * 1.0) / 1024.0;`

We are multiplying because we are referencing the voltage from voltage divider circuit as 1 volt.

After doing this, i am getting the exact values battery voltage which then i convert to percentage.

Thank you!