MyRoboPathOpen Robotics Lab
Arduino Uno #13 of 35
Est. 0.5 Hours
Budget: $1 - $3
Beginner

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.

Arduino Uno R3LDR Photoresistor10-Bit ADC (A0)Voltage DividerHysteresis Control
LDR Photoresistor Ambient Light Sensor & Auto Lamp Blueprint
LDR Photoresistor Ambient Light Sensor & Auto LampCAD Blueprint

Key Learning Objectives & Robotics Concepts

By building and programming this project, you will master the following core engineering skills:

1Understand how photoresistor resistance drops nonlinearly as incident lux rises.
2Analyze a resistor-LDR voltage divider and convert 10-bit ADC counts (0–1023) into percentage brightness.
3Prevent twilight rapid relay/LED chatter using separate Dark-ON and Bright-OFF hysteresis thresholds.
4Compare software ADC thresholds against onboard LM393 hardware comparator tuning.

Power Architecture & Voltage Staging

5V / 3.3V Regulated Power

The 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.

LDR Photoresistor Ambient Light Sensor & Auto Lamp Power Architecture Schematic
Figure: Regulated Arduino Uno supply rails, current budget, and component protection.Power Schematic

Bill of Materials (BOM) & Hardware Components

Bill of Materials for LDR Photoresistor Ambient Light Sensor & Auto Lamp
ComponentSpecifications & AlternativeQtyApprox Cost
Arduino Uno R3ATmega328P with 10-bit ADC1$8.00
LDR Light Sensor Module (or Bare LDR + 10kΩ)CdS photoresistor + LM393 comparator1$1.20
5mm White/Yellow LED + 220Ω ResistorAutomatic night-lamp load1$0.15
Breadboard & Jumper Wires6× breadboard jumpers1$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.

Figure: Complete Arduino Uno R3 pin-to-pin wiring schematic for LDR Photoresistor Ambient Light Sensor & Auto Lamp.
Figure: Complete Arduino Uno R3 pin-to-pin wiring schematic for LDR Photoresistor Ambient Light Sensor & Auto Lamp.Wiring Schematic

Autonomous Behavior & Finite State Machine (FSM)

Deterministic Arduino loop handling LDR Photoresistor Module (AO/DO + LM393) acquisition, filtering, and real-time output updates.

Figure: Firmware execution flow and state transitions for LDR Photoresistor Ambient Light Sensor & Auto Lamp.
Figure: Firmware execution flow and state transitions for LDR Photoresistor Ambient Light Sensor & Auto Lamp.Control Algorithm

Step-by-Step Physical Assembly & Wiring Tutorial

Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.

1

Inspect LDR Module Pinout & Trimpot

Duration: 5 Mins

Locate VCC, GND, DO (Digital Out), and AO (Analog Out) on the LDR breakout board.

LDR Photoresistor Ambient Light Sensor & Auto Lamp hardware layout diagram
Stage 1: LDR Photoresistor Module (AO/DO + LM393) hardware inspection & pinout overview.Stage 1 Diagram
Step Action Checklist:
  • 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.
Engineering & Tuning Tip: If the auto-lamp LED shines directly onto the LDR, the lamp will oscillate ON/OFF—point them away from each other!
2

Wire LDR to A0/D2 & Night-Lamp LED to D9

Duration: 5 Mins

Connect power, analog/digital outputs, and the LED indicator.

LDR Photoresistor Ambient Light Sensor & Auto Lamp breadboard wiring check diagram
Stage 2: Breadboard layout & Arduino Uno pin-to-pin wiring audit.Stage 2 Diagram
Step Action Checklist:
  • Connect LDR VCC → 5V and GND → GND.
  • Connect LDR AO → A0 and DO → D2.
  • Wire D9 → 220Ω resistor → LED anode → cathode to GND.
Engineering & Tuning Tip: Using a bare 2-pin LDR? Wire 5V → LDR → A0 node → 10kΩ resistor → GND.
3

Record Bright & Dark Baseline Values

Duration: 5 Mins

Run the ADC diagnostic to find your room's exact bright vs. covered readings.

LDR Photoresistor Ambient Light Sensor & Auto Lamp power rail protection diagram
Stage 3: Supply rail check (USB 5.0V Regulated Rail (<5mA sensor)) and circuit protection verification.Stage 3 Diagram
Step Action Checklist:
  • 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.
Engineering & Tuning Tip: Knowing your real room min/max lets you set exact hysteresis thresholds instead of guessing.
4

Upload Hysteresis Auto-Nightlight Firmware

Duration: 10 Mins

Flash the smoothed light monitor with separate ON/OFF hysteresis bounds.

LDR Photoresistor Ambient Light Sensor & Auto Lamp serial telemetry verification diagram
Stage 4: Firmware upload & 115200 baud Serial Monitor verification.Stage 4 Diagram
Step Action Checklist:
  • 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.
Engineering & Tuning Tip: A 10–15% deadband between turn-ON and turn-OFF eliminates flicker during sunset transitions.
5

Calibrate Onboard Comparator Trimpot

Duration: 5 Mins

Turn the blue potentiometer gently until the module's onboard DO LED toggles at dusk.

LDR Photoresistor Ambient Light Sensor & Auto Lamp live operational logic diagram
Stage 5: Live operational logic flow & calibration verification.Stage 5 Diagram
Step Action Checklist:
  • 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.
Engineering & Tuning Tip: Using AO gives continuous dimming flexibility, while DO frees up analog pins for threshold-only triggers.

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 Diagnostic

Minimal standalone sketch to verify pin wiring and signal response for LDR Photoresistor Module (AO/DO + LM393) before running the full project firmware.

Arduino IDE Diagnostic ScriptC++ / Arduino
// 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);
}
Expected Output: A0 Raw shifts cleanly (e.g., 150 in bright light vs 850 in shadow).

Complete Production Firmware

Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.

arduino_uno_ldr_photoresistor_light_sensor.ino
arduino_uno_ldr_photoresistor_light_sensor.inoArduino C++
/**
 * 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

1Double-check all 4 Arduino Uno pin connections against the wiring schematic before plugging in USB.
2Upload the Bench Diagnostic sketch first and confirm Serial Monitor at 115200 baud matches: A0 Raw shifts cleanly (e.g., 150 in bright light vs 850 in shadow).
3Upload the complete production sketch and verify every condition in the operational truth table below.

Input Signal vs. System Output Truth Table:

Operational truth table for LDR Photoresistor Ambient Light Sensor & Auto Lamp
Input / StatePin / SignalDetected ConditionExpected Output
Light < 30%A0 Dark ZoneDusk / ShadowNight-Lamp D9 = ON
30% – 42%Hysteresis BandTwilight TransitionHold Previous State
Light > 42%A0 Bright ZoneDaylight / Lit RoomNight-Lamp D9 = OFF

Engineering Troubleshooting Matrix

Engineering troubleshooting matrix for LDR Photoresistor Ambient Light Sensor & Auto Lamp
ProblemLikely CauseDiagnostic CheckEngineering Solution
Lamp turns ON in bright light and OFF in the darkModule voltage divider polarity inverted relative to codeObserve A0 raw change when covering sensorFlip INVERT_MODULE between true and false.
Lamp flashes rapidly in dim lightLED light reflecting back onto the LDR or no hysteresis gapShield LED from LDR with paperAim LED away from LDR and maintain ≥10% hysteresis gap.
A0 reading stuck at 0 or 1023Missing ground or missing 10kΩ divider resistor on bare LDRMeasure voltage at A0 with multimeterVerify 5V/GND connections and divider midpoint tap.

Engineering Challenges & Upgrades

Take your build to the next level with these progressive engineering upgrades:

Level 1Proportional Dimming Streetlight

Use analogWrite(D9, map(lightPct, 0, 100, 255, 0)) so the lamp brightens smoothly as darkness deepens.

Level 2Dual-LDR Solar Tracker

Compare left/right LDRs on A0 and A1 to rotate an SG90 servo directly toward the brightest light.

Level 3Laser Tripwire Alarm

Advance to Project 31 by aiming a KY-008 red laser directly at the LDR.