hi
I am trying to make a project that has a standalone atmega328p chip and am wanting to run it from a single cell lipo battery (3.7 to 4.2 volts) I also want to be able to read the voltage of multiple cells wired in parallel to tell when they are getting flat. the problem is that I cannot read the voltage correctly because as the voltage of the battery decreases the adc still reads 1023 so can not get the correct voltage. how can I fix this problem.
Your description would benefit from a posting of the code. However, if you are powering the Atmega as you say you are, then I assume your sing a 3.3v version of the chip. Have you declared an analog reference voltage? Have you made sure that the battery voltage you're reading is in the range of that reference voltage? Be sure, if necessary, to use a voltage divider to get your measured voltage down to or below the level of the analog reference voltage chosen.
If you dig through Gumpy Mike's link what you'll eventually figure out is this:
The 328p has an internal 1.1V reference. So you can measure Vcc by setting up the A/D registers to use Vcc as the analog reference and the internal 1.1V reference as the source. Then you read it. As Vcc drops the A/D value increases. You need to calibrate this because the internal reference isn't exactly 1.1V and varies from chip to chip.
If you search the internet you can find code that sets up the A/D appropriately and does the read. Like this, for example.
To add to what @jboyton has said. The trick is to "measure" the 1.1v reference voltage using the battery voltage as a reference.
When your battery is (say) 4.2v then 1.1v will give an analogRead() value of 268, and when the battery voltage has fallen to 3.7v it will give a value of 304.