4. Sensors
There are a huge number of sensors available to us. Here we will learn about PIR motion detectors and an I2C environmental sensor
Environmental Sensors
Temperature and Humidity:
DHT11/DHT22 (Digital, GPIO)
AM2302 (I2C)
SHT31 (I2C)
BME280 (I2C/SPI - also measures pressure)
Barometric Pressure:
BMP180 (I2C)
BMP388 (I2C/SPI)
Air Quality:
MQ-series gas sensors (Analog: MQ-2, MQ-135, etc.)
CCS811 (I2C - VOC and eCO2)
SGP30 (I2C - VOC and CO2)
Light and UV:
TSL2561/TSL2591 (I2C - light intensity)
VEML6070 (I2C - UV index)
BH1750 (I2C - ambient light)
Sound:
KY-038 (Analog/Digital - microphone)
MAX4466/MAX9814 (Analog - microphone)
Motion and Position Sensors
Accelerometer and Gyroscope:
MPU6050 (I2C - 6-axis accelerometer/gyroscope)
LSM6DS3 (I2C/SPI - 6-axis IMU)
Magnetometer:
HMC5883L (I2C)
Inertial Measurement Units (IMU):
MPU9250 (I2C - 9-axis IMU)
BNO055 (I2C - integrated IMU with onboard processing)
Distance and Proximity:
HC-SR04 (Digital, GPIO - ultrasonic)
VL53L0X (I2C - laser time-of-flight)
Tilt and Vibration:
SW-520D (Digital - tilt sensor)
Piezo vibration sensors (Analog)
Electrical and Magnetic Sensors
Current and Voltage:
INA219 (I2C - voltage and current monitoring)
ACS712 (Analog - current sensing)
Magnetic Field:
A3144 (Digital - Hall effect)
Health and Biometrics Sensors
Pulse and Heart Rate:
MAX30102/MAX30100 (I2C - pulse oximeter and heart rate)
Temperature:
MLX90614 (I2C - infrared thermopile for contactless temperature)
Image and Optical Sensors
Cameras:
OV7670 (SPI - low-resolution image capture)
Color Sensors:
TCS34725 (I2C - RGB color sensing)
Touch and Gesture Sensors
Touch:
TTP223 (Digital - capacitive touch sensor)
Gesture:
APDS-9960 (I2C - gesture recognition)
Specialized Sensors
GPS:
NEO-6M (UART - GPS module)
RFID:
RC522 (SPI - RFID reader)
Soil Moisture:
Soil moisture sensors (Analog)
Water Level/Leak Detection:
YL-69 (Analog - water level)
PIR Sensor
A Passive Infra Red sensor senses changes in heat and are often found in alarm systems


Here we will activate a buzzer when motion is detected

import board
import digitalio
import pwmio
# Setup digital input for PIR sensor:
pir = digitalio.DigitalInOut(board.GP16)
pir.direction = digitalio.Direction.INPUT
buzzer = pwmio.PWMOut(board.GP14, variable_frequency=True)
OFF = 0
ON = 2**15 #Sets the Duty Cycle
# Main loop that will run forever:
old_value = pir.value
while True:
pir_value = pir.value
if pir_value == True:
# PIR is detecting movement! Turn on Buzzer.
buzzer.frequency = 440
buzzer.duty_cycle = ON
# Check if this is the first time movement was
# detected and print a message!
if not old_value:
print('Motion detected!')
else:
# PIR is not detecting movement. Turn off Buzzer.
buzzer.duty_cycle = OFF
# Again check if this is the first time movement
# stopped and print a message.
if old_value:
print('Motion ended!')
old_value = pir_value
'''CHALLENGES:
1. Can you turn on a red led when motion is detected
2. Can you get the buzzer to alternate between two different frequencies
'''AHT 20 Environmental Sensor
The AHT 20 sensor detected temperature and humidity


#import libraries
import time
import board, busio
import adafruit_ahtx0
# Setup
i2c = busio.I2C(board.GP17, board.GP16) #SCL, SDA
sensor = adafruit_ahtx0.AHTx0(i2c)
#Loop
while True:
print("\nTemperature: %0.1f C" % sensor.temperature)
print("Humidity: %0.1f %%" % sensor.relative_humidity)
time.sleep(2)
'''
Challenge: Can you turn the temperature sensor into a touch sensor that
turns on an led when you put your finger on it?
'''
Last updated
Was this helpful?