Light
Interfacing LDR (Photoresistor) Light with Raspberry Pi
What is the LDR (Photoresistor)?
LDR (Photoresistor) practical reference
The LDR (Light Dependent Resistor), also called a photoresistor or photocell, is a passive component whose resistance drops sharply as light increases — typically from over 1MΩ in darkness down to a few hundred ohms in bright light. It has no polarity and no active circuitry, so it cannot be read directly by a microcontroller: you build a voltage divider with a fixed resistor (commonly 10kΩ) so the changing resistance becomes a changing voltage an ADC pin can measure.
It is the go-to sensor for automatic night lights, streetlight controllers, light-following robots, camera exposure triggers, and simple ambient-light dimmers. The catch that trips up beginners is response time: cheap CdS-based LDRs are slow (tens of milliseconds to react) and non-linear, so they suit on/off or slow-trend lighting decisions but not fast or precise lux measurements — for that, use a digital light sensor like BH1750 instead.
Key specifications
- Type: Cadmium Sulfide (CdS) photoresistor
- Package size: 5mm (also 4mm, 7mm, 12mm)
- Dark resistance: ~1MΩ (typical, 0 lux)
- Light resistance: ~200Ω–1kΩ (typical, 10 lux)
- Spectral peak response: ~540nm (green, close to human eye)
- Response time: ~20–30ms rise, ~30ms+ fall (slow, non-linear)
- Max power rating: 100mW (typical 5mm package)
- Max voltage: 150V DC (passive, not logic-level restricted)
- Interface: Analog only — needs a voltage divider and ADC pin, no library
Pinout and connection notes
Two-leg passive component, no polarity. Standard circuit: LDR from VCC to a sense node, fixed 10kΩ resistor from that sense node to GND, sense node to your ADC pin. Swap the LDR and resistor positions to invert whether brighter light reads higher or lower.
- Leg A: No polarity — connect to VCC or the divider midpoint, either leg works
- Leg B: No polarity — connect to the fixed resistor / ground side of the divider
Integration checklist
- Confirm the exact part number and breakout-board revision.
- Verify supply voltage, current demand, logic levels, and signal direction against the datasheet.
- Check controller pin assignments and bus address or interface mode against the example code.
- Power up with external loads disconnected, then test signals before full integration.
- If readings or communication fail, check power and ground first, then pin mapping, interface settings, and library initialization.
Troubleshooting and reproducibility
Record the board used, wiring, library/version, supply arrangement, and measured behavior. Use the selected board-specific wiring example as the source of truth. Never assume visually similar modules share identical voltage tolerance or pin order.
Setting Up LDR (Photoresistor) with Raspberry Pi
Library
spidev by doceme/py-spidev
pip install spidev
Wiring
| Component pin | Raspberry Pi pin | Note |
|---|---|---|
| Leg A | 3.3V | |
| Leg B | MCP3008 CH0 + 10kΩ to GND | |
| MCP3008 VDD/VREF | 3.3V | |
| MCP3008 CLK | GPIO11 | |
| MCP3008 DOUT | GPIO9 | |
| MCP3008 DIN | GPIO10 | |
| MCP3008 CS | GPIO8 |
Swipe sideways to see every column →
Raspberry Pi has no analog input pins, so an MCP3008 ADC (~$3) is required — worth calling out clearly so people don't wire it straight to a GPIO expecting it to work.
Related Components
A3144
Digital Hall-effect switch for contactless magnetic detection
BH1750
I2C lux sensor that outputs calibrated light readings — no ADC or calibration required.
Flame Sensor
Infrared flame detector for early fire-alert projects.
TSL2561
Dual-photodiode lux sensor with good accuracy across mixed lighting conditions.
Related Guides & Projects
Raspberry Pi Pico W and Pico 2 W: Install MicroPython with Thonny or UF2
Hold BOOTSEL, drag the correct UF2 file, open the REPL and blink the onboard LED. Includes the Pico vs Pico W vs Pico 2 W firmware differences and what to do when the drive does not appear.
Raspberry Pi Pico W Web Server in MicroPython: Control an LED and Read a Sensor
Connect the Pico W to WiFi, serve a responsive control page, switch an LED and display the onboard temperature or an external sensor, with timeouts and reconnect handling.
Raspberry Pi Pico Oscilloscope with MicroPython
Convert a Raspberry Pi Pico into a basic 500 kSPS oscilloscope using its 12-bit ADC and MicroPython. Displays waveforms on an SSD1306 OLED or streams samples over USB serial to a Python plotter on your PC.
Self-Hosted Nextcloud NAS on Raspberry Pi 4
Build a private, durable Nextcloud NAS on a Raspberry Pi 4 -- SSD boot instead of a failure-prone SD card, Docker Compose with Redis caching, Tailscale for remote access with zero ports forwarded, and an automated backup strategy that actually runs.