Measuring Voltage on the ESP32 Feather Board

Schematics, Example Code and Q&A
mlitke
Posts: 1
Joined: Mon Nov 30, 2020 2:54 pm

Measuring Voltage on the ESP32 Feather Board

Unread post by mlitke »

I purchased a few of the ESP32 Feather Boards and wanted to share some info from tests I have run to measure the battery voltage. I use the boards to make battery powered sensors that run for several months at a time, so knowing the battery voltage helps me figure out when to charge the batteries before they become to low to power the board. As described here, the ESP32 Feather Board is similar to the Adafruit HUZZAH32 with changes that include a _much_ lower power consumption in deep sleep. There is a resistor divider connected to VBAT and A13, so A13 can be used to measure the voltage. To decrease power consumption, the ESP32 Feather Board also adds the ability to connect/disconnect the sense resistors via pin 2. If using the defaults for the boards ADC precision (12-bit) and attenuation (1.1V), the equation to convert the value read from pin A13 to battery voltage is:

battery voltage = (analogRead (A13) * 1.1 * 2.0 * 3.3) / 4096.0

1.1V -> attenuation
2.0 -> resistor divider halves the voltage
3.3V -> nominal battery voltage
4096.0 -> 12-bit precision

Here is a sample sketch:

Code: Select all

const int BAT_VOLT_PIN = A13;
const int BAT_SWITCH_PIN = 2;

void setup (void){
   
   pinMode (BAT_VOLT_PIN, INPUT);
   pinMode (BAT_SWITCH_PIN, OUTPUT);
   
   Serial.begin (115200);

   return;
}

void loop (void){

   PrintBattVolts ();

   delay (5000);

   return;
}

void PrintBattVolts (void){

   float bat;

   digitalWrite (BAT_SWITCH_PIN, HIGH); // connect sense resistors
   delay (2); // wait a bit for voltage to settle

   bat = ((float)analogRead (BAT_VOLT_PIN) * 1.1 * 2.0 * 3.3) / 4096.0;

   digitalWrite (BAT_SWITCH_PIN, LOW); // disconnect sense resistors (lowers power consumption)

   Serial.print ("Battery voltage: ");
   Serial.print (bat);
   Serial.println (" V");

   return;
}
I have run tests with four different boards, and seen pretty reasonable results. I measured battery voltage using code similar to the above and compared to values read on a multimeter. For all cases the battery voltage read by the multimeter was 3.72V, and the values from the code were 3.87V (4.0% difference), 3.66V (1.5% difference), 3.61V (3.0% difference), and 3.58V (3.8% difference). I also ran a long term test with one of the boards that used an INA219 sensor and code similar to the above to record voltage at regular intervals. Aside from some of the reads being a bit noisy on the ADC, the shape of the voltage vs time plots were pretty much the same with just a constant offset between the two plots (see the attached image).

So far I have been pretty impressed with these boards, thanks for making them!

- Matt
Attachments
tmp.png
Post Reply