import machine
import time
#######################################
# Pin and constant definitions
#######################################
ANALOG_PIN = 26 # ADC pin for analogue components
BUTTON_PIN = 16 # Pushbutton pin
DISPLAY_PINS = [0, 1, 2, 3, 4, 5, 6] # 7-segment display segments (a to g)
DP_PIN = 7 # Decimal point pin
DIGIT_PINS = [8, 9, 10, 11] # Display digit select lines (DIG1 to DIG4)
# Seven-segment display digits (0-9), in binary for common cathode display
DIGIT_VALUES = [
0b00111111, # 0
0b00000110, # 1
0b01011011, # 2
0b01001111, # 3
0b01100110, # 4
0b01101101, # 5
0b01111101, # 6
0b00000111, # 7
0b01111111, # 8
0b01101111 # 9
]
#######################################
# Global variables
#######################################
last_button_press_time = 0
current_voltage_mv = 0
#######################################
# Function definitions
#######################################
# Function to read the ADC pin and
# convert the digital value to a voltage level in the 0-3.3V range
def read_analogue_voltage(pin):
global current_voltage_mv
sum_values = 0
num_samples = 16 # Number of samples to average
for _ in range(num_samples):
sum_values += pin.read_u16() # Read ADC value as a 16-bit unsigned integer
average_value = (sum_values / num_samples) * 3.3 / 65535 # Convert ADC reading to voltage
voltage_mv = int(average_value * 1000) # Convert to millivolts and then to integer
current_voltage_mv = voltage_mv # Update global voltage value
# Print voltage to console
print("Voltage: {} mV".format(voltage_mv))
# Function to display the voltage on the seven-segment display
def display_voltage(voltage_mv):
if voltage_mv > 9999:
voltage_mv = 9999
elif voltage_mv < 0:
voltage_mv = 0
# Convert voltage to individual digits
digit4 = voltage_mv % 10
voltage_mv //= 10
digit3 = voltage_mv % 10
voltage_mv //= 10
digit2 = voltage_mv % 10
voltage_mv //= 10
digit1 = voltage_mv % 10
# Display digit1 on seven-segment display with DP on
display_digit(digit1, 0)
machine.Pin(DP_PIN, machine.Pin.OUT).value(1) # Set DP pin to HIGH for digit1
time.sleep(0.005)
# Turn off DP for all other digits
machine.Pin(DP_PIN, machine.Pin.OUT).value(0) # Set DP pin to LOW for remaining digits
# Display remaining digits
display_digit(digit2, 1)
time.sleep(0.005)
display_digit(digit3, 2)
time.sleep(0.005)
display_digit(digit4, 3)
time.sleep(0.005)
# Function to display a digit on the common cathode seven-segment display
def display_digit(digit_value, digit_index):
for pin in DIGIT_PINS:
machine.Pin(pin, machine.Pin.OUT).value(1)
# Set segments for the digit value
segments = DIGIT_VALUES[digit_value]
for i, seg_pin in enumerate(DISPLAY_PINS):
machine.Pin(seg_pin, machine.Pin.OUT).value(segments >> i & 1)
machine.Pin(DIGIT_PINS[digit_index], machine.Pin.OUT).value(0)
# Function to setup GPIO/ADC pins and polling loop
def setup():
global last_button_press_time
last_button_press_time = 0
# Initialize ADC pin and other necessary pins
adc = machine.ADC(machine.Pin(ANALOG_PIN))
button = machine.Pin(BUTTON_PIN, machine.Pin.IN, machine.Pin.PULL_UP)
# Initialize DP pin
machine.Pin(DP_PIN, machine.Pin.OUT).value(0)
while True:
if button.value() == 0:
current_time = time.ticks_ms()
if current_time - last_button_press_time > 200: # 200ms debounce time
read_analogue_voltage(adc)
last_button_press_time = current_time
# Continuously refresh the display with the current voltage
display_voltage(current_voltage_mv)
time.sleep_ms(10)
if __name__ == '__main__':
setup()
#This project is designed to create a system using a Raspberry Pi Pico microcontroller
#to measure and display voltage on a 4-digit seven-segment display.
#It includes functionalities to read analog voltage inputs, debounce a pushbutton for control,
#and dynamically update the display based on user interactions.