import dht
from machine import Pin, ADC
from time import sleep
# DHT22 sensor
sensor = dht.DHT22(Pin(18))
# MQ2 smoke sensor (analog)
smoke_sensor = ADC(28)
# LEDs
red = Pin(13, Pin.OUT)
green = Pin(14, Pin.OUT)
amber = Pin(15, Pin.OUT)
while True:
# Read temperature and humidity
sensor.measure()
temp = sensor.temperature()
humidity = sensor.humidity()
# Read gas sensor
gas_value = smoke_sensor.read_u16()
# Convert to voltage
voltage = (gas_value / 65535) * 3.3
# Print readings
print("Temperature:", temp, "C")
print("Humidity:", humidity, "%")
print("Gas Analog Value:", gas_value)
print("Voltage:", round(voltage, 2), "V")
# Turn OFF LEDs
red.off()
green.off()
amber.off()
# Temperature status
if temp < 30:
green.on()
print("Temperature Status: Normal Operating Condition")
elif 30 <= temp <= 35:
amber.on()
print("Temperature Status: Warning / Getting Hot")
else:
red.on()
print("Temperature Status: Critical Overheating")
# Gas status
if gas_value > 30000:
print("Gas Status: DANGER! Smoke Detected!")
elif 15000 <= gas_value <= 30000:
print("Gas Status: Warning!")
else:
print("Gas Status: Normal")
print("-----------------------------")
sleep(2)