LDR Sensor Module Documentation
The LDR (Light Dependent Resistor) Sensor Module is a photosensitive device that measures the intensity of light in its surroundings. The module converts the light intensity into an analog voltage signal, allowing microcontrollers to read and process the data. This module is widely used in various IoT applications, including ambient light sensing, object detection, and automatized lighting systems.
The LDR Sensor Module typically consists of three pins:
VCC: Power supply pin (typically 3.3V or 5V)
GND: Ground pin
OUT: Analog output pin
Example 1: Basic Light Intensity Measurement with Arduino
In this example, we will connect the LDR Sensor Module to an Arduino board to measure the ambient light intensity.
Connect the VCC pin of the LDR Sensor Module to the 5V pin on the Arduino board
Connect the GND pin of the LDR Sensor Module to the GND pin on the Arduino board
Connect the OUT pin of the LDR Sensor Module to any analog input pin (e.g., A0) on the Arduino board
Code:
```c
const int ldrPin = A0; // Analog input pin for LDR sensor
int lightIntensity = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
lightIntensity = analogRead(ldrPin);
Serial.print("Light Intensity: ");
Serial.println(lightIntensity);
delay(1000);
}
```
Explanation:
In this example, we read the analog voltage signal from the LDR Sensor Module using the `analogRead()` function and store it in the `lightIntensity` variable. The value ranges from 0 (complete darkness) to 1023 (maximum brightness). We then print the light intensity value to the serial monitor using `Serial.println()`.
Example 2: Automatic LED Control with Raspberry Pi
In this example, we will connect the LDR Sensor Module to a Raspberry Pi to control an LED based on the ambient light intensity.
Connect the VCC pin of the LDR Sensor Module to the 3.3V pin on the Raspberry Pi
Connect the GND pin of the LDR Sensor Module to the GND pin on the Raspberry Pi
Connect the OUT pin of the LDR Sensor Module to any analog input pin (e.g., GPIO 17) on the Raspberry Pi
Connect an LED to a digital output pin (e.g., GPIO 18) on the Raspberry Pi
Code:
```python
import RPi.GPIO as GPIO
import time
# Set up GPIO mode
GPIO.setmode(GPIO.BCM)
# Define LDR sensor pin and LED pin
ldrPin = 17
ledPin = 18
# Set up LDR sensor pin as input
GPIO.setup(ldrPin, GPIO.IN)
# Set up LED pin as output
GPIO.setup(ledPin, GPIO.OUT)
while True:
# Read LDR sensor value
ldrValue = GPIO.input(ldrPin)
# If light intensity is low, turn on the LED
if ldrValue < 500:
GPIO.output(ledPin, GPIO.HIGH)
else:
GPIO.output(ledPin, GPIO.LOW)
# Wait for 1 second before taking the next reading
time.sleep(1)
```
Explanation:
In this example, we use the `RPi.GPIO` library to interact with the Raspberry Pi's GPIO pins. We read the LDR sensor value using `GPIO.input()` and store it in the `ldrValue` variable. If the light intensity is low (i.e., `ldrValue` is less than 500), we turn on the LED by setting the `ledPin` high using `GPIO.output()`. Otherwise, we turn off the LED. The `time.sleep(1)` function is used to introduce a delay between readings.