from machine import Pin, ADC
from time import sleep
# Initialize temperature sensor and components
temp = ADC(Pin(33))
temp.atten(ADC.ATTN_11DB) # Allows reading from 0 to 3.3V
temp.width(ADC.WIDTH_12BIT) # 12-bit resolution (0-4095)
r = Pin(12, Pin.OUT)
g = Pin(14, Pin.OUT)
b = Pin(27, Pin.OUT)
buzzer = Pin(15, Pin.OUT)
while True:
raw = temp.read() # Raw ADC value
voltage = raw * (3.3 / 4095) # Convert ADC value to voltage
temperature_c = voltage * 100 # Assuming 10mV/°C (for LM35)
print("Temperature:", temperature_c, "°C")
# Buzzer on if temp > 27°C
if temperature_c > 27:
buzzer.value(1)
else:
buzzer.value(0)
# LED logic
if 0 <= temperature_c <= 25:
r.off()
g.on()
b.off()
elif 26 <= temperature_c <= 35:
r.on()
g.off()
b.off()
else:
r.off()
g.off()
b.on()
sleep(1)