Battery level readout


#1

Thought people might be interested in this. I’ve recently gotten more into my Cardputer at length and have found that I’d like to know how the battery is doing. Per the back of the device, GPIO 10 is connected to a voltage divider which will scale the battery voltage for the 12-bit ADC. Putting that math together (against a max ADC voltage of 3300):

(defun bat nil 
  (princ
    (concatenate
      'string
      (string
        (*
          100
          (/ (analogread 10) 3300)))
      "%"))
  nothing)

Output:

> (bat)
98.2%

Relevant part of the schematic:


#2

Awesome! I saw that pin 10 was something to do the battery but I couldn’t figure out what to do with the value


#3

I think just as @hasn0life has pointed out in my post on doing this with the T-Deck, there is still something missing here, probably related to ADC calibration in order for this to work out right. Despite leaving things charging for a very long time, I don’t see values above 77 or 78% for the battery, which makes me think that my possible maximum is off. Will try to get to the bottom of this.


#4

You can measure your battery voltage with a multimeter and compare it to what your ESP thinks it is to see if it’s off or not.


#5

The maximum battery voltage of a Lipo is 4.2V. The voltage divider has two 100kΩ resistors, so it divides VBAT by 2 to bring it into range of the 3.3V supply. To get the battery level as a percentage I think you therefore need:

percentage = (r x 3.3 x 2 x 100) / (1024 x 4.2)

where r is the reading. A good approximation to this factor is 2/13, so you could use:

(format t "~a%" (/ (* (analogread 10) 2) 13))