How to Read Temperature Using LM35 Sensor in Arduino
To read temperature using an
LM35 sensor in Arduino, connect the sensor's output to an analog pin and use analogRead() to get the voltage value. Convert this value to temperature by multiplying the voltage by 100 because LM35 outputs 10mV per °C.Syntax
The basic syntax to read temperature from an LM35 sensor involves reading the analog input and converting it to Celsius.
analogRead(pin): Reads the voltage from the sensor connected to the specified analog pin.- Conversion formula:
temperature = (analogValue * (5.0 / 1023.0)) * 100converts the analog reading to temperature in Celsius.
arduino
int sensorPin = A0; // Analog pin connected to LM35 int analogValue = analogRead(sensorPin); // Read analog value float voltage = analogValue * (5.0 / 1023.0); // Convert to voltage float temperatureC = voltage * 100; // Convert voltage to temperature in Celsius;
Example
This example reads the temperature from the LM35 sensor connected to analog pin A0 and prints the temperature in Celsius to the Serial Monitor every second.
arduino
const int sensorPin = A0; void setup() { Serial.begin(9600); // Start serial communication } void loop() { int analogValue = analogRead(sensorPin); // Read sensor value float voltage = analogValue * (5.0 / 1023.0); // Convert to voltage float temperatureC = voltage * 100; // Convert voltage to temperature Serial.print("Temperature: "); Serial.print(temperatureC); Serial.println(" °C"); delay(1000); // Wait 1 second before next reading }
Output
Temperature: 23.45 °C
Temperature: 23.50 °C
Temperature: 23.48 °C
...
Common Pitfalls
- Incorrect pin connection: Make sure the LM35 output pin is connected to an analog input pin, not a digital pin.
- Wrong voltage reference: The Arduino analog input reads 0-5V by default; if using a different reference voltage, adjust the formula accordingly.
- Ignoring sensor power: LM35 needs 5V and ground connections; missing these will give wrong readings.
- Not converting analog value properly: Forgetting to scale the analog reading to voltage and then to temperature causes incorrect results.
arduino
/* Wrong way: Using digitalRead instead of analogRead */ int sensorPin = A0; int value = digitalRead(sensorPin); // WRONG: LM35 output is analog /* Right way: Use analogRead and convert */ int analogValue = analogRead(sensorPin); float voltage = analogValue * (5.0 / 1023.0); float temperatureC = voltage * 100;
Quick Reference
| Step | Description |
|---|---|
| Connect LM35 output to Arduino analog pin (e.g., A0) | Read analog voltage from sensor |
| Use analogRead(pin) to get sensor value | Returns 0-1023 corresponding to 0-5V |
| Convert analog value to voltage | voltage = analogValue * (5.0 / 1023.0) |
| Convert voltage to temperature | temperatureC = voltage * 100 (°C) |
| Print or use temperature value in your program | Use Serial.print() or other output |
Key Takeaways
Connect LM35 output to an Arduino analog pin and power it with 5V and ground.
Use analogRead() to get the sensor's voltage reading.
Convert the analog reading to voltage, then multiply by 100 to get temperature in Celsius.
Always verify wiring and use the correct conversion formula for accurate temperature readings.
Print temperature values to Serial Monitor for easy debugging.