#Q.Write a micropython script to control the brightness of led connceted to gpio 4 with respect to the indensity of sun light falling on the LDR connected to gpio 34(if the indensity of the sunlight is more the brightness of lde is less)
from machine import Pin, ADC, PWM
from time import sleep
led_pin = Pin(4, Pin.OUT)
ldr_pin = Pin(34, Pin.IN)
ldr = ADC(ldr_pin)
led_pwm = PWM(led_pin)
led_pwm.freq(1000)
def map_ldr_to_brightness(ldr_value):
return int((ldr_value / 4095) * 1023)
while True:
ldr_value = ldr.read()
brightness = map_ldr_to_brightness(ldr_value)
led_pwm.duty(brightness)
sleep(0.1)