#Import Library
import ultrasonic_lib
from dht import DHT22
from machine import Pin, ADC
from time import sleep
#Pin Declaration
TRIG = Pin(13)
ECHO = Pin(12)
Red_LED_pin = Pin(4,Pin.OUT)
Green_LED_pin = Pin(2,Pin.OUT)
Pir_pin = Pin(27,Pin.IN,Pin.PULL_UP)
dht_sensor = DHT22(Pin(14))
ldr = ADC(25)
light = Pin(5,Pin.OUT)
fan = Pin(4,Pin.OUT)
motor = Pin(2,Pin.OUT)
buzzer = Pin(18,Pin.OUT)
#Create object name
sonic = ultrasonic_lib.HCSR04(trigger_pin=TRIG, echo_pin=ECHO)
#Main Program
while True:
# water level
level =(-(sonic.distance_cm() / 4) + 100)
# temperature
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
# light indencity
darkness = round((ldr.read() / 4098) * 100)
if level <= 10 :
motor.value(1)
elif level >= 90:
motor.value(0)
if temperature >= 24:
fan.value(1)
else:
fan.value(0)
#light.value(not light.value())
if darkness <= 30:
light.value(1)
elif darkness >= 60:
light.value(0)
if Pir_pin.value() == 1:
buzzer.value(1)
else:
buzzer.value(0)
print('Water Level :',round(level),'\tTemperature :',temperature,'\tDarkness :',darkness)
print(buzzer.value())
sleep(0.1)