Batteries have a maximum and minimum voltage. The processor reads the voltage and you can use a formula to calculate the percentage left till minimum voltage.
Or in more advanced cases you count the actual energy units going in and out of the battery, these are known as coulomb counters.
[Here is a good chat on the subject.](https://forum.arduino.cc/index.php?topic=476131.0)
Latest Answers