"""Write a micropython script to interface an LDR with GPIO 14 of esp32 to do the following
1.) Read the data from LDR and print on shell window
2.) Calculate the amount of voltage provided by LDR w.r.t change of light intensity
3.) Control the led connected to GPIO 2 of ESp32 (Sunlight Led OFF or VIce versa)
4.) Control the brightness of LED w.r.t intensity of Sunlight"""
from machine import Pin, ADC, PWM
from time import sleep
ldr = ADC(Pin(14))
ldr.width(ADC.WIDTH_10BIT)
ldr.atten(ADC.ATTN_11DB)
led=Pin(12,Pin.OUT)
led_pwm = PWM(Pin(2),5000)
while True:
ldr_value = ldr.read()
print("LDR value:", ldr_value)
sleep(0.5)
voltage = ldr_value * (3.3 / 1023)
print("LDR voltage:", voltage)
sleep(0.5)
led_pwm.duty(1015-ldr_value)
sleep(0.5)