In this experiment, we learn how to read analog values from an LDR (Light Dependent Resistor) using the ADC (Analog-to-Digital Converter) of the Raspberry Pi Pico. We use this input to automatically turn an LED on or off based on light intensity, demonstrating basic light sensing and control using Micro Python.
Buy Basic Raspberry Pi Pico Kit
Item Required:
- Raspberry Pi Pico
- LDR (Light Dependent Resistor)
- 10kΩ resistor (for voltage divider with LDR)
- Jumper wires
- Breadboard
- USB cable Micro B
Connections:


Code:
from machine import Pin, ADC
import time
ldr = ADC(Pin(27))
led = Pin("LED", Pin.OUT)
# Set threshold for "darkness" (adjust based on environment)
DARK_THRESHOLD = 15000 # 0 to 65535 scale
while True:
samples = []
for _ in range(10):
samples.append(ldr.read_u16())
time.sleep_ms(10)
average = sum(samples) // len(samples)
print("Average LDR Value:", average)
if average > DARK_THRESHOLD:
led.off()
else:
led.on()
time.sleep(0.05) # Short delay (50 ms)
Result:
The Pico continuously reads light levels through the LDR. If the environment is bright, the LED remains ON. When it gets dark (light level drops below the threshold), the LED turns OFF. The average of 10 readings smooths out fluctuations for more stable LED control.