I would like to measure the battery usage of my ESP32 device. As my device runs in deep sleep mode 99% of the time, it's important that the battery monitoring circuit does not add too much to the current usage while the device is asleep (I will only check battery level during the once a week that the device will be switched on).

I have followed this guide which describes a low power battery usage circuit using a p-channel mosfet to allow current through the voltage divider only when a digital IO pin is turned on.

JEELabs battery monitor

I have attempted to simulate this circuit here, however it does not behave as expected. When I switch the digital IO to HIGH, the p-channel gate is not opened. If I replace the capacitor with a wire, it seems that the circuit at least switches on and off, but it seems there is probably an important reason for including the capacitor here.

Is there something wrong with the circuit shown in this guide? Is there something wrong with my simulation of the circuit?

(Note: I've also successfully simulated the circuit given in this answer but I prefer the jeelabs.org version simply because it uses fewer components. This question is to find out whether it will work for me.)


The issue with the circuit you have given us, is in order to turn on the MOSFET the capacitor needs to initially be discharged. That is, the right side of the cap needs to be at, or close to, the same voltage as the gate.

Now there really is only one way to make that happen, that is, turn on the output pin in the high state and wait long enough for the charge on the capacitor to equalize, then set the pin low and take your measurement.

After that, either set the pin high again and leave it as an output, or turn off the pin and repeat the whole process again next time.

Adding a discharge path would be better as shown below, but it does add the leakage current of R5 through the GPIO back in.

However: Please be aware, the GPIO needs to be able to handle the full battery voltage on it in these circuits. The PNP in the linked answer, which in my opinion is a much better solution, provides you with the isolation you probably need. Though I might have switched the BC547 NPN for a small signal MOSFET.


simulate this circuit – Schematic created using CircuitLab

| improve this answer | |
  • 1
    \$\begingroup\$ Thank you very much. I understand now how the capacitor provides a buffer of current which you can use to measure the voltage at the ADC over a short period of time. If I reduce the simulation speed it clearly shows the ADC voltage increases to 3.2 V as the GPIO moves from High to Low. However, like you say the GPIO is exposed to the full 4.2 V of the battery so this will not work for me (the High voltage of the GPIO pins on the ESP32 is 3.3v). \$\endgroup\$ – Alex Spurling Dec 5 '17 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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