XIAO ESP32C3 Battery ussage

One way that I have used successfully on boards that don’t have any built-in circuitry to detect the USB is to simply put in a resistor divide by 2 circuit from the 5V pin and connect the centre of the resistor divider to an analog input and read the voltage. A bit of maths lets you work out if the 5V is present. (Note that it might not be exactly 5v depending on diodes, usb supply etc. But there ought to be a significant difference to the voltage sensed this way between when the USB is plugged in or if you are running on a single cell LiPo battery.
Note that you need the resistor divider because the 5V is more than the chip’s inputs can take. Divide by 2 is safe (i.e. 5/2 is less than the 3.7 or so that the pins can take) There is probably a diode from the USB V+ as well that drops the voltage actually seen at the 5V pin.
Good luck!

1 Like