Light level detection with LED indicator using LDR

HARDWARE REQUIRED:
- PICUNO Microcontroller board
- 1 × LED
- 1 × 220Ω resistor
- 1 × 10kΩ resistor
- 1 × LDR (Light Dependent Resistor)
- Breadboard
- Jumper wires
- USB cable
DESCRIPTION:
The LDR and 10kΩ resistor are connected in a voltage divider configuration. As ambient light increases, the resistance of the LDR decreases, which changes the voltage at the junction between the LDR and resistor. This voltage is read by the Analog pin on the PicUNO. The microcontroller compares the light level to a preset threshold and lights up an LED when the environment is too dark.
CIRCUIT DIAGRAM:
[Fritzing image to be added here]
- Connect the PICUNO board to the computer using a USB cable.
- Connect the Anode of the LED to one terminal of a 220Ω resistor.
- Connect the other terminal of the resistor to GPIO pin 6.
- Connect the Cathode of the LED to GND.
- Connect one end of the LDR to 3.3 V
- Connect the other end of the LDR to Analog Pin A0 through one end of 10kΩ resistor.
- Connect the other end of the 10kΩ resistor to GND.
SCHEMATIC:
One end of the LDR → VCC (3.3V)
Other end of the LDR → Analog pin A0
A 10kΩ resistor from A0 → GND
LED anode → 220-ohm resistor → GPIO 6
LED cathode → GND
CODE -- C:
int ldrPin = A0;
int ledPin = 6;
int threshold = 400; // Adjust based on ambient light
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int ldrValue = analogRead(ldrPin);
Serial.print(\"LDR Value: \");
Serial.println(ldrValue);
if (ldrValue < threshold) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(200);
}
int ledPin = 6;
int threshold = 400; // Adjust based on ambient light
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int ldrValue = analogRead(ldrPin);
Serial.print(\"LDR Value: \");
Serial.println(ldrValue);
if (ldrValue < threshold) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(200);
}
analogRead(ldrPin) - Reads light level (0--1023).
threshold - Determines the trigger point for darkness.
digitalWrite(ledPin, HIGH) - Turns ON LED if dark.
Serial.println(...) - Monitors LDR value in real time.
threshold - Determines the trigger point for darkness.
digitalWrite(ledPin, HIGH) - Turns ON LED if dark.
Serial.println(...) - Monitors LDR value in real time.
CODE -- PYTHON:
from machine import ADC, Pin
import time
ldr = ADC(0) # A0 pin
led = Pin(6, Pin.OUT)
while True:
light = ldr.read_u16()
print(\"LDR Value:\", light)
if light < 30000:
led.value(1)
else:
led.value(0)
time.sleep(0.2)
import time
ldr = ADC(0) # A0 pin
led = Pin(6, Pin.OUT)
while True:
light = ldr.read_u16()
print(\"LDR Value:\", light)
if light < 30000:
led.value(1)
else:
led.value(0)
time.sleep(0.2)
ADC(0) - Sets A0 as analog input.
read_u16() - Returns light value (0--65535).
light < 30000 - Adjust this based on actual light conditions.
led.value(1) - LED ON when it's dark.
read_u16() - Returns light value (0--65535).
light < 30000 - Adjust this based on actual light conditions.
led.value(1) - LED ON when it's dark.