from machine import ADC, Pin
import time
import math
# Define the pin connected to the NTC thermistor
ntc_pin = Pin(26)
# Create ADC object
adc = ADC(0) # Use ADC 0 (0-65535)
# Function to read temperature from NTC sensor
def read_temperature():
# Read raw analog value from ADC
adc_value = adc.read_u16()
# Convert ADC value to voltage
voltage = adc_value * 3.3 / 65535 # 3.3V reference voltage
# Calculate resistance of NTC based on voltage divider
resistor = 10000 # 10kOhm resistor
ntc_resistance = resistor * (3.3 / voltage - 1)
# Calculate temperature using Steinhart-Hart equation
# NTC parameters: resistance at 25°C (R25), B coefficient, reference temperature (Tref)
R25 = 10000 # Resistance of NTC at 25°C
B_coefficient = 3950 # B coefficient of NTC
Tref = 298.15 # Reference temperature in Kelvin (25°C)
# Calculate temperature in Kelvin
temperature_kelvin = 1 / (1 / Tref + 1 / B_coefficient * math.log(ntc_resistance / R25))
# Convert temperature from Kelvin to Celsius
temperature_celsius = temperature_kelvin - 273.15
return temperature_celsius
# Main loop
while True:
# Read temperature from NTC sensor
temperature = read_temperature()
# Print temperature
print("Temperature: {:.2f} °C".format(temperature))
# Wait for a short period
time.sleep(1)