# imports
from machine import Pin, ADC, Timer
import math
import time
#######################################
# Pin and constant definitions
#######################################
SEVEN_SEGMENT_START_PIN = 0
ANALOGUE_INPUT_PIN = 26
DISPLAY_COUNT = 4
MEASUREMENT_COUNT = 16
DECIMAL_PRECISION = 3
ADC_RANGE = float((math.pow(2, 16) - 1))
button_1 = Pin(16, Pin.IN, Pin.PULL_UP)
# HEX values for 7 segment display values
digit_list_hex = [
0x40, # 0
0x79, # 1
0x24, # 2
0x30, # 3
0x19, # 4
0x12, # 5
0x02, # 6
0x78, # 7
0x00, # 8
0x10, # 9
0x08, # A
0x03, # B
0x46, # C
0x21, # D
0x06, # E
0x0E, # F
0x7F # Empty
]
#######################################
# Global variables
#######################################
display_value = 0
segment_pins = []
display_select_pins = []
analogue_voltage_pin = None
current_display_index = DISPLAY_COUNT - 1
display_timer = None
prev_analogue_voltage = -1
#######################################
# Function definitions
#######################################
# Function to read the ADC pin and convert the digital value to a voltage level
def read_analogue_voltage(pin):
global display_value, prev_analogue_voltage, DECIMAL_PRECISION
total_value = 0
for _ in range(MEASUREMENT_COUNT):
total_value += pin.read_u16()
average_value = total_value // MEASUREMENT_COUNT
analogue_voltage = round((average_value / ADC_RANGE) * 3.3, DECIMAL_PRECISION)
new_display_value = int(analogue_voltage * math.pow(10, DECIMAL_PRECISION))
if display_value != new_display_value:
print(f'voltage: {analogue_voltage} mV')
display_value = new_display_value
disable_display_timer()
current_display_index = DISPLAY_COUNT -1
display_digit(16, -1)
time.sleep(0.1)
enable_display_timer()
# Function to disable timer that triggers scanning 7 segment displays
def disable_display_timer():
global display_timer
display_timer.deinit()
# Function to enable timer that triggers scanning 7 segment displays
def enable_display_timer():
global display_timer
display_timer.init(period=4, mode=Timer.PERIODIC, callback=scan_display)
# Function to handle scanning 7 segment displays
def scan_display(timer):
global current_display_index, display_value
digit = int((abs(display_value) // math.pow(10, current_display_index))) % 10
display_digit(digit, current_display_index,
current_display_index == DECIMAL_PRECISION and 0 != DECIMAL_PRECISION)
current_display_index = (current_display_index - 1)
if current_display_index < 0:
current_display_index = DISPLAY_COUNT - 1
# Function to display the given value on the display with the specified index
def display_digit(digit_value, digit_index, dp_enable=False):
if digit_value < 0 or digit_value > len(digit_list_hex):
return
for pin in display_select_pins:
pin.value(0)
mask = digit_list_hex[digit_value]
for i in range(7):
segment_pins[i].value((mask >> i) & 1)
segment_pins[7].value(1 if dp_enable == False else 0)
if digit_index == -1:
for pin in display_select_pins:
pin.value(1)
elif 0 <= digit_index < DISPLAY_COUNT:
display_select_pins[digit_index].value(1)
# Function to setup GPIO/ADC pins, timers and interrupts
def setup():
global segment_pins, display_select_pins, analogue_voltage_pin, display_timer
for i in range(SEVEN_SEGMENT_START_PIN + 8, SEVEN_SEGMENT_START_PIN + 8 + DISPLAY_COUNT):
pin = Pin(i, Pin.OUT)
pin.value(0)
display_select_pins.append(pin)
for i in range(SEVEN_SEGMENT_START_PIN, SEVEN_SEGMENT_START_PIN + 8):
pin = Pin(i, Pin.OUT)
pin.value(1)
segment_pins.append(pin)
analogue_voltage_pin = ADC(ANALOGUE_INPUT_PIN)
display_timer = Timer()
enable_display_timer()
button_pressed = False
# Interrupt handler for the button press
def int_handler(pin):
global button_pressed
button_pressed = True
button_1.irq(trigger=Pin.IRQ_FALLING, handler=int_handler)
if __name__ == '__main__':
setup()
while True:
if button_pressed:
read_analogue_voltage(analogue_voltage_pin)
button_pressed = False
time.sleep(0.1)