Let’s go over the application of a linear temperature sensor with the Pi. Keyestudio provides the analog LM35 sensor, with a sensitivity of 10 mV per degree Celsius, and a range between 0 and 100 degrees. While primarily designed to take advantage of the Arduino analog input channels, it’s possible to use the LM35 in conjunction with the MCP3008 analog-to-digital converter chip.


The breadboard schematic above illustrates the wiring for the LM35 (using CH0) and the MCP3008. Note that the temperature sensor is connected to the 5V supply voltage as well as the GND GPIO pins of the Raspberry Pi. The Python code below shows a simple application of the temperature sensor in an execution loop. Due to the long response times of this type of sensor, a sampling period of 0.5 seconds is plenty fast.
# Importing modules and classes
import time
import numpy as np
from gpiozero import MCP3008
# Creating ADC channel object for temperature sensor
chtemp = MCP3008(channel=0, clock_pin=11, mosi_pin=10, miso_pin=9, select_pin=8)
# Assigning some parameters
tsample = 0.5 # Sampling period for code execution (s)
tdisp = 1 # Sampling period for value display (s)
tstop = 20 # Total execution time (s)
vref = 3.3 # Reference voltage for MCP3008
ktemp = 100 # Temperature sensor gain (degC/V)
# Initializing variables and starting main clock
tprev = 0
tcurr = 0
tstart = time.perf_counter()
# Execution loop
print('Running code for', tstop, 'seconds ...')
while tcurr <= tstop:
# Pausing for `tsample`
time.sleep(tsample)
# Getting current time (s)
tcurr = time.perf_counter() - tstart
# Getting sensor normalized voltage output
valuecurr = chtemp.value
# Calculating temperature
tempcurr = vref*ktemp*valuecurr
# Displaying temperature every `tdisp` seconds
if (np.floor(tcurr/tdisp) - np.floor(tprev/tdisp)) == 1:
print("Temperature = {:d} deg C".format(int(np.round(tempcurr))))
# Updating previous time value
tprev = tcurr
print('Done.')
# Releasing GPIO pins
chtemp.close()
The next piece of code replaces the computer screen display with a 7-segment LED display. It’s a more interesting way to do both the input and output with a Raspberry Pi.
# Importing modules and classes
import time
import numpy as np
import tm1637
from gpiozero import MCP3008
# Creating 4-digit 7-segment display object
tm = tm1637.TM1637(clk=18, dio=17)
# Creating ADC channel object for temperature sensor
chtemp = MCP3008(channel=0, clock_pin=11, mosi_pin=10, miso_pin=9, select_pin=8)
# Assigning some parameters
tsample = 0.5 # Sampling period for code execution (s)
tdisp = 1 # Sampling period for value display (s)
tstop = 20 # Total execution time (s)
vref = 3.3 # Reference voltage for MCP3008
ktemp = 100 # Temperature sensor gain (degC/V)
# Initializing variables and starting main clock
tprev = 0
tcurr = 0
tstart = time.perf_counter()
# Execution loop
print('Running code for', tstop, 'seconds ...')
while tcurr <= tstop:
# Pausing for `tsample`
time.sleep(tsample)
# Getting current time (s)
tcurr = time.perf_counter() - tstart
# Getting sensor normalized voltage output
valuecurr = chtemp.value
# Calculating temperature
tempcurr = vref*ktemp*valuecurr
# Displaying temperature every `tsample` seconds
tm.temperature(int(np.round(tempcurr)))
print('Done.')
# Clearing display and releasing GPIO pins
tm.write([0, 0, 0, 0])
chtemp.close()
Temperature Sensor Accuracy

What about the accuracy of the LM35 sensor? Luckily enough I do have a LabJack T7 and an actual laboratory grade thermocouple (which has an accuracy of +/- 2 deg. C). At the time of this post, the temperature in my house was 21 deg. Celsius. Using the code above, the LM35 was reading 20 deg. C, while the thermocouple with the LabJack T7 was reading 21 deg. C.
The Python program below shows how to use the LabJack T7 for a simple temperature measurement. You can learn more about using Python to interact with a LabJack T7 here.
Unlike the thermocouple, the LM35 cannot be submerged in an ice bath (approx. 0 deg. C) or boiling water (approx. 100 deg. C), since that would clearly damage its electronic circuitry. Therefore I had to settle with the very unsophisticated measurement of the ambient temperature in my house to compare the two types of sensors.
# Importing modules and classes
import time
import numpy as np
from labjack_unified.devices import LabJackT7
# Creating LabJack object
ljt7 = LabJackT7()
# Assigning `K` type thermocouple to channel AIN0
ljt7.set_TC('AIN0', 'K')
# Assigning some parameters
tsample = 0.5 # Sampling period for code execution (s)
tdisp = 1 # Sampling period for value display (s)
tstop = 20 # Total execution time (s)
# Initializing variables and starting main clock
tprev = 0
tcurr = 0
tstart = time.perf_counter()
# Execution loop
print('Running code for', tstop, 'seconds ...')
while tcurr <= tstop:
# Pausing for `tsample`
time.sleep(tsample)
# Getting current time (s)
tcurr = time.perf_counter() - tstart
# Getting current temperature
tempcurr = ljt7.get_TCtemp()
# Displaying temperature every `tdisp` seconds
if (np.floor(tcurr/tdisp) - np.floor(tprev/tdisp)) == 1:
print("Temperature = {:d} deg C".format(int(np.round(tempcurr))))
# Updating previous time value
tprev = tcurr
print('Done.')
# Closing LabJack object
ljt7.close()