How to measure a supply voltage to arduino uno?

I have a project
that i should use 3.7 v battery and measure the batterie's voltage !
I just used the analogRead and analogreference but hasn't worked
If any one can help me I will be grateful

Use the band-gap reference and a voltage divider on the input.

Don't forget to credit me in your assignment write-up.

Or credit Scott at https://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
But the factor of 1023 is wrong. It should be 1024.

But the factor of 1023 is wrong. It should be 1024.

  1. Assume Vref to the ADC = 5V.

  2. The Full Scale (FS) of the ADC is 5V.

  3. FS ia 5V; it menas that the all 10 output bits will assume LH-states when 5V is applied to the
    input of the ADC.

  4. The decimal count of 11 1111 1111 = 1024-1 = 1023.

  5. The correct factor is 1023 and not 1024.

GolamMostafa:

  1. Assume Vref to the ADC = 5V.

  2. The Full Scale (FS) of the ADC is 5V.

  3. FS ia 5V; it menas that the all 10 output bits will assume LH-states when 5V is applied to the
    input of the ADC.

  4. The decimal count of 11 1111 1111 = 1024-1 = 1023.

  5. The correct factor is 1023 and not 1024.

The correct factor is 1024, your reasoning is incorrect.
Please refer to the data sheet.

  1. The correct factor is 1023 and not 1024.

This is a common misunderstanding and has been debated many times.

To help understand the error in your reasoning, pretend that the ADC has only two bits, with a reference of 1 V, and work out what voltages the individual ADC output values will represent, when using either 3 or 4 as the divisor.

Better still, go for the full reductio ad absurdum, and imagine a one bit ADC.

  1. My reasoning has been seen as 'incorrect (@AOWL)' and not 'wrong'. It has left enough space for my
    reasoning to be corrected/adjusted.

  2. My reasoning has also been said to contain 'error (@jremington)'. There is also, as I see, room for
    my reasoning to be compensated.

  3. We (including many others) differ by only one count.
    (a) http://www.microchip.com/forums/m589143.aspx
    (b) -------------

  4. Data sheets of ATmega328 says: ADC (output value) = (Vin/Vref) * 1024

The data sheet does not explain the rationality of the inclusion of the figure 1024, as it is not a
text/design book.

  1. We may try to explore the theory of SAR type ADC to see the root cause of the difference of
    1 count and then decide which one (1023 or 1024) is the reasonable figure.

Assume 10-Bit SAR type ADC; Vref = 5V; Vin = 5V

Bit Set (Internal DAC Input) Clocking Pulse Injected DAC Output EOC
B9 512 2.5V (Vref/2) inactive (0)
B8 256 +1.25V 0
B7 128 +0.625V 0
B6 64 +0.3125 0
B5 32 +0.15625V 0
B4 16 +0.07125V 0
B3 8 +0.0390625V 0
B2 4 +0.01953125V 0
B1 2 +0.009765625V 0
B0 1 +0.0048828125V active (1)

11 1111 1111 1023 4.9951171875V Conversion done

(a) The ADC logic has found active 'conversion done' status after pumping 1023 clocking pulses
though the voltage produced by the internal DAC is less than the actual Vin by 0.0048828125V.

(b) The ADC logic could make the DAC's output equal to the actual Vin by pumping another clocking
pulse; but, at the cost of making 0s for B9-B0. Thanks to the smart ADC that it does not do it.

  1. Should we accept 1023 at the accommodation of the error of only 0.0048828125V or 1024 at the
    cost of loosing all the ADC value? The analogRead(x) will always read lower 10-bit.

The correct value to use is the number of steps in the interval, 1024. That is simple mathematics, and has nothing to do with the data sheet.

Granted, the difference between 1023 and 1024 is not large. However, if you investigate the two-bit and one-bit ADC examples that we suggested in previous posts, you should then understand the point.

jremington:
The correct value to use is the number of steps in the interval, 1024. That is simple mathematics, and has nothing to do with the data sheet.

Granted, the difference between 1023 and 1024 is not large. However, if you investigate the two-bit and one-bit ADC examples that we suggested in previous posts, you should then understand the point.

  1. Most of the times, the ground-level fact remains encapsulated by our apparent perceptions. It is
    hard to understand through cognitive process unless clear (theoretical and practical) examples are
    demonstrated.

  2. We clearly see that the opinions are in divided between 1023 and 1024.

  3. If we taks FS as 4.9951171875V, the factor is certainly 1024; otherwise (for 5V FS), the factor is
    1023 with error of 1 LSB.

It is hard to understand through cognitive process unless clear (theoretical and practical) examples are demonstrated.

Indeed. You haven't done the thought experiments we suggested.

Good luck with your efforts to grapple with these very challenging concepts!

jremington:
Indeed. You haven't done the thought experiments we suggested.

Good luck with your efforts to grapple with these very challenging concepts!

jremington:
Indeed. You haven't done the thought experiments we suggested.

Good luck with your efforts to grapple with these very challenging concepts!

grapple

  1. Something that does not work in theory will never work in practice.

  2. Something that works in theory may not work in practice.

  3. In many cases, we fine tune theory based on the scientific (repetitions of the same
    task under identical setup produce the same outcomes) observation.

  4. To agree whether the factor will be 1023 or 1024, it is not enough to wish good luck; but, to
    providing support with proper tools.

  5. We can perform the following experiment using ArduinoUNO:
    (a) Set the Vref at: 1.1V (internal).
    (b) Connect AREF-pin to A0-pin.
    (c) Perform Serial.println(analogRead(A0));.

  6. The Monitor shows: 1023

  7. If I am asked to calculate an unknown voltage (0<=Vin <=1.1V)fed at Pin-A0, I will certainly use
    the factor (1023) of Step-5.

  1. The Monitor shows: 1023

...Which is what it would have showed had you connected a 1.2V source.
Your experiment proves nothing.
Go check the datasheet's ADC section.

AWOL:
...Which is what it would have showed had you connected a 1.2V source.
Your experiment proves nothing.
Go check the datasheet's ADC section.

Your experiment proves nothing.

So, Vin = k1023 is 'wrong'; Vin = k1024 is 'right'.

Ok, here comes the half-fat reductio ad absurdum (because I'm posting on my phone).
You have a one bit successive approximation ADC.
It has a reference voltage of 1 volt (to keep the arithmetic simple).
It can return only two readings - zero or one.
What does the reading of one represent?
One volt?
Wrong.
It represents half a volt or greater.

It represents half a volt

  1. SAR starts with setting up (HIGH) the MS-Bit with a value equal to Vref/2 (1/2 Volt).

  2. Because, it is 1-bit ADC; the MS-Bit is established; no other bit awaits to be defined;
    the conversion is done.

  3. The ADC value is 1 (1/2 Volt) with an error of 1/2 volt.

  4. In a closed loop regulation system, the available data to us are the Vref and the value of
    of the in situ ADC for the calculation of error signal.

  5. In a Forum like this where industrially experienced people are voluntarily engaged to
    answer to the queries even of the novices, the placement of the sophisticated
    methodology (reductio ad absurdum) to resolve the ADC issue is not much desired.

Some people say that the ADC conversion process of the ATmega328 invoves 1024 steps
(0 t0 1023); I see it as a 10-Step process, and this brings the difference of one count. It is
a matter of misunderstand/understanding; it cannot be resolved by reductio ad absurdum
methodology.

I'm assuming you quoted my earlier post twice to emphasise its correctness.
Thank you.

The ADC value is 1 (1/2 Volt) with an error of 1/2 volt.

...And with a ten bit converter, a reading of 1023 represents . . . what?
We got the half a volt or greater, by dividing one volt by two, aka 21 (one being the number of bits the convertor has)
(Have you had the light-bulb moment yet?)

...And with a ten bit converter, a reading of 1023 represents . . . what?

  1. What should we consider - it is what we have in our minds or what we observe?

  2. The conversion is finished. The ADC has produced a digital value which is equal to the Vin with error
    (+/- 1/2 of LSB).

  3. You tell me what to answer - 1023 or 1024?

Well, for a one bit converter, we divide by 21, so for a ten bit converter, we divide by (Ta-da! ) 210
(and we still have an error, albeit much smaller)

Ten bits represent 1024 values, from 0 to 1023. When you divide you MUST use the number of values and not the maximum value.
So 0 represent a voltage from 0 to the minimum that the ADC get as 1
1023 represent a value from little less 5V (about the same difference between 0 and 1) and 5V
The total number of values the ADC returns is 1024.

Using only one bit, like AWOL and jremington wrote, you have 2 values and the maximum is 1 but when you divide you must use 2 since the possible values are two.