Measure Temperature with an LM35 and Arduino: Wiring and Code

Engineering illustration: Measure Temperature with an LM35 and Arduino: Wiring and Code

Measuring temperature is one of the most popular introductory Arduino projects, and the LM35 sensor is one of the simplest and most accurate options available for it, thanks to its linear output that makes the calculation straightforward.

Sensor specifications

The LM35 gives an output that scales linearly with temperature at a rate of 10 millivolts per degree Celsius, with an accuracy of about 0.5°C at 25°C. It draws no more than 60 microamps, with negligible self-heating of under 0.08°C in still air. Its main advantage is that its reading is calibrated directly in Celsius, with no extra calibration needed from the user.

Wiring it up

The sensor has only three pins: the two power pins connect to the Arduino's 5V and ground, while the signal pin connects to analog pin A2.

The conversion formula

The code relies on the following to convert the analog reading into an actual Celsius temperature:

temp = analogRead(tempPin);
temperatureC = analogRead(tempPin) * (vRef / 1023.0) / 0.010;

The approximate 0.488 value comes from dividing 5000 millivolts by 1023 ADC intervals, then dividing by the LM35 sensitivity of 10 millivolts per degree Celsius (the number of levels a 10-bit analog-to-digital converter can distinguish across a 0-5V range). The reading can then also be shown in Fahrenheit using the well-known formula: (Celsius × 9 ÷ 5) + 32, with both values typically displayed together on the Serial Monitor.

Where is it used?

The LM35 sensor is used in HVAC control systems, small-scale weather stations, overheating alarm systems for electronic devices, and other applications that need a simple, accurate temperature reading.

The LM35 sensor and the complete set of components for this project are available at Juthour Tech.

Improve measurement quality

  • Verify the pin order for your exact package in the datasheet; reversing the device can damage it.
  • Measure the real ADC reference voltage instead of assuming exactly 5.000V, and average 16–32 samples.
  • Place a 100nF bypass capacitor close to the sensor and keep its signal wire away from motors and relays.
  • Total uncertainty includes sensor error, ADC reference error, and noise; ADC resolution alone is not system accuracy.

Trusted reference: Texas Instruments LM35 datasheet.

Leave your comment

*
*
Only registered users can leave comments.