Hi,
I've built my very first PCB with atmega328p-au. Everything works fine (MCU, sensor, radio) except battery measurement. It always give me 0.4V regardless of what battery is connected (1,5V 1xAA or 3V 2xAA).
const float VOLTAGE_DIVIDER_R1 = 1e6;
const float VOLTAGE_DIVIDER_R2 = 470e3;
uint8_t BATTERY_SENSE_PIN = A1;
void setup() {
// use the 1.1 V internal reference
#if defined(__AVR_ATmega2560__)
analogReference(INTERNAL1V1);
#else
analogReference(INTERNAL);
#endif
}
...
// gives me 127
int sensorValue = analogRead(BATTERY_SENSE_PIN);
// 3.440426e+00
const float voltageDivider = ((VOLTAGE_DIVIDER_R1+VOLTAGE_DIVIDER_R2)/VOLTAGE_DIVIDER_R2)*1.1;
// 3.363075e-03
const float voltsPerBit = voltageDivider/1023;
// 0.43 V
float batteryV = sensorValue * voltsPerBit; // calculated
R1 = 470K
R2 = 1M
C3 = 100nF
C3 is connected to A1
Any idea what could be wrong?