Problems with analog input reading with MKR1000 and a 9 V Battery

Hi to everyone !

I’m writing this topic to have an help about my project with an Arduino MKR 1000 powered by a 9 Volt Battery.

I need to read the value of the battery voltage to calculate the percentage of the charge.

I will attach the scheme.

I use a voltage divider to get 1/3 voltage of the battery on A0 and then i convert the value obtained with analogRead(A0) to a float and calculate the full voltage.

Here s the code :

#define PIN_BATTERY A0

float batteryVolt;          

void setup()
{
  Serial.begin(9600);    
}

void loop()
{
  batteryVolt = analogRead(PIN_BATTERY);
  
  Serial.println("Vout");

  batteryVolt = (batteryVolt * (3.3 / 1023.0));
  
  Serial.println(batteryVolt);
  
  Serial.println("Vin");
  
  batteryVolt = (batteryVolt*3.0);
  
  Serial.println(batteryVolt);  
         
  delay(1000);
}

The problem is that the value read from the Arduino is a lot higher than the value read by a tester, theese are the values :

Arduino Values

Vin = 7.97 battery V
Vout = 2.66 1/3 battery V

Tester Values

Vin = 7.80 battery V
Vout = 2.59 1/3 battery V

If i read the A0 without a pin it float around 1.31 V.

I dont know if this is normal and how can i fix it.

Also using more bits to read the analog value the value is remain the same.

It s not a lot of difference but for me is important.

You are within a very few %'s

What about the components you are using; Multimeter , resistors, the reference voltage, internal ADC .. are they all perfect?

Accept your numbers.. calibrate if the numbers are far off.