from machine import Pin, ADC, PWM
import time
# Set up potentiometer
potentiometer_pin = 34
# Potentiometer is connected to GPIO 34 (ADC1)
potentiometer = ADC(Pin(potentiometer_pin))
potentiometer.width(10) # Setting ADC Resolution to 10-bits (0-1023)
# Set up buzzer
buzzer_pin = Pin(15) # Buzzer is connected to GPIO 15
buzzer = PWM(buzzer_pin)
# Set buzzer PWM frequency and duty cycle
buzzer.freq(1000) # Initial frequency of 1000 Hz
buzzer.duty(0) # Start with the buzzer off
while True:
# Read potentiometer value (0-1023)
pot_value = potentiometer.read()
# Map potentiometer value to a Volume Precentage range (e.g., 0 - 100)
volume = (pot_value * (100)) // 1023
# Set duty cycle to a moderate value (e.g., 50%)
buzzer.duty(pot_value)
# Debug: Print values
print("Potentiometer Value:", pot_value, "Buzzer Volume:", volume)
# Delay for stability
time.sleep(0.1)