Pybricks can report the Lego spike battery voltage with hub.battery.voltage()
.
A similar solution is possible for the LMS-ESP32 by using the 18650 Shield V3.
The battery voltage is connected thru a 1MOhm voltage divider to an ADC capable pin of the LMS-ESP32. The support capacitor stabilizes the voltage while charging the ESP32's internal sampling capacitor.
I cut some traces, scraped off the solder mask and soldered SMD resistors to the bottom of the shield and repurposed one of the three 5V pin headers to provide this signal.
[!WARNING] Don't reverse the battery!
The one-time fuse F1 at the lower left corner of the image was obviously blown with a nice crater. It worked as designed and prevented further damage to other devices. Replacing this 8205A dual channel MOSFET circuit fixes the problem.
from machine import ADC, Pin
adcBat = ADC(Pin(13), atten=ADC.ATTN_11DB) # 11DB = 0-2.45V
def vBat() -> int:
'''returns battery voltage in millivolt'''
return adcBat.read_uv()//500