from machine import Pin, ADC
import math
# These constants should match the photoresistor's "gamma" and "rl10" attributes
GAMMA = 0.7
RL10 = 50
# Initialize the ADC on pin 28
ldr = ADC(28)
# Read the 16-bit analog value
analog_value = ldr.read_u16()
# Convert the analog value into lux value:
def calculate_lux():
global analog_value # Corrected to take analog_value as an argument
max_analog_value = 65535.0 # 2^16 -1
voltage = analog_value / max_analog_value * 5.0
resistance = 2000.0 * voltage / (1.0 - voltage / 5.0)
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1.0 / GAMMA))
print (voltage)
return lux
# Calculate and print the lux value
lux_value = calculate_lux() # Pass the analog_value to the function
print(lux_value)