Turn on LED in Low Light with Pico
In this exercise, we'll control an LED based on ambient light levels using an LDR. The goal is to automatically turn on the LED in low light conditions - creating an automatic night light.
Hardware Requirements
- LED – Any standard LED (choose your preferred color)
- LDR (Light Dependent Resistor) – Used to detect light intensity
- Resistors:
- 330Ω – For the LED to limit current
- 10kΩ – For the LDR voltage divider circuit
- Jumper Wires – For connections
Circuit Connection
LDR Circuit:
- One side of the LDR connects to AGND (Analog Ground)
- The other side connects to GPIO26 (ADC0)
- A 10kΩ resistor connects between GPIO26 and ADC_VREF (3.3V)
LED Circuit:
- LED anode → GPIO15 (or any free GPIO)
- LED cathode → 330Ω resistor → GND
Pico 2 W Note: GPIO 23, 24, 25, and 29 are reserved for wireless. Don't use GPIO25 for the LED like on standard Pico 2.
Code Implementation
use embassy_rp::adc::{Adc, Channel};
use embassy_rp::gpio::{Level, Output, Pull};
use embassy_time::Timer;
const LIGHT_THRESHOLD: u16 = 3800;
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default());
// Initialize ADC for LDR
let mut adc = Adc::new(p.ADC, Irqs, Config::default());
let mut p26 = Channel::new_pin(p.PIN_26, Pull::None);
// Initialize LED
let mut led = Output::new(p.PIN_15, Level::Low);
loop {
// Read light level
let level = adc.read(&mut p26).await.unwrap();
// Turn on LED in darkness
if level > LIGHT_THRESHOLD {
led.set_high();
} else {
led.set_low();
}
Timer::after_secs(1).await;
}
}How It Works
The code reads the ADC value from the LDR every second. When the ADC value exceeds the threshold (indicating low light), the LED turns on. In bright conditions, the LED stays off. You may need to adjust the LIGHT_THRESHOLD value based on your room lighting and specific LDR.
Testing
Try this in a room by turning lights on and off. Cover the LDR with your hand to simulate darkness - the LED should turn on. Uncover it, and the LED should turn off. Adjust the threshold value if needed for better sensitivity.