LM35 Temperature Sensor Raspberry Pi

 1. Introduction

The LM35 is an analog temperature sensor that provides output voltage proportional to temperature. Since Raspberry Pi does not have an inbuilt analog input, an ADC like MCP3008 is used to read the sensor values.


2. Components

  • Raspberry Pi
  • LM35 Temperature Sensor
  • MCP3008 ADC
  • Breadboard
  • Jumper Wires

3. Circuit and connections

Raspbery Pi
Raspberry Pi Pinout Diagram

LM35 Pinout Diagram
LM35 Pinout Diagram

Pinout Diagram MCP3008 ADC Module
Pinout Diagram MCP3008 ADC Module

Connections:

LM35 to MCP3008:

  • VCC → 3.3V
  • GND → GND
  • OUT → CH0 (MCP3008)

MCP3008 to Raspberry Pi:

  • VDD → 3.3V
  • VREF → 3.3V
  • AGND → GND
  • DGND → GND
  • CLK → GPIO11 (Pin 23)
  • DOUT → GPIO9 (Pin 21)
  • DIN → GPIO10 (Pin 19)
  • CS → GPIO8 (Pin 24)

4. Detailed Step By Step Circuit working

  1. LM35 senses temperature and outputs analog voltage
  2. Voltage increases with temperature (10mV per °C)
  3. MCP3008 converts analog signal to digital
  4. Raspberry Pi reads digital data using SPI
  5. Temperature is calculated from voltage
  6. Value is displayed on screen

5. Libraries to be included

import spidev
import time

6. Code

import spidev
import time

spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 1350000

def read_channel(channel):
adc = spi.xfer2([1, (8 + channel) << 4, 0])
data = ((adc[1] & 3) << 8) + adc[2]
return data

while True:
adc_value = read_channel(0)
voltage = adc_value * (3.3 / 1023.0)
temperature = voltage * 100

print("Temperature: {:.2f} C".format(temperature))
time.sleep(1)

7. Detailed Step By Step Code working

  1. Import SPI and time libraries
  2. Initialize SPI communication
  3. Define function to read ADC channel
  4. Read analog value from MCP3008
  5. Convert ADC value to voltage
  6. Convert voltage to temperature (×100)
  7. Print temperature
  8. Add delay for continuous reading

8. Tips

  • Use proper SPI configuration
  • Ensure correct MCP3008 wiring
  • Keep sensor away from heat sources
  • Use stable 3.3V supply

9. Uses

  • Temperature monitoring systems
  • Weather stations
  • Industrial automation
  • Home automation

10. Conclusion

The LM35 sensor provides accurate temperature measurement. Using an ADC like MCP3008 allows Raspberry Pi to read analog values efficiently.

തുടക്കക്കാർക്കായുള്ള ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.