Arduino voltage divider

i want to let the arduino nano sense battery voltage on analog "A0", the maximum voltage could be 12.6V and Arduino can handle no more than 5V, so the simple solution is a voltage divider about "1/3" to handle up to 15V.
i tried 100Kohm to 300Kohm and worked well, but when I tried to read it on my pc, i just divided the number shown on serial monitor with "68.26" when the voltage get 15V to get reading 15V at 1024.
the problem that at 10 volt "for example" the Measurements get far from real multimeter Measurements.
why this happened, and how to write the right sketch for arduino to make a real voltmeter?

Because you did not show your wiring, a wiring diagram, or your code, I don't know what is wrong BUT...

... if you are using a 100K resistor and a 300K resistor in a voltage divider, you are getting 1/4 and NOT 1/3. Thus a reading of 1023 represents 20 volts. I would expect 5 volts to be about 255 counts. It would appear that you should be dividing the reading by about 51.2, not 68.26 .

The datasheet says that the ATMega328p ADC is optimized for an input impedance of 10 Kohms. I am unclear on how a voltage divider fits into this but it would appear that your divider has an input impedance at least 10 times this. Using 10K and 30K might be better but will require more current from the source.

You should not divide to 5volt, especially with a Nano.
Because the 5volt pin could be ~4.6volt on USB supply.
That alone could be an error of about 8% in your final readout.

The solution is to divide down to about 1volt, and use the Nano's build-in 1.1volt Aref.
Example for a 12volt lead/acid battery attached.
Leo..

/*
  0 - ~17volt voltmeter
  works with 3.3volt and 5volt Arduinos
  uses the stable internal ~1.1volt reference
  10k resistor from A0 to ground, and 150k resistor from A0 to +supply
  (1k8:27k or 2k2:33k are also valid 1:15 ratios)
  100n capacitor from A0 to ground for stable readings
*/
float voltage;

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL); // use (INTERNAL1V1) for a Mega
}

void loop() {
  voltage = analogRead(A0) * 0.01660 ; // calibrate by changing the last digit(s) of 0.0166
  // print
  Serial.print("The supply is ");
  Serial.print(voltage);
  Serial.println(" volt");
  delay(1000); // remove when combine with other code
}

@obaida963

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.

Thank you for your replays, i think that the best solution is to use an internal voltage reference, because I don't want to make my resistors 10k and 30k so a current flow about 0.3 mA at 12.6V so that the battery will die in 1 month while doing nothing.I'll try 100k and 300k but with internal voltage reference.

obaida963:
Thank you for your replays, i think that the best solution is to use an internal voltage reference, because I don't want to make my resistors 10k and 30k so a current flow about 0.3 mA at 12.6V so that the battery will die in 1 month while doing nothing.I'll try 100k and 300k but with internal voltage reference.

Most of that makes no sense.

The Nano has only one internal voltage reference, which is about 1.1volt.
With a 100k:300k divider, and 1.1volt Aref enabled, you can only measure to about 4volt.
I though you wanted to measure ~12.6volt.

Is this for a lead/acid (car) battery?
0.3mA over 1month is only 2.2Ah.

Read the comments in the sketch in post#2
10k +150k only draws 75uA from a 12volt battery.
Leo..

i made a costume pcb for a power bank that arduino turns on/off "XL1507" with it's en pin.
i want from arduino to measure the battery voltage"2P3S" LI-ION battery pack, and shows on OLED the battery voltage.
but the main problem is that to measure the battery voltage i need a voltage divider about 1/2" so that Arduino can handle up to 15V.
also I don't want to drain the battery while doing nothing if my voltage divider will be like "1Kohm to 2Kohm" , so i tried 300k to 600k and worked well.
in my code i just set my battery to 12.6V "fully charged" and find the right number to divide with, and when my battery voltage reached a 8.7V "fully discharge" the Arduino shows incorrect value and way off the real measure with multimeter.
so the problem with my voltage divider or with my sketch?
if necessary i can share my easyEDA schematic and my code.
my schematic not finished yet.

Thanks to WAWA suggestion, i fixed my problem. my Arduino was running on 4.678V and not 5V so after modifying my sketch everything worked well with a voltage divider 2Mohm to 1Mohm and a 100nf Capacitor.
Arduino measurements was close to multimeter measurements with +- 10mV.
Thank you for all replays.