Not correct reading the volts in Serial monitor

I'm either getting old or losing my marbles as I've done this before, I'm trying to create a simple voltmeter does not need to be supper accurate.
The trouble I'm seem to be having is battery reads 3.991 with my fluke meter(i don't expect to get this accurate with 10bits of arduino) but the readings that are been calculated are showing 6.04. My pro mini is running at 3.3V.

So I have a resistor divider set up With R2 to ground is 3.9K and R1 is 10K, I have been playing around with these values but for now these are the values I'm using.

int analogInput = A0;
float vout;
float vin;
float R1 = 10000.0;
float R2 = 3900.0;
int value;

void setup() {
  Serial.begin(9600);
  pinMode(analogInput, INPUT);
}
void loop() {
  value = analogRead(analogInput);
  vout = (value * 5) / 1023.0;
  vin = vout / (R2 / (R1 + R2));
  Serial.print(value );
  Serial.println(" RAW ADC");
  Serial.print(vin);
  Serial.println(" VOLTS");
  delay(300);

}

On looking at various ways how other people do and there all roughly the same, Not sure where its gone wrong

Is the reference voltage 3.3V perhaps instead of 5V?

This happens to give you something close to what you want: (6.04 / 5) * 3.3 = 3.9863

1. What is the bias/excitation voltage for R1-R2 network -- 4V from Battery?
2. Include one of the following two codes in the setup() function of your sketch to decide the full scale of the ADC; also, answer to the questions of the comment field. (I don't know as I have no pro mini model.)

analogReference(INTERNAL);   //how much is connected -- 1.1V or 3.3V or 5V?
analogReference(DEFAULT);    //how much is connected -- 3.3V or 5V?

3. To carry out paper-pencil calculation, the above information is needed.

It would also be nice to know, which device is used. If it is a standard Arduino, you may get the actual reference/supply voltage with the "secret voltmeter" like this:

long read_vcc()
{
  
  //Make sure that ADC is enabled
  ADCSRA |= ADC_ENABLE;
  
  // Read 1.1V reference against AVcc
  #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
    ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
     ADMUX = _BV(MUX5) | _BV(MUX0) ;
  #else
    ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #endif  
 
  delay(5); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Start conversion
  while (bit_is_set(ADCSRA, ADSC)); // measuring
 
  return 1125300L / ADCW;
}

That would make the measurements more accurate than to use a constant like 3.3V or 5V.

Hi,

That is the direct connection to the battery.
What do you get at the junction of the potential divider that feeds A0?
Does the measured value, at A0, agree with the potential divider equation?
What raw ADC value do you get?
What calculated value do you get?

I know it s a little item but;

 vout = (value * 5) / 1023.0;

might need to be;

 vout = (value * 5.0) / 1023.0;

Tom... :smiley: :+1: :coffee: :australia:

Sorry, yeah my pro mini is running on 3.3V,
Thanks guys I’ll try your suggestions never thought about the reference is 5v and it has just dawned on me that I’m multiplying by 5 and nit 3.3v.
My pro mini is running off 4.2v lithium battery stepped down to 3.3v and have A0 been fed from the resistor divider

If you want to monitor your Battery voltage, then you have to follow these steps: (Assume Vref = 5V)
1. With 4.2V Battery connected, compute input to ADC; Vin = 1.17 V
2. Vout (output voltage proportional to Battery)
= (5/1023)xanalogRead(A0)x(4.2/1.17)

codes:

float Vout = (5.0/1023)*analogRead(A0)*(4.2/1.17);
Serial.println(Vout, 1);