engineer1984:
I tried searching around, but (as is with most programming questions I have) I can't seem to find what I need. Probably because I don't know exactly how to ask the question.
Let us start this way:
1. The LM35 Sensor produces --
250 mV (output signal) when the room temperature is 250C. //as per data sheet
350 mV when the room temperature is 350C. //as per data sheet
The response of the sensor is linear with some gain but there is no offset.
Let us designate the output signal as VDT when the room temperature is T0C.
The relation between T and VDT is --
(0.350 - 0.250)/(35 - 25) = (VDT - 0.350)/(T - 35)
==> T = 100*VDT ----------------------------------------(1)
2. VDT is an analog signal. The range of this signal is: 0 mV (at 00C) to 2000 mV (at 2000C). This signal will be digitized by the internal 10-bit ADC of the ATega328P MCU of the Arduino UNO Board. For better resollution (less mV for LSBit of ADC), let us use internal 1.1V of the MCU as the VREF of the ADC. Under this circumstance, let us find the relationship between VDT and the 'digital value of ADC (let us call it ADC)'.
when input analog voltage is 1.1V, all 10-bit of ADC will assume 1s; ADC value is: 1111111111 = 1023
when input analog voltage is VDT, ADC value is: (1023/1.1)*VDT. That is --
ADC = (1023/1.1)*VDT
==> VDT = (1.1/1023)*ADC ---------------------(2)
If the output signal of the LM35 sensor is connected with A0 (analog channel - 0) pin of UNO, the value of ADC parameter can be found by executing this instruction: analogRead(A0);. Now, Eqn(2) becomes as:
==> VDT = (1.1/1023)*analogRead(A0);-----------(2a)
Putting the value of VDT into Eqn(1), we can ahve --
T = 100*(1.1/1023)*analogRead(A0); ---------------------------------(3)
3. Let us carry out some numerical calculations on paper to see how the Eqn(3) introduces decimal points in the measured temperature value.
(1) Let us see how much minimum change in the room temperature that the ADC can detect.
The minimum analog input voltage that the ADC can detect (called resolution of ADC) =
Full Scale/2number_of_ADC_bit-1 = VREF/210-1 = 1.1/1023 = 0.001075 V. Let us see, how much change in the room temperature will produce this (0.001075 V) voltage.
For 0.200 V output, the room temperature is: 200C
For 0.001075 V output, the room temperature would be : 0.10750C.
(2) When room temperature is 20.10750C, the voltage at A0-pin is 0.201075 V. The value of T in Eqn(3) is:
T = 100*(1.1/1023)* 186.9997 //ADC = analogRead(A0) = VDT*1023/1.1 from Eqn(2a)
==> T = 20.107494 ----------------(4)
(3) If we want to keep only the integer part of the temperature value of T of Eqn(4), we tell the compiler to discard the fractional part and keep only the integer part (20 = 0x14 = 00010100b); it is done with the following style known as int casting: (the compiler stores 00010100 into the variable T.)
int T;
T = (int)100*(1.1/1023)*ananlogRead(A0);
Serial.println(T); // shows: 20
(4) If we want to keep both the integer part and the fractional part (called decimal value or float point number/value) of the temperature value of T of Eqn(4), we tell the compiler to keep both parts; it is done with the following style known as float casting:
float T;
T = (float)100*(1.1/1023)*ananlogRead(A0); --------------------------(5)
Serial.println(T, 6); // shows: 20.107494
What does the float casting do? It does the following:
Now, the compiler does not save 00010100 into the variable T; it now allocates four consecutive memory locations for the variable T and stores this 32-bit value (known as binary32 formatted value) : 41A0DC26 into these four memory locations as per binary32 'floating point number' convention of IEEE-754 Standard. I can show you how this value could be found, but I am unable to explain how the compiler does know Eqn(5) has 2-digit in the integer part and 6/more digits in the fractional part (someone else may explain!).
The Serial.println(T, 6); command takes 32-bit value from the four memory locations; it follows binary32 rules to extract the decimal number and finally shows the temperature value in Serial Monitor with 6-digit accuracy.
float-6.ino (274 Bytes)