1. There is an ADC Module inside ATmega328P Microcontroller. The brief version is shown in Fig-1, and the detailed version is given in Fig-4.

Figure-1: ADC Module inside ATmega328P MCU
2. The ADC converts uni-polar (only positive) analog DC signal (range: 0V to 5V or 0V to 1.1V) into 10-bit binary value. A bi-polar ADC converts both positive and negative signals.
3. We will learn the details of this ADC Module by building a Thermometer of Fig-2. To measure the room temperature, we will use LM35 temperature sensor (Fig-3).
4. The ADC has six analog channels named as: Ch-0 to Ch-5. These channels are connected with APin-A0 (Analog Pin-A0) to APin-5 of UNO.
5. When the input DC signal is 0V, the ADC produces the following 10-bit data for B9-B0 bits:
B9 B8 B7 B6 B5 B4 B3 B2 B1 B0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ==> 000000 0000000000 = 0x0000 = 0 decimal
6. When the input DC signal is 5V (for the range: 0V – 5V), the ADC produces the following 10-bit data for B9-B0 bits:
B9 B8 B7 B6 B5 B4 B3 B2 B1 B0
1 1 1 1 1 1 1 1 1 1 ==> 0 0 0 0 0 0 1111111111 ==> 0x03FF = 1023 decimal
7. When the input DC signal is 1.1V (for the range: 0V – 1.1V), the ADC produces the following 10-bit data for B9-B0 bits:
B9 B8 B7 B6 B5 B4 B3 B2 B1 B0
1 1 1 1 1 1 1 1 1 1 ==> 0 0 0 0 0 0 1111111111 ==> 0x03FF = 1023 decimal
8. What is Full Scale (FS) of an ADC?
It is the maximum DC voltage at the input of ADC for which all output bits are 1s.
(1) For the range: 0V – 5V, the FS is 5 V and output bit pattern is:
00000 1111111111 in binary = 0x03FF in hex = 1023 in decimal.
(2) For the range: 0V–1.1V, the FS is 1.1V and output bit pattern is:
00000 1111111111 in binary = 0x03FF in hex = 1023 in decimal.
(3) The FS value (5V or 1.1V) is always connected at the VREF point of the ADC (Fig-1).
9. Assume Full Scale: 5V
(1) If the input voltage is VDT, what will be the value generated by the ADC? Let us give the name ADCV (ADC value) for the amount generated by the ADC for an input signal.
When the input DC signal is 5V, the ADCV = 1023 in decimal
So, when the input DC signal is VDT, the ADCV = (1023/5)*VDT decimal
(2) Given: VDT = 3.5V; what will be bit pattern for ADCV?
ADCV = (1023/5)*3.5 = 67.55 ~= 68 in decimal = 0x44 = 000000 (000100 0100) in binary
10. Assume Full Scale: 1.1V
(1) If the input voltage is VDT, what will be the value generated by the ADC?
(2) For what input DC signal (VDT), the ADCV will assume the following bit pattern?
0101000111 in binary
11. After the conversion of the input DC signal into binary form, lower 8-bit (B7 – B0) of the result enters into ADCL Register (Fig-1), and upper 2-bit (B9 – B8) enters into ADCH Register (Fig-1). The higher 6-bit (B15 – B10) of the ADCH Register are always 0s (Fig-1).
12. We can execute one of the following code/instruction/function to select FS (Full Scale: 5V or 1.1V) for the ADC.
(1) analogReference(DEFAULT); //to select 5V
(2) analogReference(INTERNAL); //to select 1.1V
13. Let us put the ADC into operation by executing the following sub-steps:
(1) Select Full Scale by executing the code/function of Step-12.
(2) Select the analog channel Ch-3. The LM35 sensor is connected at Ch-3 (Fig-3).
(3) Give START command to the ADC to begin conversion (Fig-1).
(4) Wait until conversion is finished. The conversion time is 80 µs. (The ADC clock is 125 kHz (Fig-1). One clock pulse is required to produce one bit data. So, the conversion time for 10-bit output is: (1/125x103)*10 = 80 µs. However, if we include settling time of S/H circuit, the ADC requires about 13 clock cycle for conversion. Then the conversion time is about: (1/125x103)*13 = 104 µs.
(5) After conversion, ADC value automatically enters into ADCL and ADCH Registers.
14. Step-13(2) to 13(5) are carried out one after another when the following code is executed. As a result, the output of ADC (B9 – B0 = the contents of ADCL and ADCH Registers) automatically enters into variable x.
unsigned int x = analogRead(A3); //Ch-3 is connected with APin-A3; x = 16-bit
15. Now, we have the following two lines of codes to put ADC into operation in order to convert analog signal at Ch-3.
(1) analogReference(DEFAULT); //to select 5V AS Full Scale (FS)
(2) unsigned int x = analogRead(A3); //Ch-3 is AT APin-A3; x = 16-bit
16. Given: FS = 1.1V, input DC signal = VDT and Ch-3. Find relationship between VDT and analogRead(A3).
(1) When input DC signal is 1.1V, the ADC value is: 000000 1111111111 = 1023 binary.
(2) When input DC signal is VDT, ADC value is: (1023/1.1)*VDT.
(3) When ananlogRead(A3) code is executed, ADC value (000000 + B9-B0 = 16-bit enter into variable x. That is:
unsigned int x = analogRead(A3);//unsigned means always +ve.; int = 16-bit[/code]
(4) So, the ADC value of Step-16(2) must be equal to x. That is:
x = (1023/1.1)*VDT
==> (1023/1.1)*VDT = ananlogRead(A3)
==> V[sub]DT[/sub] = (1.1/1023)*analogRead(A3) (It is a floating point number
[b]17.[/b] Repeat Q-16 with FS = 5V.
[b]18.[/b] The Manufacturer of LM35 sensor says that:
[b](1)[/b] When the room temperature is 25[sup]0[/sup]C, the sensor gives an output DC signal of 0.25 V.
[b](2)[/b] When the room temperature is 75[sup]0[/sup]C, the sensor gives an output signal of 0.75 V.
[b](3)[/b] When the room temperature is T[sup]0[/sup]C, the sensor gives an output signal of V[sub]DT[/sub] V.
[b](4)[/b] In V[sub]DT[/sub], V stands for Voltage; D stands for DC voltage; T stands for temperature.
[b]19.[/b] The room_tempertaure vs sensor_signal data of Step-18 can be documented as follows:
A (25[sup]0[/sup]C, 0.25V) = A(T1, V[sub]DT1[/sub]),
B(75[sup]0[/sup]C, 0.75V) = B(T2, V[sub]DT2[/sub]), and
C (T[sup]0[/sup]C, V[sub]DT[/sub]).
[b]20.[/b] From the given three points of Step-19, express T in terms of V[sub]DT[/sub]. ([b]Hints:[/b] Get V[sub]DT[/sub] = mT + c and then get T = f(V[sub]DT[/sub])).
(T1 – T2)/(V[sub]DT1[/sub] – V[sub]DT2[/sub]) = (T1 – T)/(V[sub]DT1[/sub] – V[sub]DT[/sub])
==> (25 – 75)/(0.25 – 0.75) = (25 – T)/(0.25 - V[sub]DT[/sub])
==> T = 100*V[sub]DT[/sub] (degree Celsius)
[b]21.[/b] Assume that the output signal (V[sub]DT[/sub]) of LM35 sensor (Fig-3) is connected with Ch-3 of the ADC via Apin-A3 of UNO. Now, Express T in terms of “analogRead(A3)” by combining equations of Step-16(4) and Step-20.
[b]22.[/b] Assume that the LM35 sensor has produced a DC signal (VDT) of 0.96V; find the value of T (Room Temperature) by manual solving of the equation of Step-21.
[b]23.[/b] Assume that the Room Temperature (T) is 27.50C; find the value of the output DC signal (VDT) of LM35 sensor by manual solving of the equation of Step-21.
[b]24.[/b] Write syntactically and semantically correct C-code for the equation of T of Step-21 so that T shows temperature signal with 1-digit precision. (Precision refers number of digits after the decimal point.)
[b]25.[/b] Write syntactically and semantically correct C-code for the equation of T of Step-21 so that T shows only the integer part of the temperature.
[b]26.[/b] Pictorial view of the prototype Thermometer
[img width=500 height=266]https://forum.arduino.cc/index.php?action=dlattach;topic=723338.0;attach=399236[/img]
Figure-2: LM35 sensor, UNO and 7-segent Display Unit based Thermometer.
[b]27.[/b] Circuit Diagram for LM35 + UNO Based Thermometer
[img width=500 height=209]https://forum.arduino.cc/index.php?action=dlattach;topic=723338.0;attach=399238[/img]
Figure-3: Connection diagram among LM35, UNO and Display Unit
[b]28.[/b] Internal Details of the ADC Module of ATmega328P MCU
[img width=500 height=409]https://forum.arduino.cc/index.php?action=dlattach;topic=723338.0;attach=399545[/img]
Figure-4: Internal details of the ADC Module of ATmega328P MCU
... to be continued.







