question

I have one doubt in temperature sensor program in Arduino uno.
why are we using
/ Display Temperature in C
int tempReading = analogRead(tempPin);
float tempVolts = tempReading * 5.0 / 1024.0;
float tempC = (tempVolts - 0.5) * 100.0;
this in the program? what does the second line mean?

what does the second line mean?

This one?

  int tempReading = analogRead(tempPin);

It is reading the analog pin that the temperature sensor is connected to.

If you meant this one:

  float tempVolts = tempReading * 5.0 / 1024.0;

it is converting the value, in the range 0 to 1023, to a value in the range 0 to 5.0, which is the range of voltages that can be read on an analog pin.

 float tempVolts = tempReading * 5.0 / 1024.0;

The value returned by analogRead() will be between 0 and 1023 for a range of voltages 0 to 5V. This line of code converts the 0 to 1023 value to a voltage. Note the use of decimal places in the constants to ensure that the calculation uses floats instead of integers.

PaulS:
it is converting the value, in the range 0 to 1023, to a value in the range 0 to 5.0, which is the range of voltages that can be read on an analog pin.

Well, 0 to 4.995.

If it returns 0 to 1023, and you divide by 1024, you will never get a quotient of 1.

can we also take other value instead 1024?

You can, but them the math doesn't make sens.... ::slight_smile:

  int tempReading = analogRead(tempPin);

It is reading the analog pin that the temperature sensor is connected to.

If you meant this one:

  float tempVolts = tempReading * 5.0 / 1024.0;

it is converting the value, in the range 0 to 1023, to a value in the range 0 to 5.0, which is the range of voltages that can be read on an analog pin.
[/quote]

And in the third line why do we have to minus 0.5 with tempvolts?

And in the third line why do we have to minus 0.5 with tempvolts?

The data sheet for your temperature sensor will explain that.

PaulS:
The data sheet for your temperature sensor will explain that.

Okay, Thank you for your help. I will check data sheet in google.

I would not go all the way to Google and knock on there door to read a datasheet there... I would use Google to find it and read it in Adobe read in my shop... :smiley: ::slight_smile:

saikrishna:
can we also take other value instead 1024?

I have a lengthy discussion about 1023 vs 1024 here.

My suggested formula is:

float voltage = ((float) rawADC  + 0.5 ) / 1024.0 * Vref;

(Vref being 5 in this particular case).

Thank you

I found this in the web:

DCs can vary greatly between microcontroller. The ADC on the Arduino is a 10-bit ADC meaning it has the ability to detect 1,024 (210) discrete analog levels. Some microcontrollers have 8-bit ADCs (28 = 256 discrete levels) and some have 16-bit ADCs (216 = 65,535 discrete levels).

So by reading all these informations which I have been known, I understood sensor value can also be divided with 1023 as like 1024. 1024 values mean 0 to 1023. so even if we divide 10235/1023 =1. and 10235/1024 =4.9951171. Is this right? Can you please let me know if I did any mistake.

For any practical purpose above about eight bits, you can use 2n-1 or 2n but 2n is correct.

1024 values mean 0 to 1023. so even if we divide 1023*5/1023 =1

Yes, but there are 1024 values (0 to 1023) so dividing by 1023 is skewing the results. And, the ADC doesn’t give you an exact value, each value represents a “slot” (a range). For example, a reading of 0 doesn’t mean 0 volts, it means somewhere between 0 and 5/1024 which is 0.00488 volts.