from machine import Pin, PWM, ADC
import time
led_pin = 2 # GPIO for LED
sensor_pin = 34 # GPIO for light sensor (ADC)
led = PWM(Pin(led_pin), freq=1000) # Initialize PWM for LED with 1kHz frequency
sensor = ADC(Pin(sensor_pin)) # Initialize ADC for light sensor
def read_light_level():
raw_value = sensor.read()
light_level = (raw_value / 4095) * 100 # Convert ADC value to percentage
return light_level
def adjust_led_brightness():
light_level = read_light_level()
duty_cycle = int((100 - light_level) * 1023 / 100) # Convert light level to PWM duty cycle
led.duty(duty_cycle) # Adjust LED brightness based on light level
while True:
adjust_led_brightness()
time.sleep(0.1) # Small delay for smooth adjustments