from machine import Pin, I2C, ADC
from ssd1306 import SSD1306_I2C
from time import sleep
from math import log

Temp_PIN = 27
LED_PIN = 18

# Intialization of SSD1306
width = 128
height = 64
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=200000)
I2C_ADDR = i2c.scan()[0]  # Search for the address
oled = SSD1306_I2C(width, height, i2c)
oled.fill(0)
x = 10
y = 20

RED_LED = Pin(LED_PIN, Pin.OUT)


def measure_temperature():
    BETA = 3950
    adc = ADC(Pin(Temp_PIN))
    analog_value = adc.read_u16()
    celsius = 1 / (log(1 / (65535 / analog_value - 1)) / BETA + 1.0 / 298.15) - 273.15
    return celsius


def main():
    while True:
        # Measure the temperature and print the value in ℃
        temperature = measure_temperature()
        print("Temperature: {:.2f} ℃".format(temperature))

        # Wait 1 second before taking the next measurement
        sleep(1)

        # Check temperature and control LED accordingly
        if temperature > 50:
            RED_LED.high()
        else:
            RED_LED.low()

        # Update OLED display
        oled.fill(0)
        temp_str = "{:.2f} °C".format(temperature)
        oled.text(temp_str, x, y)
        oled.show()

        sleep(1)


if __name__ == "__main__":
    main()
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT