from machine import ADC, Pin
import time
pot = ADC(Pin(28))
ntc = ADC(Pin(28))
while True:
value = pot.read_u16()
voltage = value * 3.3 / 65535
print("voltage: {:.2f} V" .format(voltage))
time.sleep(0.5)
# ========================================================
# # imports
# import machine
# import time
# import math
# #######################################
# # Pin and constant definitions
# #######################################
# ANALOG_PIN = 28 # ADC pin for analogue components
# BUTTON_PIN = 5 # Pushbutton pin
# DISPLAY_PINS = [10, 11, 12, 13, 14, 15, 16] # 7-segment display segments (a to g)
# DP_PIN = 7 # Decimal point pin
# DIGIT_PINS = [17, 18, 19, 8] # 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
# #######################################
# display_value = 0
# 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
# # This function updates the value of the display_value global variable
# 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 disable timer that triggers scanning 7 segment displays
# def disable_display_timer():
# pass
# # Function to enable timer that triggers scanning 7 segment displays
# def enable_display_timer():
# pass
# # Function to handle scanning 7 segment displays
# # Display the value stored in the display_value global variable on available 7-segment displays
# def scan_display(timer_int):
# display_voltage(current_voltage_mv)
# # Function to display the given value on the display with the specified index
# def display_digit(digit_value, digit_index, dp_enable=False):
# 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 test available 7-segment displays
# def display_value_test():
# pass
# # Function to setup GPIO/ADC pins, timers and interrupts
# 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()