Raspberry Pi - LM35 Temperature Sensor

This tutorial instructs you how to use Raspberry Pi to read the temperature from LM35 sensor. In detail, we will learn:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×LM35 Temperature Sensor
1×ADS1115 ADC Module
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of LM35 Temperature Sensor

The LM35 Temperature Sensor Pinout

The LM35 temperature sensor has three pins:

  • GND pin: This should be linked to GND (0V)
  • VCC pin: This should be linked to VCC (5V)
  • OUT pin: This signal pin provides an output voltage that is linearly proportional to the temperature, and should be connected to an analog pin on the Raspberry Pi.
LM35 temperature sensor pinout

How It Works

The LM35 produces an output voltage that is linearly proportional to the Centigrade temperature. The scale factor of the LM35 is 10 mV/°C, meaning that the temperature can be determined by dividing the voltage (in mV) at the output pin by 10.

Since Raspberry Pi does not have an analog input pin, we need to use the ADC module such as ADS1115 ADC Module

Wiring Diagram

The wiring diagram between Raspberry Pi and LM35 temperature sensor

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code

# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-lm35-temperature-sensor import Adafruit_ADS1x15 import time # Create an ADC instance (ADS1015 or ADS1115) adc = Adafruit_ADS1x15.ADS1115() # Analog input channel connected to the LM35 analog_channel = 0 def read_lm35_temperature(): # Read the raw ADC value raw_value = adc.read_adc(analog_channel, gain=1) # Convert the raw ADC value to millivolts millivolts = (raw_value * 4.096) / 32767.0 * 1000 # Convert millivolts to Celsius using the LM35 formula temperature_celsius = millivolts / 10.0 # Convert Celsius to Fahrenheit temperature_fahrenheit = (temperature_celsius * 9/5) + 32 return temperature_celsius, temperature_fahrenheit try: while True: celsius, fahrenheit = read_lm35_temperature() print(f'Temperature: {celsius:.2f}°C ~ {fahrenheit:.2f}°F') time.sleep(1) except KeyboardInterrupt: pass

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Install the Adafruit_ADS1x15 library by running the following commands on your Raspberry Pi terminal:
sudo pip install Adafruit-ADS1x15
  • Create a Python script file LM35.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-lm35-temperature-sensor import Adafruit_ADS1x15 import time # Create an ADC instance (ADS1015 or ADS1115) adc = Adafruit_ADS1x15.ADS1115() # Analog input channel connected to the LM35 analog_channel = 0 def read_lm35_temperature(): # Read the raw ADC value raw_value = adc.read_adc(analog_channel, gain=1) # Convert the raw ADC value to millivolts millivolts = (raw_value * 4.096) / 32767.0 * 1000 # Convert millivolts to Celsius using the LM35 formula temperature_celsius = millivolts / 10.0 # Convert Celsius to Fahrenheit temperature_fahrenheit = (temperature_celsius * 9/5) + 32 return temperature_celsius, temperature_fahrenheit try: while True: celsius, fahrenheit = read_lm35_temperature() print(f'Temperature: {celsius:.2f}°C ~ {fahrenheit:.2f}°F') time.sleep(1) except KeyboardInterrupt: pass
  • Save the file and run the Python script by executing the following command in the terminal:
python3 LM35.py
  • Hold the sensor in your hand.
  • Check the output in the Terminal.
PuTTY - Raspberry Pi
Temperature: 26.31°C ~ 79.36°F Temperature: 26.44°C ~ 79.59°F Temperature: 26.50°C ~ 79.70°F Temperature: 26.56°C ~ 79.81°F Temperature: 27.06°C ~ 80.71°F Temperature: 27.75°C ~ 81.95°F Temperature: 28.37°C ~ 83.07°F Temperature: 29.00°C ~ 84.20°F Temperature: 29.56°C ~ 85.21°F Temperature: 30.00°C ~ 86.00°F Temperature: 30.31°C ~ 86.56°F Temperature: 30.62°C ~ 87.12°F Temperature: 30.87°C ~ 87.57°F

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Video Tutorial