from machine import Pin, PWM, ADC
import time
potentiometer_pin = 34 # Example GPIO pin for potentiometer, adjust as per your wiring
led_pin = 5 # Example GPIO pin for LED, adjust as per your wiring
# Initialize PWM on the LED pin
led_pwm = PWM(Pin(led_pin), freq=1000)
# Initialize ADC on the potentiometer pin
potentiometer = ADC(Pin(potentiometer_pin))
def set_led_brightness(brightness):
# Set LED brightness using PWM duty cycle (0 to 1023)
led_pwm.duty(brightness)
def read_potentiometer_value():
# Read analog value from potentiometer (0 to 4095)
return potentiometer.read()
def main():
while True:
# Read the potentiometer value
pot_value = read_potentiometer_value()
# Map the potentiometer value (0 to 4095) to LED brightness (0 to 1023)
brightness = int((pot_value / 4095) * 1023)
# Set LED brightness
set_led_brightness(brightness)
# Print potentiometer value and LED brightness for reference
print("Potentiometer Value:", pot_value, "LED Brightness:", brightness)
time.sleep(0.1) # Adjust the delay as needed
if __name__ == "__main__":
main()