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.