from utime import sleep
from machine import Pin, ADC
from math import log, pow
Led = Pin(13, Pin.OUT)
Led2 = Pin(16, Pin.OUT)
Led3 = Pin(0, Pin.OUT)
Temp_Sensor = ADC(Pin(27))
Motion_Sensor = Pin(9, Pin.IN)
Light_Sensor = ADC(Pin(28))
gas_sensor = ADC(Pin(26))
buzzer = Pin(5, Pin.OUT)
while True:
#Defining Analogue Input Devices
An_Value_temp = Temp_Sensor.read_u16()
An_Value_gas = gas_sensor.read_u16()
An_Value_light = Light_Sensor.read_u16()
#Temperature Sensor Value
BETA = 3950
celsius = round(1 / (log(1 / (65535. / An_Value_temp - 1)) / BETA + 1.0 / 298.15) - 273.15)
#Photoresistor Reading
GAMMA = 0.7
RL10 = 50
An_Value_light = Light_Sensor.read_u16()
voltage = An_Value_light / 65535 * 3.3
resistance = 2000 * voltage / (3.3 - voltage)
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, 1 / GAMMA)
#Gas Sensor
gas_constant = 40000
sleep(1)
#If Conditions
if Motion_Sensor.value() == 1:
Led.value(1)
print("Motion = 1")
elif Motion_Sensor.value() == 0:
Led.value(0)
print("Motion = 0")
if celsius >= 26:
Led2.value(1)
else:
Led2.value(0)
if(lux < 500):
Led3.value(1)
else:
Led3.value(0)
if(An_Value_gas > gas_constant):
buzzer.value(1)
else:
buzzer.value(0)
print("Temperature:", celsius, "°C")
print("Light Intensity: ", lux)
print("Gas Intensity: ", An_Value_gas)
print("\n")
sleep(1)