from machine import Pin, ADC, PWM
from utime import sleep
print("Hello, ESP32!")
# Define the LED pin
led_pin = Pin(2, Pin.OUT)
# Define the LDR pin
ldr_pin = Pin(34)
ldr = ADC(ldr_pin)
# Configure ADC parameters
ldr.width(ADC.WIDTH_10BIT)
ldr.atten(ADC.ATTN_11DB)
# Define the PWM pin
pwm_pin = Pin(4) # Use a different pin for PWM
# Initialize PWM
led_pwm = PWM(pwm_pin, 5000)
while True:
ldr_value = ldr.read()
print("LDR Value:", ldr_value)
# Scale the LDR value to fit within the duty cycle range (0-1023)
duty_cycle = int(ldr_value * (1023 / 1024))
print("Duty Cycle:", duty_cycle)
# Set the duty cycle for PWM
led_pwm.duty(duty_cycle)
sleep(0.1)