I didn’t take a look at the code behind analogRead(), basically (but it’s not true for everything about MCU) it’s hard to write a code which works in every situation. For ADCs for example you can specify a resolution, a bitrate etc … And Arduino is for the most common usages. So sometimes it’s not perfect … But it works with some tweaks
There seems to be significant difference in the analog (vbat) read behavior of the 4631 after waking from deep sleep, depending on the power source and serial output!
When on bat and using “MAX_SAVE” in the deep sleep example, readings are ok. Only the very first reading after the first wakeup after a mcu reboot is way off! Subsequent wakeups, the analog read is fine! This can be handled by doing a few analog reads in the setup after boot before going to sleep.
When the mcu is on bat and usb, even when the bat is fully charged, the first couple reads after wakeup are more noisy.
What is really interesting, removing MAX_SAVE ( enabling serial output and LEDs) while debugging on usb, things get ridiculously noisey and addition of long delays after wake dont help, the analog reads are really unreliable even after delays.
Even if the battery is fully charged, the battery charger chip is still active when USB is plugged in. I guess that’s why you get the fluctuating values.