from machine import Pin, ADC
import time
# Define the pins for the LED Bar Graph
led_pins = [15, 2, 4, 5, 18, 19, 21, 22, 23, 13]
# Initialize the pins as outputs
leds = [Pin(pin, Pin.OUT) for pin in led_pins]
# Define the analog pin for the potentiometer
analog_pin = ADC(0)
while True:
# Read the potentiometer value
sensor_reading = analog_pin.read_u16()
# Map the value to a range from 0 to the number of LEDs
led_level = sensor_reading // (65535 // len(led_pins))
# Loop over the LED array
for i, led in enumerate(leds):
if i < led_level:
led.value(1) # Turn the LED on
else:
led.value(0) # Turn the LED off
time.sleep(0.01) # Wait a bit before updating the LEDs again