Several consumer devices now involve temperature monitoring. For example, most air conditioners on the market feature climate control, where the AC continuously monitors the ambient temperature of a room and regulates the air conditioning accordingly. Even some digital watches now have built-in temperature monitors. Many other consumer appliances also offer temperature monitoring.
In this project, we built a temperature sensor using Arduino and the LM35 temperature sensor. The LM35 can be used to detect ambient and surface temperatures.
The purpose of the sensor built here is to monitor the ambient temperature of a room during a 24-hour cycle and display the current temperature, the maximum temperature recorded, and the minimum temperature recorded in that 24-hour cycle on an LCD panel.
Required components :
1. Arduino UNO x1
2. One 16×2 character x1 LCD
3. A 10K pot x1
4. A 330 Ohm resistor or any low value resistor x1
5. One LM35 sensor x1
6. One breadboard x1
7. Male-to-male jumper or connecting wires
Circuit Diagram :
Circuit Connections
The temperature monitor built in this project uses an LM35 temperature sensor. The 16x2 character LCD displays the current, maximum and minimum temperatures recorded on a 24-hour cycle.
The circuit is designed by connecting LM35 and character LCD with Arduino UNO.
The LM35 series includes sensors such as LM35, LM35A, LM35CA, LM35C and LM35D. The sensor used is the LM35 from the TO-92 LP package. In this packaging, the sensor comes as a three-terminal device. From the front view, the leftmost terminal provides the power supply, the middle terminal provides the analog output, and the rightmost terminal is the sensor ground.
In the circuit, the sensor receives a single power supply from the Arduino. The analog output of the sensor is connected to the analog input pin A0 of the Arduino UNO. In this circuit configuration, the LM35 acts as the basic centigrade temperature sensor. It outputs analog voltage directly proportional to temperature in a positive range (2˚ to 150˚C).
To display the current, minimum and maximum recorded temperatures, a 16×2 character LCD JHD162A is interfaced with the Arduino UNO. The 16-pin LCD module is interfaced in 4-bit mode.
- Pins 1 (GND) and 16 (LED) of the LCD module are connected to ground.
- Pin 2 (VCC) is connected to VCC.
- Pin 15 (LED+) of the LCD module is connected to VCC through a small value resistor.
- Pin 3 (VEE) is connected to the variable terminal of a potentiometer, while the potentiometer's fixed terminals are connected to ground and VCC.
- The R/W pin is connected to ground as the Arduino will only write data to the LCD module.
- RS, EN, DB4, DB5, DB6 and DB7 pins of LCD are connected to pins 0, 2, 8, 9, 10 and 11 of Arduino UNO respectively.
The circuit is assembled on a breadboard. The breadboard is provided with a common ground and a 5V power rail from one of the ground pins and the 5V pin of the Arduino UNO.
Arduino Sketch
How the project works
The LM35 is a general purpose temperature sensor from Texas Instruments. The series consists of the LM35, LM35A, LM35CA, LM35C and LM35D sensors.
The LM35 sensor used in this circuit can detect a temperature ranging from -55˚ to 150˚C. It is also internally calibrated to centigrade scale and provides an output voltage directly proportional to centigrade temperature. The sensor output is accurate to +/-0.25˚C at room temperature and +/-0.75˚C at full scale.
The LM35 sensor receives a positive or negative power supply from the Arduino. Its analog output is connected to pin A0 of the Arduino UNO.
The Arduino is programmed to sample the analog voltage from the sensor to obtain the temperature reading. In the LM35 datasheet, it can be seen that at startup, the sensor output stabilizes at 40 uSEC. Since the sensor is powered with a constant DC voltage of 5V, a stable analog output from the sensor is expected.
The LM35 sensor can be configured as a basic or full-scale centigrade temperature sensor in one circuit.
As a basic centigrade temperature sensor, the LM35 must be configured according to this circuit diagram:
As a full-scale centigrade temperature sensor, the LM35 must be configured according to this circuit diagram:
The sensor will gain 10mV per degree centigrade. In a full-scale configuration, sensor gain is obtained in reverse polarity at negative temperatures.
Temperature can be derived from the voltage output according to this equation:
Temperature (in ˚C) = LM35 output voltage (in millivolts)/10
When using Arduino, the LM35 cannot be configured as a full-scale centigrade temperature sensor as the analog pins on the Arduino board can only detect unipolar voltages. Therefore, the sensor must be configured as a basic centigrade temperature sensor, where it detects and measures temperature ranges from 2˚ to 150˚C.
For a given temperature range, the sensor's analog output must be between 20mV and 1.5V. At room temperature (25˚C), the sensor output should be 250 mV.
It is ideal to take a large number of voltage samples from the sensor and obtain an average value as the final result. This is due to breadboard connections, sensor power fluctuations, and other operational factors.
By collecting a significant number of voltage samples, a constant average temperature value can be obtained – which can be used as a reliable sensor temperature reading.
However, the temperature monitor designed here does more than just detect the current ambient temperature. It also keeps a record of the temperature over a 24-hour cycle, interpreting the minimum and maximum temperature over an entire day.
But instead of recording temperatures over a long period of time to find maximum and minimum temperatures, the Arduino is programmed to compare recently recorded temperatures with previously recorded temperatures.
The device records the first temperature reading, which involves taking 4,000 consecutive voltage samples from the LM35. The average value of these samples is then used to record the first temperature after the device is turned on. Approximately four times the number of voltage samples taken for the first reading are required to ensure a stable average value. This number of samples is collected for reliable reading after initialization.
The Arduino takes the analog voltage at pin A0 and converts it to a 10-bit digital value, ranging from 0 to 1023, using the internal ADC. The analog voltage value can be obtained by multiplying the ADC value by a reference voltage (which in this case is 5V) and dividing the expression by 1024.
Analog Voltage = ADC Value * Analog Reference / 1024
= ADC Value * 5V/1024
As the sensor gains 10mV per degree Celsius, the analog voltage (in mV when divided by 10) gives the temperature in degrees Celsius. Since the sensor-derived voltage is in Volts, the analog voltage reading must be multiplied by 1,000 to convert it to mV and then divided by 10 to obtain the correct temperature reading.
The temperature value is obtained by this equation:
Temperature (˚C) = Analog Voltage (V) * 100
After recording the first temperature reading, the current, last recorded, minimum and maximum temperatures are also defined. Next, the Arduino is programmed to take temperature readings every second. Therefore, a loop runs and iterated 86,400 times (24 hours x 60 minutes x 60 seconds).
In each iteration, the temperature of the LM35 sensor is recorded by taking 1,000 consecutive sensor voltage samples. An average value of these samples is then recorded to obtain a reliable and constant result.
The recently recorded temperature is compared with the last, minimum and maximum temperatures recorded. If the recently recorded temperature is lower than the last recorded temperature and the minimum temperature, the minimum temperature will be set to the current temperatures. Likewise, if the newly recorded temperature is higher than the last recorded and maximum recorded temperatures, the maximum temperature is set to the current temperature.
The current temperature reading and updated minimum and maximum temperature readings are displayed on the character LCD. If the recorded temperature is less than 10˚C, a zero is filled in front of it so that the displayed readings appear well formatted on the display panel.
At the end of the iteration, the current temperature reading is transferred to the last recorded temperature and given a delay of 999 milliseconds.
The Arduino will take 65 to 260 uSEC of sampling voltage on its analog input and convert it to an ADC value. After about 1,000 voltage samples are collected in each iteration, the Arduino takes less than a millisecond to obtain an average temperature reading from over 1,000 samples.
A 999 millisecond delay at the end of each iteration means it takes about a second to take two consecutive temperature readings. Therefore, every second, Arduino:
- Records a temperature reading from the LM35 sensor
- Compares with previously recorded temperatures and recorded minimum and maximum temperatures
- Updates minimum and maximum temperatures
- Displays current, minimum and maximum temperatures on LCD
- Protects the current temperature reading as the last recorded temperature
Programming guide
To begin with, the LiquidCrystal.h library is imported into the code. Then, an object defined by the variable “lcd” is defined as the LiquidCrystal class.
#include
//LCD Liquid Crystal(RS, E, D4, D5, D6, D7);
LiquidCrystal lcd(0, 2, 8, 9, 10, 11);
A global variable 'LM35' is defined to denote the analog input pin A0 where the LM35 sensor is connected. A 'vin' variable of type float is defined to hold the current ADC reading. Additionally, a 'vin_av' variable of type float is defined to hold the average of the ADC readings of the voltage samples.
A variable 'tempc' of integer type is defined to calculate and store the temperature. The variables current_temp, last_temp, min_temp and max_temp are defined to store values of the current, last recorded, minimum and maximum temperatures respectively.
A counter variable 'i' is defined for the execution loops that involve collecting analog voltage samples from the LM35 sensor. A counter variable 'j' is defined to execute all code at a 1 second interval for 24 hours.
const int LM35 = A0;
float vin;
float vin_av = 0;
int tempc, current_temp, last_temp;
int min_temp, max_temp;
float i, j;
In the configuration function, the LCD is initialized to 16×2 size using the Begin method.
null configuration
{
lcd.begin(16, 2);
}
A temp_init function is defined that records the first reading of the LM35 sensor collecting 4,000 voltage samples. The first temperature record is defined as the last recorded temperatures, minimum and maximum.
void temp_init {
for(i=0; i<4000; i++){
vin = analogRead(LM35);
vin_av = vin_av + vin;
}
vin_av = vin_av/4000;
vin_av = (vin_av/1023)*5;
tempc = vin_av*100;
last_temp = tempc;
min_temp = temp;
max_temp = tempc;
}
A LogTemp function is defined to record the temperature of the LM35 sensor by collecting 1,000 voltage samples and averaging them to obtain a reliable and constant reading.
int LogTemp {
for(i=0; i<1000; i++){
vin = analogRead(LM35);
vin_av = vin_av + vin;
}
vin_av = vin_av/1000;
vin_av = (vin_av/1023)*5;
tempc = vin_av*100;
return temperature;
}
In the loop function, the first temperature reading is taken by calling the temp_init function. A for loop is executed that iterates 86,400 times with one iteration every second.
In each iteration:
- The LCD display goes out
- Static text strings are printed on the LCD
- The current temperature reading is recorded by calling the LogTemp function
- The current temperature is compared with the last recorded, minimum and maximum temperatures
- Minimum and maximum temperature values are updated
- Current, minimum and maximum temperature values are displayed on the LCD in the appropriate format
- The current temperature reading is guaranteed to be the last recorded temperature
- A delay of 999 milliseconds is given to cover the time of one second
empty loop
{
……
}
The loop function iterates until the Arduino turns off. Thus, the Arduino records the temperature of the LM35 sensor every second and interprets the minimum and maximum temperature over a 24-hour cycle, according to the user program above.
Practical observations
When this basic Arduino-based LM35 centigrade temperature sensor was tested in a room with AC ventilation, it was found that the minimum temperature recorded was 20˚C. Without air conditioning, the maximum room temperature was 36˚C (during summer). When the air conditioning was set to 22˚C, the recorded temperature was between 24˚ and 25˚C.
(Links to Video P02-DV of this project)