LM35 is an analog temperature sensor, this sensor has analog voltage output. This sensor is cheap and quite easy to use. Before we continue let’s see features first. This is the complete datasheet.
- Temperature range from -50℃ – 150℃
- 10mV/℃ scale factor
- 4-30V power supply
- Calibrated Directly in Celsius (Centigrade)
- Low Self-Heating, 0.08°C in Still Air
Wiring Diagram
So we should connect the VCC to 5V, GND to GND, and the output to the analog pin of Arduino, in this example I put it on A0.
The Code / Sketch
We can start by using the Arduino IDE example sketch, I use the ReadAnalogVoltage. Open the sketch and we will need to do a little editing. OK, so this sketch is actually used to read the voltage that is connected at pin A0.
And then we need to convert the voltage to temperature. How to do that?
Did you still remember the datasheet earlier? The sensor will output 10mV/degree of Celcius. So the temperature is Voltage in millivolt divided by 10 or the voltage in volt times 100;
So I made a float temperature variable and gave it the voltage times 100.
After that, I print the voltage and temperature to the serial monitor. Here’s the edited sketch
// the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): float voltage = sensorValue * (5.0 / 1023.0); float temperature = voltage*100; // print out the value you read: Serial.println("Voltage : "); Serial.println(voltage); Serial.println("Temperature : "); Serial.println(temperature); delay(500); }
Upload the sketch, and after the upload is completed, then we can open the serial monitor.
And that’s the result, my room temperature is read at about 27 degrees of Celsius and 28. and of course the voltage is about 0.27 and 0.28.
So that’s it, we have successfully read the temperature using the LM35 sensor. Now, how if we wanted to display the temperature to an LCD? easy! just connect an LCD. Here’s the wiring diagram.
I use the LCD with an I2C backpack so it just needs to connect the I2C pin to Arduino. As usual, I2C means SDA to A4 and SCL to A5. Don’t forget the VCC and ground.
After the wiring is completed, now time to go back to Arduino IDE. So, I already made the code to display the temperature to LCD.
#include <Wire.h> #include <LiquidCrystal_I2C.h> // Set the LCD address to 0x27 for a 16 chars and 2 line display LiquidCrystal_I2C lcd(0x27, 16, 2); // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); lcd.init(); lcd.backlight(); lcd.print("LM35 Test"); delay(1000); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): float voltage = sensorValue * (5.0 / 1023.0); float temperature = voltage*100; // print out the value you read: Serial.println("Voltage : "); Serial.println(voltage); Serial.println("Temperature : "); Serial.println(temperature); lcd.setCursor(0,0); lcd.print("T :"); lcd.print(temperature); lcd.print((char)223); lcd.print("C"); delay(500); }
Here’s the result, the temperature is now displayed on the LCD. Now to make sure the sensor is working, I will put a hot soldering iron on the sensor, and we can see the temperature is increasing fast.
Now if I take back the soldering iron, it slowly drops to normal.
Thank you for reading this tutorial! and by the way here’s the full video on this tutorial.