# modules
from machine import Pin
from time import sleep
from dht import DHT22
import utime # if the sensor is DHT11, import DHT11 instead of DHT22
# creating a DHT object
# change DHT22 to DHT11 if DHT11 is used
dht = DHT22(Pin(15))
trigger = Pin(27, Pin.OUT)
echo = Pin(26, Pin.IN)
# continuously get sensor readings while the board has power
while True:
# getting sensor readings
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
# displaying values to the console
print(f"Temperature: {temp}°C Humidity: {hum}% ")
# format method or string concatenation may also be used
#print("Temperature: {}°C Humidity: {:.0f}% ".format(temp, hum))
#print("Temperature: " + str(temp) + "°C" + " Humidity: " + str(hum) + "%")
# delay of 2 secs because DHT22 takes a reading once every 2 secs
sleep(2)
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.0343) / 2
print("Distance 1: ", distance, "cm")
while True:
ultra()
utime.sleep(1)