The Raspberry Pi computer does not have a way to read analog inputs. It’s a digital-only computer. Compare this to the Arduino, AVR or PIC microcontrollers that often have 6 or more analog inputs! Analog inputs are handy because many sensors are analog outputs, so we need a way to make the Pi analog-friendly.
You can use a simple MCP3008 analog to digital converter (ADC) to read up to 8 channels of analog input with 10-bit precision. Or use a fancier ADS1x15 series ADC to read 4 channels with 12 to 16-bit precision (ADS1115), and a programmable gain stage–wow, fancy! You’ll be up and running in no time with a Python library and examples to read analog inputs from both these ADCs.
Reading temperature with LM35
1) pi@raspberrypi:~$ sudo pip3 install gpiozero
2) Enable SPI (or I2C) communication:
Serial Peripheral Interface (SPI) is a full-duplex serial protocol for communicating with high-speed peripherals.
The SPI Controller on Raspberry Pi™ hardware can drive two SPI peripheral devices. The SPI Controller has four pins:
- GPIO 11 (SPI0_SCLK) outputs a serial clock signal to synchronize communications.
- GPIO 10 (SPI0_SDO) outputs data to the SPI peripheral device.
- GPIO 9 (SPI0_SDI) receives data from the SPI peripheral device.
- GPIO 8 (SPI0_CE0) enables one SPI peripheral device.
- GPIO 7 (SPI0_CE1) enables the other SPI peripheral device.
You can connect two SPI devices, such as displays, sensors, and flash storage to the SPI pins on the Raspberry Pi hardware. Connect both devices to the SCLK, SDO, and SDI pins. Connect each device to one of the CE pins.
raspi-config – Interfacing options – SPI – reboot
3)Python code to read temperature
from gpiozero import MCP3008
from time import sleep
lectura = MCP3008(channel=0)
while True:
temperatura = (lectura.value * 3.3) * 100
print(‘Temperatura:’, temperatura, ‘C’)
sleep(0.5)
If you’re using TMP36 sensor remember to change one line of code in order to substract 0.5V:
temperatura = (lectura.value * 3.3 – 0.5) * 100
The LM35 boasts a slightly higher temperate range at -55°C to 150°C versus the -40°C to 125°C range of the TMP36. The LM35 is also more accurate with a temperature reading that is accurate to +-0.5°C compared to the +-2°C accuracy of the TMP36. Before calculating the temperature reading, we subtract 0.5V from the output voltage because the TMP36 has a 500mV offset. This offset is what gives the sensor the ability to read negative temperatures.
Another possibility is to use LM75 to measure temperature using I2C communication:
The I2C (inter integrated circuit) bus allows multiple devices to be connected to your Raspberry Pi, each with a unique address, that can often be set by changing jumper settings on the module. It is very useful to be able to see which devices are connected to your Pi as a way of making sure everything is working (not only LM35 but Arduino among many IC).

The DHT22 is a versatile and low-cost humidity and temperature sensor.
Install git to install DHT22 library:
sudo apt update
sudo apt install git
Enter the command below to verify the installation:
git –version
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo apt-get update
sudo apt-get install build-essential python-dev
sudo python setup.py install
Now you will have to reboot your Pi system to get the Adafruit driver.
#Libraries
import Adafruit_DHT as dht
from time import sleep
#Set DATA pin
DHT = 4
while True:
#Read Temp and Hum from DHT22
h,t = dht.read_retry(dht.DHT22, DHT)
#Print Temperature and Humidity on Shell window
print(‘Temp={0:0.1f}*C Humidity={1:0.1f}%’.format(t,h))
sleep(5) #Wait 5 seconds and read again
Download the code from here and open it with Thonny Python IDE or run it from terminal.
Compare data using Phyphox App for Android using t test.
Idea: Measure 2LED – 2LDR speed with Arduino inclinated plane rotational inertia.