# IMPORTS
from machine import Pin, ADC, I2C
from picozero import Speaker
from time import sleep
from math import log
# PINS
motion = Pin(15, Pin.OUT)
led1 = Pin(2, Pin.OUT)
ntc = ADC(Pin(27))
ldr = ADC(Pin(26))
mq2 = ADC(Pin(28))
led2 = Pin(6, Pin.OUT)
led3 = Pin(10, Pin.OUT)
buzzer = Speaker(13)
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
while True:
sleep(0.02) # A short delay to let the simulation start
BETA = 3950
analogValue = ntc.read_u16()
celsius = round(1 / (log(1 / (65535. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15)
GAMMA = 0.7
RL10 = 50
analogValue2 = ldr.read_u16()
voltage = analogValue2 / 65535. * 5
resistance = 2000 * voltage / (1 - voltage / 5)
lux = round(pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA)))
# Analog Value Conversion
if motion.value():
led1.on()
#print("Motion detected")
else:
led1.off() # Red led turns off if motion has stopped
#print("Motion stopped")
if lux >= 500:
led3.off()
#print("Light")
else:
led3.on()
#print("Dark")
#print(f"Temperature is {celsius}°C")
if mq2.read_u16() >= 58654:
#print("Smoke: Detected")
buzzer.on()
else:
#print("Smoke: 0")
buzzer.off()
print("")
if celsius >= 35:
led2.on() # On if temperature is greater than or equal to 35
else:
led2.off() # Off if temperature is less than 35
sleep(2)