How to measure the battery

A project log for Homefixer ESP8266 devboard

A lightweight and simple devboard for ESP8266-12 with 2.54mm breadboard pinout and works with 1V-5.5V batteries

Johan WestlundJohan Westlund 01/01/2018 at 22:420 Comments
Because the ADC in ESP -12F is only 0-1V I had to make a voltage divider, but voltage dividers drains power constantly. A quickfix could be to chose a high value resistor, but then again the noise could increase or the ADC might be affected by the high impedance. So I wanted something better. With some google magic I found that others had used a transistor to turn on and off the voltage divider.

The reason that the transistor is on the high side of the divider is because otherwise the ADC input will be exposed to the battery voltage when the transistor is "open"(high impedance).

Using a high side transistor(PMOS) introduce another problem. To be able to put it in "open" state, the gate(1) - source(2) voltage needs to be <=0V. A 3.3V µcontroller can't put out say 4.2V like in a fully charged LiPo battery. One way could be to but transistor in between to step up the voltage, but I found one way that works really good in this case and it is cheaper by using a capacitor instead as the step-up stage.

It works in the way that the capacitor is conducting the AC part of the signal on ADC_ON. So falling and rising edge on a pulse will "jump over" the capacitor and "open/close" the transistor. According to some forum post a falling edge will "close" the transistor for 2 ms, enough time to make one sample of the voltage on the ADC pin.

Here is some example code I have used with success:

  digitalWrite(15, LOW); //Close transistor
  int sensorValue = analogRead(A0); //Measure
  digitalWrite(15, HIGH); //Open transistor
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (6.85 / 1023.0);

ADC_ON is connected to pin 15 on the ESP.

I tried with delay(1) between LOW and READ but that was to long. This code have worked without problems.

The 6.85 value is the value that corresponds to the battery value when the ESP measures 1V, but this was tested on one of my units and your miles my vary.

Tip: connect to a good power supply and check what this code read and the calibrate the 6.85 value to something that works good for you.