measure voltage

Hello,

i`m new in the arduino world and i need some help. (My board is the Arduino Mega2560)
I want to measure the voltage on a accu (1,2 V)
I connect the negative pole of the accu to GND and the positive to A2.

I use the following code:

int Ubit = 0;
float U = 0;

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

void loop()
{
Ubit = analogRead(A2); // Pin12 read
U = (Ubit/1023)*5; // A/D-value (bit) in Volts
Serial.print(U); // show U in Volts
Serial.println(" V");
Serial.println(Ubit);
delay(1000);
}

and this is what i get:
0.00 V
267
0.00 V
330
...

Why do I get 0.00 V?
It doesn`t matter if I connect the accu or not.

thanks for your help
sweetsuit

U = (Ubit/1023.0)*5.0;

Please use code tags when posting code.

First post your code right. Read how to use this forum sticky.

Next you are doing int maths and then assigning it to float you need to do float maths all the way through.

AWOL:

U = (Ubit/1023.0)*5.0;

Actually the 1023.0 should be 1024.0, although as the 5V reference supply is not very accurate, it doesn't make much difference. I would use this:

U = (float)Ubit * (5.0/1024.0);

because it makes it clear where the integer to floating point conversion should happen, and the slow floating point division only involves constants - so the compiler will do it at compile time instead of it being done at run time.

Actually the 1023.0 should be 1024.0

It's a fine distinction, but I'm really not sure.
1024 implies eleven bits of resolution, but it's a ten bit converter.

AWOL:

Actually the 1023.0 should be 1024.0

It's a fine distinction, but I'm really not sure.
1024 implies eleven bits of resolution, but it's a ten bit converter.

From the atmega328p datasheet:

24.7 ADC Conversion Result
After the conversion is complete (ADIF is high), the conversion result can be found in the ADC
Result Registers (ADCL, ADCH).
For single ended conversion, the result is

ADC = VIN ? 1024/VREF

where VIN is the voltage on the selected input pin and VREF the selected voltage reference (see
Table 24-3 on page 264 and Table 24-4 on page 265). 0x000 represents analog ground, and
0x3FF represents the selected reference voltage minus one LSB.

AWOL:

Actually the 1023.0 should be 1024.0

It's a fine distinction, but I'm really not sure.
1024 implies eleven bits of resolution, but it's a ten bit converter.

While the maximum number you will get is 1023 there are actually 1024 divisions of voltage.
It is like lamp posts and gaps, a question on the old 11+ exam.

It's an interesting conundrum, isn't it?

I'd never really thought about it before, but taking the common-sense reductio ad absurdum approach, and imagining a 1 bit ADC, measuring, say, a 1V input, the voltage resolution of the device is 1V (there is only one (21-1) interval between the minimum and maximum readings), but if you follow the "divide by 2n" rule, the two readings the device can give are zero and 0.5volts.

It seems wrong that the device can't distinguish zero and 1 volt, but strictly speaking, all it can say is "less than 0.5V" or "0.5V and above" (or "0.5V and below" or "greater than 0.5V).

Thanks for making me sit down and think about it!

but strictly speaking, all it can say is "less than 0.5V" or "0.5V and above" (or "0.5V and below" or "greater than 0.5V).

Yes and that is all that it is saying no matter how many bits of resolution you have. The more bits and the smaller is this gap between the above this but below that voltage it reports.

Thanks guys,

now it works.

But I have a second question.

What is the input-impedance of an analog input and what current is allowed there?
I want to disscharge the accu with a resistor and meassure the voltage over time.
Where can I find some electrical specifications of the Inputs and Outputs?
Are the values the same as Atmel Atmega Datasheet?

Thanks for the quick help
you`re great

The input impedance is very high, it is in the processor's data sheet if you want an exact value.
However it is recommended that you drive it with an output impedance of 10K to ensure the sample and hold capacitor reacts quickly.
So you could try directly connecting your experiment, it might be good enough. If not then use an op amp wired up as a voltage follower I front of the input.