Make Arduino Measure Its Own Voltage From Vin

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.

Thank you very much.

See Help with Aref reading on Mini Pro 3.3V - Project Guidance - Arduino Forum and http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/

The output of a 5V power bank will be 5V regardless of battery level, until the battery is empty.

Thank you very much

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...

Just for your own reference here is a link to getting the values for a divider:

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.

Isn't it great that Atmel has thought of that and provides us with the fixed 1.1V reference?

Take care that is not always exactly 1.1V, you should measure it.

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.

It's stable - you just have to measure the actual voltage once (for each individual chip), and then you can use that number.