from machine import Pin
import utime
from time import sleep
from dht import DHT22
dht = DHT22(Pin(27))
led = Pin(3, Pin.OUT)
pir = Pin(7, Pin.IN, Pin.PULL_UP)
trigger = Pin(28,Pin.OUT)
echo = Pin(18, Pin.IN)
def ultra():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() ==1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed * 0.0330) / 2
print("The object can be seen from a distance of",distance, "cm")
while True:
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
print("Temperature: " + str(temp) + "°C" + " Humidity: " + str(hum) + "%")
ultra()
sleep(2)
if pir.value() == 1:
# Motion detected, turn on the LED
led.high()
else:
# No motion, turn off the LED
led.low()
utime.sleep(0.1)