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

  1. Temperature and Humidity:

    • DHT11/DHT22 (Digital, GPIO)

    • AM2302 (I2C)

    • SHT31 (I2C)

    • BME280 (I2C/SPI - also measures pressure)

  2. Barometric Pressure:

    • BMP180 (I2C)

    • BMP388 (I2C/SPI)

  3. Air Quality:

    • MQ-series gas sensors (Analog: MQ-2, MQ-135, etc.)

    • CCS811 (I2C - VOC and eCO2)

    • SGP30 (I2C - VOC and CO2)

  4. Light and UV:

    • TSL2561/TSL2591 (I2C - light intensity)

    • VEML6070 (I2C - UV index)

    • BH1750 (I2C - ambient light)

  5. Sound:

    • KY-038 (Analog/Digital - microphone)

    • MAX4466/MAX9814 (Analog - microphone)


Motion and Position Sensors

  1. Accelerometer and Gyroscope:

    • MPU6050 (I2C - 6-axis accelerometer/gyroscope)

    • LSM6DS3 (I2C/SPI - 6-axis IMU)

  2. Magnetometer:

    • HMC5883L (I2C)

  3. Inertial Measurement Units (IMU):

    • MPU9250 (I2C - 9-axis IMU)

    • BNO055 (I2C - integrated IMU with onboard processing)

  4. Distance and Proximity:

    • HC-SR04 (Digital, GPIO - ultrasonic)

    • VL53L0X (I2C - laser time-of-flight)

  5. Tilt and Vibration:

    • SW-520D (Digital - tilt sensor)

    • Piezo vibration sensors (Analog)


Electrical and Magnetic Sensors

  1. Current and Voltage:

    • INA219 (I2C - voltage and current monitoring)

    • ACS712 (Analog - current sensing)

  2. Magnetic Field:

    • A3144 (Digital - Hall effect)


Health and Biometrics Sensors

  1. Pulse and Heart Rate:

    • MAX30102/MAX30100 (I2C - pulse oximeter and heart rate)

  2. Temperature:

    • MLX90614 (I2C - infrared thermopile for contactless temperature)


Image and Optical Sensors

  1. Cameras:

    • OV7670 (SPI - low-resolution image capture)

  2. Color Sensors:

    • TCS34725 (I2C - RGB color sensing)


Touch and Gesture Sensors

  1. Touch:

    • TTP223 (Digital - capacitive touch sensor)

  2. Gesture:

    • APDS-9960 (I2C - gesture recognition)


Specialized Sensors

  1. GPS:

    • NEO-6M (UART - GPS module)

  2. RFID:

    • RC522 (SPI - RFID reader)

  3. Soil Moisture:

    • Soil moisture sensors (Analog)

  4. 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