Detecting LiPo voltage is not the best way to check charge level but it’s the easiest.
I was using a PR2040-Zero (works like a Pico), all analog pins were in use.
I wanted to indicate when the LiPo needed a charge so came up with this circuit using a LM393 op amp. The onboard LDO voltage regulator is used as the reference voltage. (3V3)
See discussion below, the original circuit had a few problems which have been fixed in this pic.
The main problem was the LM393 does not work with a 3V3 supply, it stops working at 3.522V. The output remains low even though the non inverting input is greater than the inverting input. The solution was to use VSYS as the supply even though it is changing as the battery discharges.
I wanted to indicate when the battery had reached about 3.5V. The values of the high and low trigger points were calculated using a web page provided by @Robert93820. Comparator Hysteresis Calculator
The VSYS resistor divider was calculated to bring it to the low voltage calculation.
The circuit I built triggers at 3.58V, which is above the point where the LM393 output remains low.
Note: The hysteresis points change slightly as VSYS changes but no enough to affect circuit operation.
This circuit has now worked through a number of charge and discharge cycles. The assistance by @Robert93820 was invaluable and help me better understand analog circuits and op amps. This little circuit will feature in future LiPo projects to indicate when the battery needs charging. It is better than wasting a ADC pin on just measuring the supply.
Cheers
Jim
EDIT: original has been edited due to problems discussed below.