Measuring battery voltage

I'm designing a device that uses an ATmega328 @ 8MHz with Arduino bootloader that I would power from a 1 cell lithium ion battery (4.2V fully charged). The device contains 2 potentiometers, 3 buttons and a small oled display that is controlled through I2C (pins A4-A5). I'm not using a voltage regulator but powering it direct from the battery to keep things simple.

I would like to monitor the battery voltage through an analog input. So I can display a low battery warning on the oled display. But since I'm not using a voltage regulator I don't have a reference voltage. I thought about using the internal voltage reference (1.1V). But how will this effect the I2C pins and the 2 analog inputs I'm using for the potentiometer?

Maybe I should use a low dropout 2.8V regulator?

No, the ATmega328P can set its internal mux in such a way that the VCC is measured with the internal 1.1V as reference. It can measure its own 5V :slight_smile:

That code can be better.

// this was done in the past
//   result = ADCL;
//   result |= ADCH<<8;
// this is how is should be done
result = ADCW;

If you use the analog inputs for something else, you have to set the mux normal, do a dummy analogRead and wait 20ms.

That's very interesting, thanks for the link. Looks like the perfect solution for my problem.