Home » Raspberry Pi and LM35 temperature sensor example

Raspberry Pi and LM35 temperature sensor example

by shedboy71

In this example we will connect an LM35 temperature sensor to our Raspberry Pi using an MCP3008 a/d converter

The LM35 series are precision integrated-circuit temperature sensors, whose output voltage is linearly proportional to the Celsius (Centigrade) temperature. The LM35 thus has an advantage over linear temperature sensors calibrated in Kelvin, as the user is not required to subtract a large constant voltage from its output to obtain convenient Centigrade scaling. The LM35 does not require any external calibration or trimming to provide typical accuracies of ±1/4°C at room temperature and ±3/4°C over a full -55 to +150°C  temperature range

Here is a picture of the pins, its important to get these correct or you can damage the sensor

I purchased a small module, which looks like this. This was clearly labelled and helps avoid any mistakes with wiring, I also prefer using cables to modules rather than breadboards

Schematics and Parts

You will need a way of connecting the LM35 to a Raspberry Pi, in this case an A/D converter such as an mcp3008

Very simple to connect Vcc is 3v3, Gnd is any Gnd and out goes to MCP3008 channel 0, you can see this below

Code

[codesyntax lang=”python”]

import spidev
import time

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

def readadc(adcnum):
# read SPI data from MCP3008 chip, 8 possible adc’s (0 thru 7)
if adcnum > 7 or adcnum < 0:
return -1
r = spi.xfer2([1, 8 + adcnum << 4, 0])
adcout = ((r[1] & 3) << 8) + r[2]
return adcout

while True:
value = readadc(0)
volts = (value * 3.3) / 1024
temperature = volts / (10.0 / 1000)
print (“%4d/1023 => %5.3f V => %4.1f °C” % (value, volts,
temperature))
time.sleep(0.5)

[/codesyntax]

Results

 

Links

LM35 datasheet

5PCS LM35D Temperature Sensor TO92 Packing

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More