import machine
import utime
# Initialize I2C display
from machine import I2C, Pin
import ssd1306
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Initialize the temperature sensor
temp_sensor = machine.ADC(4)
# Initialize the fan control pin
fan_pin = machine.Pin(2, machine.Pin.OUT)
while True:
# Read temperature from the integrated sensor (or DS18B20)
temperature = temp_sensor.read_u16() * 3.3 / 65535
# Convert the temperature to degrees Celsius
temperature = (temperature - 0.5) * 100
# Display temperature on the OLED display
oled.fill(0)
oled.text("Temperature:", 0, 0)
oled.text("{:.2f} C".format(temperature), 0, 20)
oled.show()
# Check if the temperature is above 30°C and turn on the fan
if temperature > 30:
fan_pin.on()
else:
fan_pin.off()
utime.sleep(10) # Adjust the sleep time as needed