LDR Photoresistor Ambient Light Sensor & Auto Lamp
Sense ambient brightness using a cadmium-sulfide photoresistor voltage divider and hysteresis auto-nightlight logic.
Teach your Arduino to see daylight and darkness using an LDR (Light Dependent Resistor). Wire a 10kΩ voltage divider (or 4-pin LDR module) into analog channel A0, read 10-bit ADC luminosity values (0–1023), and build a flicker-free automatic night lamp with dual-threshold hysteresis.
Key Learning Objectives & Robotics Concepts
By building and programming this project, you will master the following core engineering skills:
Power Architecture & Voltage Staging
5V / 3.3V Regulated PowerThe LDR sensor module draws <5mA from the Arduino Uno 5V pin. Both the analog divider output (AO) and LM393 comparator output (DO) stay cleanly within 0–5V logic bounds. 10kΩ voltage divider keeps sensor current below 0.5mA while maintaining full 0–5V dynamic range on A0.
Bill of Materials (BOM) & Hardware Components
| Component | Specifications & Alternative | Qty | Approx Cost |
|---|---|---|---|
| Arduino Uno R3 | ATmega328P with 10-bit ADC | 1 | $8.00 |
| LDR Light Sensor Module (or Bare LDR + 10kΩ) | CdS photoresistor + LM393 comparator | 1 | $1.20 |
| 5mm White/Yellow LED + 220Ω Resistor | Automatic night-lamp load | 1 | $0.15 |
| Breadboard & Jumper Wires | 6× breadboard jumpers | 1 | $2.00 |
Electrical & System Architecture
Connect LDR module VCC to 5V, GND to GND, AO to A0, and DO to D2 (for standalone bare LDR, pair with a 10kΩ resistor to GND and tap midpoint to A0). Wire auto-lamp LED (+220Ω) to D9.
Autonomous Behavior & Finite State Machine (FSM)
Deterministic Arduino loop handling LDR Photoresistor Module (AO/DO + LM393) acquisition, filtering, and real-time output updates.
Step-by-Step Physical Assembly & Wiring Tutorial
Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.
Inspect LDR Module Pinout & Trimpot
Locate VCC, GND, DO (Digital Out), and AO (Analog Out) on the LDR breakout board.
- Verify whether your LDR module increases or decreases AO voltage in bright light (most modules pull AO LOW in bright light).
- Locate the blue potentiometer that adjusts the hardware DO comparator threshold.
- Position the photoresistor face upward away from LED glare.
Wire LDR to A0/D2 & Night-Lamp LED to D9
Connect power, analog/digital outputs, and the LED indicator.
- Connect LDR VCC → 5V and GND → GND.
- Connect LDR AO → A0 and DO → D2.
- Wire D9 → 220Ω resistor → LED anode → cathode to GND.
Record Bright & Dark Baseline Values
Run the ADC diagnostic to find your room's exact bright vs. covered readings.
- Upload the diagnostic snippet and open Serial Monitor.
- Record the raw A0 value under normal desk lighting.
- Cup your hand over the LDR and record the darkened A0 reading.
Upload Hysteresis Auto-Nightlight Firmware
Flash the smoothed light monitor with separate ON/OFF hysteresis bounds.
- Adjust INVERT_SENSOR if your module outputs higher ADC in darkness (true for standard 4-pin LM393 modules).
- Upload the production sketch below.
- Cover the LDR slowly with your hand and verify the LED switches ON cleanly without flickering.
Calibrate Onboard Comparator Trimpot
Turn the blue potentiometer gently until the module's onboard DO LED toggles at dusk.
- Shade the LDR slightly and rotate the trimpot with a small screwdriver until the DO LED just lights.
- Uncover the sensor and confirm the DO LED turns off promptly.
- Compare digital D2 state against analog A0 percentage in Serial Monitor.
Subsystem Benchtop Verification Scripts
Test each hardware subsystem independently before deploying the full autonomous firmware.
1Bench Diagnostic: LDR Photoresistor Module (AO/DO + LM393) Hardware Check
Subsystem DiagnosticMinimal standalone sketch to verify pin wiring and signal response for LDR Photoresistor Module (AO/DO + LM393) before running the full project firmware.
// Diagnostic: Raw LDR Analog & Digital Output Check
void setup() { pinMode(2, INPUT); Serial.begin(115200); }
void loop() {
Serial.print("A0 Raw: "); Serial.print(analogRead(A0));
Serial.print(" | D2 DO: "); Serial.println(digitalRead(2));
delay(200);
}Complete Production Firmware
Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.
/**
* Project 13: LDR Ambient Light Sensor & Hysteresis Auto-Lamp
* Board: Arduino Uno R3 | Pins: A0 (LDR AO), D9 (Auto-Lamp LED)
*/
const uint8_t LDR_AO = A0;
const uint8_t LAMP_LED = 9;
const bool INVERT_MODULE = true; // Standard LM393 LDR modules output lower ADC in bright light
const int DARK_ON_PCT = 30; // Turn lamp ON below 30% brightness
const int BRIGHT_OFF_PCT = 42; // Turn lamp OFF above 42% brightness
bool lampActive = false;
int readSmoothedAdc() {
long sum = 0;
for (uint8_t i = 0; i < 8; i++) {
sum += analogRead(LDR_AO);
delay(4);
}
return sum / 8;
}
void setup() {
pinMode(LAMP_LED, OUTPUT);
Serial.begin(115200);
Serial.println(F("LDR Auto-Nightlight Controller Ready"));
}
void loop() {
int raw = readSmoothedAdc();
int lightPct = INVERT_MODULE ? map(raw, 1023, 0, 0, 100) : map(raw, 0, 1023, 0, 100);
lightPct = constrain(lightPct, 0, 100);
if (!lampActive && lightPct < DARK_ON_PCT) {
lampActive = true;
} else if (lampActive && lightPct > BRIGHT_OFF_PCT) {
lampActive = false;
}
digitalWrite(LAMP_LED, lampActive ? HIGH : LOW);
Serial.print(F("ADC: ")); Serial.print(raw);
Serial.print(F(" | Light: ")); Serial.print(lightPct);
Serial.print(F("% | Nightlamp: "));
Serial.println(lampActive ? F("ON") : F("OFF"));
delay(250);
}Firmware Architecture Explanation:
- Averages 8 consecutive ADC readings to suppress 50/60Hz indoor fluorescent/LED lighting ripple.
- Applies a 12-percentage-point hysteresis band (30% ON vs 42% OFF) to guarantee zero twilight flicker.
- Supports both standard LM393 modules and bare photoresistor dividers via INVERT_MODULE.
Operational Verification & Pin State Matrix
Input Signal vs. System Output Truth Table:
| Input / State | Pin / Signal | Detected Condition | Expected Output |
|---|---|---|---|
| Light < 30% | A0 Dark Zone | Dusk / Shadow | Night-Lamp D9 = ON |
| 30% – 42% | Hysteresis Band | Twilight Transition | Hold Previous State |
| Light > 42% | A0 Bright Zone | Daylight / Lit Room | Night-Lamp D9 = OFF |
Engineering Troubleshooting Matrix
| Problem | Likely Cause | Diagnostic Check | Engineering Solution |
|---|---|---|---|
| Lamp turns ON in bright light and OFF in the dark | Module voltage divider polarity inverted relative to code | Observe A0 raw change when covering sensor | Flip INVERT_MODULE between true and false. |
| Lamp flashes rapidly in dim light | LED light reflecting back onto the LDR or no hysteresis gap | Shield LED from LDR with paper | Aim LED away from LDR and maintain ≥10% hysteresis gap. |
| A0 reading stuck at 0 or 1023 | Missing ground or missing 10kΩ divider resistor on bare LDR | Measure voltage at A0 with multimeter | Verify 5V/GND connections and divider midpoint tap. |
Engineering Challenges & Upgrades
Take your build to the next level with these progressive engineering upgrades:
Use analogWrite(D9, map(lightPct, 0, 100, 255, 0)) so the lamp brightens smoothly as darkness deepens.
Compare left/right LDRs on A0 and A1 to rotate an SG90 servo directly toward the brightest light.
Advance to Project 31 by aiming a KY-008 red laser directly at the LDR.