import machine
import time
import ssd1306
# Initialize OLED
i2c = machine.I2C(sda=machine.Pin(26), scl=machine.Pin(27))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Initialize temperature sensor (replace with your actual sensor)
temp_sensor = machine.ADC(28) # replace with your actual sensor
conversion_factor = 3.3 / (65535)
# Initialize LEDs
led_red = machine.Pin(0, machine.Pin.OUT) # replace with your actual pins
led_green = machine.Pin(1, machine.Pin.OUT) # replace with your actual pins
while True:
# Read temperature
reading = temp_sensor.read_u16() * conversion_factor
temperature = 27 - (reading - 0.706) / 0.001721
# Display temperature on OLED
oled.fill(0)
oled.text('Temperature: {}'.format(temperature), 0, 0)
oled.show()
# Control LEDs based on temperature
if temperature > 5:
led_red.value(1)
led_green.value(0)
elif temperature < 0:
led_red.value(0)
led_green.value(1)
# Wait for a while before next reading
time.sleep(1)