Sensors on the Raspberry Pi
These sensors are extra: the labs read sensors on the Arduino. Use this page for projects. Python packages that are not part of Raspberry Pi OS must be installed in a virtual environment first. See Python virtual environment and Thonny.
Ultrasonic distance sensor (HC-SR04)
The HC-SR04 runs on 5V and its Echo pin outputs 5V. The Pi's GPIO is 3.3V, so put a voltage divider on Echo.
Wiring:
| HC-SR04 pin | Connect to |
|---|---|
| VCC | 5V (pin 2) |
| GND | GND (pin 6) |
| Trig | GPIO23 (pin 16) |
| Echo | through the divider to GPIO24 (pin 18) |
Voltage divider: Echo → 1 kΩ resistor → GPIO24, and GPIO24 → 2 kΩ resistor → GND. This brings 5V down to about 3.3V.
from gpiozero import DistanceSensor
from time import sleep
sensor = DistanceSensor(echo=24, trigger=23, max_distance=2)
while True:
print(f"Distance: {sensor.distance * 100:.1f} cm") # .distance is in metres
sleep(0.5)
The Grove Ultrasonic Ranger uses a single signal pin. It is easiest to use on the Arduino (see Ultrasonic sensor (Arduino)) and send the reading to the Pi.
Temperature: DS18B20 (1-Wire)
Digital, accurate (±0.5 °C) and needs no extra Python library.
- Turn on 1-Wire:
sudo raspi-config→ Interface Options → 1-Wire → Yes, then reboot. - Wire it: red → 3.3V (pin 1), black → GND (pin 6), yellow/data → GPIO4 (pin 7), and a 4.7 kΩ resistor between data and 3.3V (many modules already have it).
- Check it appears:
ls /sys/bus/w1/devices/should show a folder starting with28-.
import glob, time
device = glob.glob("/sys/bus/w1/devices/28-*")[0]
def read_temp():
with open(device + "/temperature") as f:
return int(f.read()) / 1000 # value is in thousandths of a degree
while True:
print(f"Temperature: {read_temp():.2f} °C")
time.sleep(1)
Temperature and humidity: DHT11 / DHT22
-
Create a virtual environment and install the library:
cd /home/pi # uses the lab venv (make it with --system-site-packages)source env/bin/activatepip install adafruit-circuitpython-dht -
Wire: VCC → 3.3V, GND → GND, DATA → GPIO4 (with a 10 kΩ pull-up if your sensor is not on a module).
import time, board, adafruit_dht
dht = adafruit_dht.DHT22(board.D4) # use DHT11 for the blue sensor
while True:
try:
print(f"{dht.temperature:.1f} °C {dht.humidity:.0f} %")
except RuntimeError as e:
print("Read failed, retrying:", e) # normal for DHT sensors, just retry
time.sleep(2) # do not read faster than every 2 s
PIR motion sensor
Wire VCC → 5V, GND → GND, OUT → GPIO17. The PIR's output is 3.3V, so it is safe.
from gpiozero import MotionSensor
from signal import pause
pir = MotionSensor(17)
pir.when_motion = lambda: print("Motion detected")
pir.when_no_motion = lambda: print("No motion")
pause()
Analog sensors (potentiometer, light, Grove analog)
The Pi has no analog inputs. Two options:
- Add an MCP3008 ADC chip (SPI). gpiozero supports it:
from gpiozero import MCP3008, thenMCP3008(channel=0).valuegives 0.0 to 1.0. - Read the sensor on an Arduino and send the value to the Pi over USB. See Arduino to Raspberry Pi over USB serial.
Pi CPU temperature
vcgencmd measure_temp
Or in Python: from gpiozero import CPUTemperature; print(CPUTemperature().temperature).