from machine import ADC, Pin
from math import log
import time
from utime import sleep
BETA = 3950 # Beta coefficient of the thermistor
R0 = 10000 # Resistance of thermistor at 25 degrees Celsius
T0 = 298.15 # Reference temperature (Kelvin) for Beta calculation
adc = ADC(Pin(28))
last_temperature = None
pins1 = [
Pin(2, Pin.OUT), # A
Pin(3, Pin.OUT), # B
Pin(4, Pin.OUT), # C
Pin(5, Pin.OUT), # D
Pin(6, Pin.OUT), # E
Pin(8, Pin.OUT), # F
Pin(7, Pin.OUT), # G
Pin(0, Pin.OUT) # DP (not connected)
]
# Define pins for the second 7-segment display (rightmost digit)
pins2 = [
Pin(16, Pin.OUT), # A
Pin(17, Pin.OUT), # B
Pin(18, Pin.OUT), # C
Pin(21, Pin.OUT), # D
Pin(20, Pin.OUT), # E
Pin(22, Pin.OUT), # F
Pin(26, Pin.OUT), # G
Pin(19, Pin.OUT) # DP (not connected)
]
digits1 = [1, 1, 1, 1, 1, 1, 0, 1], # -
# Common anode 7-segment display digit patterns for both displays
digits = [
[0, 0, 0, 0, 0, 0, 1, 1], # 0
[1, 0, 0, 1, 1, 1, 1, 1], # 1
[0, 0, 1, 0, 0, 1, 0, 1], # 2
[0, 0, 0, 0, 1, 1, 0, 1], # 3
[1, 0, 0, 1, 1, 0, 0, 1], # 4
[0, 1, 0, 0, 1, 0, 0, 1], # 5
[0, 1, 0, 0, 0, 0, 0, 1], # 6
[0, 0, 0, 1, 1, 1, 1, 1], # 7
[0, 0, 0, 0, 0, 0, 0, 1], # 8
[0, 0, 0, 1, 1, 0, 0, 1] # 9
]
def read_temperature():
analogValue = adc.read_u16()
if analogValue == 0: # Avoid division by zero
analogValue = 1
resistance = (65535 / analogValue) - 1
resistance = R0 / resistance
temperature = 1 / (log(resistance / R0) / BETA + 1.0 / T0) - 273.15
return temperature
def reset():
"""Turns off all segments on both 7-segment displays."""
for pin1, pin2 in zip(pins1, pins2):
pin1.value(1)
pin2.value(1)
reset()
while True:
temperature = read_temperature()
if last_temperature is None or abs(temperature - last_temperature) >= 0.1:
print("Temperature: {:.2f} ℃".format(temperature))
last_temperature = temperature
t1=int(temperature//10)
t2=int(temperature%10)
print(t1,t2)
time.sleep(0.1)
if temperature <= 0:
b=abs(temperature)
t3=int(b%10)
for j in range(len(pins1)):
pins1[j].value(digits1[0][j])
for a in range(len(pins2)):
pins2[a].value(digits[t3][a])
else:
for j in range(len(pins1)):
pins1[j].value(digits[t1][j])
pins2[j].value(digits[t2][j])