# Course : BUEL456C - Introduction to Raspberry Pi (VTU)
# Wokwi : NTC Temperature Sensor on GP26 simulates LDR ADC
# Click the NTC sensor slider to change temperature and
# observe how the raw ADC value and voltage change
from machine import ADC, Pin
import math
import time
# ---- ADC setup on GP26 (NTC OUT pin) ------------------------
adc = ADC(Pin(26)) # NTC analog output --> GP26 (ADC0)
# ---- NTC constants (Steinhart-Hart / Beta equation) ---------
BETA = 3950 # NTC Beta coefficient
R_REF = 10000 # Reference resistor = 10 kOhm
R_NOM = 10000 # NTC nominal resistance at 25 C
T_NOM = 298.15 # 25 C in Kelvin
# ---- Classification thresholds (temperature based) ----------
HOT = 35 # above this --> HOT
COLD = 15 # below this --> COLD
# ---- Convert raw ADC to temperature (Celsius) ---------------
def raw_to_temp(raw):
if raw <= 0 or raw >= 65535:
return None
voltage = raw * 3.3 / 65535
resistance = R_REF * voltage / (3.3 - voltage)
kelvin = 1.0 / (math.log(resistance / R_NOM) / BETA
+ 1.0 / T_NOM)
return kelvin - 273.15
# ---- Classify temperature -----------------------------------
def classify(temp):
if temp is None: return "ERROR "
if temp > HOT: return "HOT "
if temp < COLD: return "COLD "
return "NORMAL"
# ---- ASCII bar graph (0 C = empty, 50 C = full) -------------
def bar(temp):
if temp is None: return "[" + "?" * 20 + "]"
filled = int(max(0, min(20, (temp / 50) * 20)))
return "[" + "#" * filled + " " * (20 - filled) + "]"
# ---- Main program -------------------------------------------
print("=" * 58)
print(" NTC Sensor + Pico ADC | BUEL456C Wokwi")
print(" Click the NTC sensor to change temperature")
print("=" * 58)
print(f" Cold < {COLD} C | Hot > {HOT} C")
print("-" * 58)
while True:
raw = adc.read_u16()
volt = raw * 3.3 / 65535
temp = raw_to_temp(raw)
pct = (raw / 65535) * 100
if temp is not None:
print(f" Raw:{raw:6d} | {volt:.3f}V | "
f"{pct:5.1f}% | {temp:6.2f}C | "
f"{bar(temp)} | {classify(temp)}")
time.sleep(1)Loading
pi-pico
pi-pico