Hello, I am doing an Arduino project using Arduino Mega and a 5V cellphone powerbank. I need to make the Arduino measure its Vin voltage in order to print the powerbank's battery percentage. How can I do this? How should the circuit and code be? I have read about voltage dividers but I don't know what they are or how to build them. Please help me.
powerbanks internally have a 3.7V lithium battery and 5V boost converter. The state of charge is
indicated by a row of LEDs. If you want more sophisticated you may have to roll your own...
You would actually need a voltage reference that is independent of the power supply you are trying to measure. You can't measure a power supply when it's also the reference. Hence you would need a voltage reference and a voltage divider with the power supply voltage at the top. the divider output would then go into the ADC and the ADC should be configured to use the external voltage reference. In this way, you can measure your battery voltage.
Actually that's true. I forgot about the bandgap reference internally in the AVR chip. This can also be used. As mentioned, it's not as accurate as a precision reference, but if you're just measuring battery voltage, it can probably give you the voltage with around tens of mV precision.