# Import necessary libraries
import machine
import time
import math
# Define the pin for NTC sensor
NTC_PIN = 26 # Assuming you've connected to pin GP26
# Define the resistance and beta values of the NTC sensor
NTC_RESISTANCE = 10000 # Resistance of the resistor (10kΩ)
NTC_BETA = 3950 # Beta value of the NTC thermistor
# Function to read temperature from NTC sensor
def read_temperature():
adc = machine.ADC(NTC_PIN)
raw_value = adc.read_u16()
resistance = NTC_RESISTANCE / ((65535 / raw_value) - 1) # Calculate resistance
temperature = 1 / (1 / (273.15 + 25) + (1 / NTC_BETA) * math.log(resistance / 10000)) - 273.15 # Calculate temperature in Celsius
return temperature
# Main loop
while True:
temperature = read_temperature()
print("Temperature: {:.2f} °C".format(temperature))
time.sleep(1) # Delay for 1 second