import machine
import math
import utime
# Constants
BETA = 3950 # Beta coefficient of the thermistor
R1 = 10000 # Series resistor value (in ohms)
V_REF = 3.3 # Reference voltage (Raspberry Pi Pico)
# Initialize ADC on Pin 28
adc = machine.ADC(machine.Pin(28))
while True:
# Read the raw ADC value
analog_value = adc.read_u16()
# Convert ADC value to voltage
voltage = (analog_value / 65535) * V_REF
# Calculate the resistance of the thermistor
if voltage == 0:
print("Voltage is 0V, skipping calculation to avoid division by zero.")
continue
resistance = R1 / ((V_REF / voltage) - 1)
# Calculate temperature in Kelvin using the Beta formula
kelvin = 1 / (1 / 298.15 + (1 / BETA) * math.log(resistance / 10000))
# Convert Kelvin to Celsius
celsius = kelvin - 273.15
# Print the temperature
print("CSE Classroom Temperature: {:.2f} °C (DR.G.U POPE)".format(celsius))
# Delay for 1 second
utime.sleep(1)