T-Deck battery readout


#1

I’m working on getting a battery percent and/or voltage readout for my T-Deck in uLisp. For now, I’m getting values that are too high to be accurate, so I’m going to document my findings and process as I work on things going forward, and anyone is welcome to chime in with advice or ideas.

Hardware

The BAT_ADC is on GPIO 4 per the schematic as well as the pinout.

A TP4065 IC has a blue LED hooked to the ~CHRG pin, which means the light is on when charging and off when done charging or not attached.

GPIO 4 has a voltage divider setup (2x 100K resistors) to halve the battery voltage for the ADC into the 0-2.1V range.

The ESP32-S3 ADC is 12-bit (values of 0 to 4095).

uLisp

I defined two functions, one for fractional readout and one for voltage, like so. I also configure the ADC to be 12-bit from the uLisp all-device default of 10-bit (0 to 1023).

(analogreadresolution 12)

(defun batp nil (/ (analogread 4) 4095))

(defun batv nil (* (/ (* (analogread 4) 2) 4095) 3.3))

The first should give a float value 0 to 1.0 and the second a voltage amount. Note that I’ve doubled things to account for the voltage divider.

Doing that with a full battery (I tried several) I get values around 0.69 and 4.55 pretty consistently, which obviously are high as well as unchanging.

Next up: figuring out why! I’m guessing that the ESP32-S3 in particular might require some more nuanced ADC setup to get accurate readings and/or multiple samples or waits for accuracy.


#2

Digging a bit more, this looks like it might be an ADC attenuation issue. Docs here.


#3

More research is needed, but it looks like adding this line in initBoard() seems to solve the scale being off:

analogSetAttenuation(ADC_ATTENDB_MAX);

I can’t get at the other constants for specific attenuation amounts yet, but the max is what I need anyway (since it goes up to 3.1V), so that works. I then properly get a scaled ADC reading.


#4

FYI the ESP32 ADC is notoriously non-linear (tho I don’t know about the S3 specifically), so expect weird results. There’s some things you can do to help but its fundamentally flawed in its accuracy as far as I can tell. I’m not sure if that’s what you’re running into, but its something to watch out for.