# reference: https://wokwi.com/projects/377926870010444801
# https://wokwi.com/projects/370257513012246529
from machine import ADC, Pin
import math
from time import sleep
GAMMA = 0.7;
RL10 = 85;
LDR = ADC(Pin(26))
led_pin = Pin(18, Pin.OUT) # Assuming GPIO Pin 18 is connected to the LED via replay
while True:
# Convert the analog value into lux value:
# For Raspberry Pi Pico in MicroPython, this can range from 0 to 65025.
analogValue = LDR.read_u16()
voltage = analogValue / 65025 * 3.3
resistance = 5000 * voltage / (1 - voltage / 3.3)
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA))
print("lux = ", lux)
if lux < 100:
led_pin.off() # Turn off LED
else:
led_pin.on() # Turn on LED
sleep(2)