DEV Community

Hedy
Hedy

Posted on

How do light sensors like LDR or TSL2561 work?

Light sensors like LDRs (Light Dependent Resistors) and TSL2561 work by converting light intensity into measurable electrical signals, but they use different principles and technologies. Here’s how they function:

1. LDR (Light Dependent Resistor)
Working Principle:

  • Photoconductivity: An LDR is made of a high-resistance semiconductor (e.g., cadmium sulfide, CdS).
  • When light strikes the material, photons excite electrons, reducing resistance.
  • Dark: High resistance (~MΩ).
  • Bright: Low resistance (~few kΩ).

Key Characteristics:

  • Slow response time (tens to hundreds of milliseconds).
  • Non-linear response (logarithmic resistance vs. light intensity).
  • Sensitive to visible light (peak sensitivity ~550nm, similar to human eyes).

Typical Circuit (Voltage Divider):

text
Vcc → LDR → Resistor → Ground  
Output: Voltage at junction (varies with light).  
Enter fullscreen mode Exit fullscreen mode
  • Pros: Simple, cheap, no power needed (passive).
  • Cons: Low precision, affected by temperature.

Applications:

  • Automatic night lights.
  • Camera exposure control (old-school).
  • Basic light/dark detection.

2. TSL2561 (Digital Light Sensor)
Working Principle:

  • Photodiodes + ADC: Uses two photodiodes (visible + IR) and an integrating ADC.
  • Dual-Diode Design:

    • Channel 0 (Visible + IR): Broadband sensitivity.
    • Channel 1 (IR only): Isolates IR for accurate lux calculation.
  • I²C Interface: Outputs digital lux values (no analog circuitry needed).

Key Characteristics:

  • High precision (0.1–40,000 lux range).
  • Fast response (~100ms).
  • IR rejection (avoid errors from non-visible light).
  • Programmable gain/integration time (adapts to dynamic ranges).

Typical Circuit:

text
Vcc → TSL2561 (I²C SDA/SCL) → Microcontroller
Enter fullscreen mode Exit fullscreen mode
  • Pros: Accurate, digital output, ambient light compensation.
  • Cons: Requires power, more complex than LDR.

Applications:

  • Smartphone/tablet auto-brightness.
  • Industrial light monitoring.
  • IoT environmental sensors.

3. Key Differences

4. When to Use Which?
Use an LDR if:

  • You need a simple, low-cost solution.
  • Speed/precision don’t matter (e.g., dark/light threshold detection).

Use a TSL2561 if:

  • You need accurate lux measurements.
  • Your project requires digital output (e.g., Arduino/RPi).
  • IR interference must be minimized.

5. Example Code (Arduino)
LDR Readout:

cpp
int ldrPin = A0;  
void setup() { Serial.begin(9600); }  
void loop() {  
  int ldrValue = analogRead(ldrPin);  
  Serial.println(ldrValue);  
  delay(500);  
}
Enter fullscreen mode Exit fullscreen mode

TSL2561 Readout (Using Library):

cpp
#include <Adafruit_TSL2561.h>  
Adafruit_TSL2561 tsl = Adafruit_TSL2561();  

void setup() {  
  tsl.begin();  
  tsl.enableAutoRange(true);  
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  
}  

void loop() {  
  uint16_t lux = tsl.getLuminosity(TSL2561_VISIBLE);  
  Serial.print("Lux: "); Serial.println(lux);  
} 
Enter fullscreen mode Exit fullscreen mode

6. Advanced Notes

  • LDR Drawbacks: Aging effects, hysteresis, poor low-light sensitivity.
  • TSL2561 Alternatives:

Top comments (0)