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:

enter image description here

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(){

float VoltageCalculator(){
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  for(int i = 0;i<=10; i++){
    total = total - readings[readIndex];
    // read from the sensor:
    readings[readIndex] = analogRead(A0);
    // add the reading to the total:
    total = total + readings[readIndex];
    // advance to the next position in the array:
    readIndex = readIndex + 1;
    // if we're at the end of the array...
    if (readIndex >= numReadings) {
      // ...wrap around to the beginning:
      readIndex = 0;
    // calculate the average:
    float analogvalue = total / numReadings;

    //  int analogvalue = analogRead(A0);
    temp = (analogvalue * 3.3) / 1024.0;       
    volt = temp / (r2/(r1+r2));  

    Serial.print("Actual Voltage : ");
  float volt_percent;

  if(volt == 3.1){
    volt_percent = 0;
  } else{
    float volt_diff = volt - 3.1;
    volt_percent = (volt_diff/1.1)*100;
      Serial.print("Voltage percentage : ");
  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?

1 Answer 1


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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.