import machine
import utime
import dht
from machine import Pin, I2C
from hcsr04 import HCSR04 # Custom library for Ultrasonic sensor
from mlx90614 import MLX90614 # Custom library for MLX90614 thermal sensor
# Setup for DHT22 Sensor
dht_sensor = dht.DHT22(Pin(22))
# Setup for PIR Sensor
pir_sensor = Pin(15, Pin.IN)
# Setup for Ultrasonic Sensor
ultrasonic_sensor = HCSR04(trigger_pin=17, echo_pin=18)
# Setup for IR Sensor
ir_sensor = Pin(16, Pin.IN)
# Setup for MLX90614 Thermal Sensor (I2C communication)
i2c = I2C(0, scl=Pin(15), sda=Pin(14), freq=100000)
mlx_sensor = MLX90614(i2c)
def read_sensors():
# Reading DHT22 Sensor
dht_sensor.measure()
temp = dht_sensor.temperature()
humidity = dht_sensor.humidity()
print("DHT22 - Temperature: {} C, Humidity: {} %".format(temp, humidity))
# Reading PIR Sensor
pir_value = pir_sensor.value()
if pir_value:
print("PIR: Motion detected!")
else:
print("PIR: No motion detected.")
# Reading Ultrasonic Sensor
distance = ultrasonic_sensor.distance_cm()
print("Ultrasonic Sensor - Distance: {:.2f} cm".format(distance))
# Reading IR Sensor
ir_value = ir_sensor.value()
if ir_value == 0:
print("IR: Obstacle detected!")
else:
print("IR: No obstacle.")
# Reading MLX90614 Thermal Sensor
ambient_temp = mlx_sensor.read_ambient_temp()
object_temp = mlx_sensor.read_object_temp()
print("MLX90614 - Ambient Temp: {} C, Object Temp: {} C".format(ambient_temp, object_temp))
# Main loop
while True:
read_sensors()
utime.sleep(2) # Delay of 2 seconds before next read
Loading
pi-pico-w
pi-pico-w