from machine import Pin, ADC, Timer
import time
import math
# Segment and digit pin setup
segment_pins = [Pin(i, Pin.OUT) for i in range(7)]
digit_pins = [Pin(i, Pin.OUT) for i in [11, 10, 9, 8]] # Digit 0–3
dp = Pin(7, Pin.OUT)
# Button input
button_pin = Pin(16, Pin.IN, Pin.PULL_UP)
# ADCs
adc = ADC(Pin(26)) # Slide potentiometer
# Segment codes: 0–9, '-', blank
displayCodes = [
0x3F,
0x06,
0x5B,
0x4F,
0x66,
0x6D,
0x7D,
0x07,
0x7F,
0x6F,
0x40,
0x00 # '-' = 10, blank = 11
]
# Globals
display_value = 162345
display_mode = 0
current_digit = 0
last_button_time = 0
display_timer = Timer()
digit = [0,1,2,3]
# --- Sensor Reading Functions ---
def read_temperature():
try:
val = adc.read_u16()
voltage = (val / 65535) * 3.3
#BETA = 3950
#resistance = 10000 / (3.3 / voltage - 1)
#temp = 1 / (math.log(resistance / 10000) / BETA + 1 / 298.15) - 273.15
return voltage
except:
return 0
def read_voltage_mv():
val = adc.read_u16()
return int((val / 65535) * 3300)
def read_light_percent():
val = adc.read_u16()
return int((val / 65535) * 100)
# --- Display Functions ---
def update_display_value():
global display_value
if display_mode == 0:
display_value = read_temperature()
#display_value = int(temp) # e.g., 23.6°C → 236
print(f"Voltage: {display_value:}V")
elif display_mode == 1:
voltage_mv = read_voltage_mv()
display_value = voltage_mv # e.g., 3.300V → 3300
print(f"Voltage: {voltage_mv / 1000:.3f}V")
elif display_mode == 2:
light = read_light_percent()
display_value = light # Percent: 0–100
print(f"Voltage: {light}V")
def display_digit(digit_value, digit_index, dp_enable=False):
for i in range(7):
segment_pins[i].value(not ((displayCodes[digit_value] >> i) & 1))
for i, pin in enumerate(digit_pins):
pin.value(1 if i == digit_index else 0)
dp.value(0 if dp_enable else 1)
def clear_display():
time.sleep(0.01)
for i, pin in enumerate(digit_pins):
pin.value(0)
dp.value(1)
def charToDigit(char):
if char == '-':
digit = 10
elif char.isdigit():
digit = int(char)
else:
digit = 11
return digit
def scan_display(timer=None):
global current_digit
val = abs(display_value)
val_str = str(val)
#if len(val_str) < 4:
#val_str = (" " * (4 - len(val_str))) + val_str
#elif len(val_str) > 4:
#val_str = val_str[-4:]
display_digit (charToDigit(val_str[0]),0,True)
clear_display()
display_digit(charToDigit(val_str[2]),1)
clear_display()
display_digit(charToDigit(val_str[3]),2)
clear_display()
display_digit(charToDigit(val_str[4]),3)
clear_display()
def button_handler(pin):
global display_mode, last_button_time
now = time.ticks_ms()
if time.ticks_diff(now, last_button_time) > 300:
#display_mode = (display_mode + 1) % 3
last_button_time = now
update_display_value()
print(f"Switched to mode: {display_mode} (0=Temp, 1=Volt, 2=Light)")
# --- Main ---
if __name__ == '__main__':
button_pin.irq(trigger=Pin.IRQ_FALLING, handler=button_handler)
#display_timer.init(freq=1000, mode=Timer.PERIODIC, callback=scan_display)
print("Ready! Press button to switch modes (0=Temp, 1=Volt, 2=Light).")
try:
while True:
scan_display()
except Exception as e:
print("Error:", e)