from machine import Pin
from machine import ADC, Pin
from math import log
import time
# 7-segment display layout
# A
# ---
# F | G | B
# ---
# E | | C
# ---
# D
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))
oldTemperature = 0
pins = [
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)
]
pins2 = [
Pin(26, Pin.OUT), # A
Pin(22, Pin.OUT), # B
Pin(21, Pin.OUT), # C
Pin(20, Pin.OUT), # D
Pin(19, Pin.OUT), # E
Pin(17, Pin.OUT), # F
Pin(18, Pin.OUT), # G
Pin(0, Pin.OUT) # DP (not connected)
]
# Common anode 7-segment display digit patterns
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
[0, 0, 0, 1, 0, 0, 0, 1], # a
[1, 1, 0, 0, 0, 0, 0, 1], # b
[0, 1, 1, 0, 0, 0, 1, 1], # C
[1, 0, 0, 0, 0, 1, 0, 1], # d
[0, 1, 1, 0, 0, 0, 0, 1], # E
[0, 1, 1, 1, 0, 0, 0, 1], # F
]
def reset():
"""Turns off all segments on the 7-segment display."""
for pin in pins2:
pin.value(1)
reset()
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
while True:
temperature = int(read_temperature())
if temperature < 0:
temperature = 0
if oldTemperature != temperature:
oldTemperature = temperature
print(temperature)
i1 = int(temperature / 10)
i2 = int(str(temperature)[-1])
for j in range(len(pins2) - 1):
pins[j].value(digits[i1][j])
pins2[j].value(digits[i2][j])
time.sleep(0.1)