Photoresistor (LDR) Light Sensor using Arduino in Wokwi Simulator

LDR Photoresistor Light Sensor with Arduino Uno — Wokwi Simulation | MakeMindz
Beginner Project · Module 7 · Arduino + Wokwi

LDR Light Sensor
with Arduino Uno

Build an automatic light detection system using a photoresistor (LDR) — reads light intensity and controls an LED automatically. Simulated free in Wokwi, no hardware needed.

Arduino UNO Photoresistor (LDR) Voltage Divider Analog Input A0 ~25 min project Beginner
// 00 — Module Overview

What You'll Build

An automatic light-detecting system that turns an LED ON in dark conditions and OFF in bright light — just like a real street lamp controller.

Analog Sensing

Read real-world light intensity as a 0–1023 digital value using analogRead().

Voltage Divider

Understand how the LDR and 10kΩ resistor convert resistance change into voltage change.

LED Auto-Control

LED turns ON in DARK/DIM and OFF in BRIGHT — threshold-based automation logic.

Serial Debugging

Live data printed to Serial Monitor — essential skill for real hardware debugging.

ADC Conversion

Learn how Arduino's 10-bit ADC converts analog voltage into numbers (0–1023).

Real-World IoT

Foundation for automatic street lights, smart home lighting, and energy-saving systems.


// 01 — The Science

How the LDR & Voltage Divider Work

Arduino can only read voltage, not resistance directly. So the LDR pairs with a fixed resistor to form a voltage divider — converting resistance changes into readable voltage.

Vout = 5V × Rfixed / (RLDR + Rfixed)

LDR + 10kΩ resistor · Output read at Arduino A0

Bright light → LDR resistance drops (e.g. 500Ω)

Voltage at A0 increases toward 5V

Arduino ADC reads a high value (e.g. 850+)

Dark → resistance rises → voltage drops → low ADC value

Code compares value to thresholds → controls LED

Why 10kΩ as the fixed resistor?

A 10kΩ resistor is chosen because it's close to the LDR's typical resistance in moderate light. This centres the voltage divider output in the mid-range of the ADC, giving you the best sensitivity across both dark and bright conditions.

💡 The ADC value range is 0–1023. 0 = 0V (total darkness), 1023 = 5V (maximum brightness). In practice, values sit somewhere in between depending on ambient light.


// 02 — Parts List

Components Used

All available as virtual parts in Wokwi — no shopping needed.

01
Arduino UNOMicrocontroller — reads analog values and controls the LED output.
02
Photoresistor (LDR)Light-dependent resistor — resistance changes with light intensity.
03
10kΩ ResistorPaired with the LDR to form a voltage divider circuit.
04
LED (Red)Visual output — turns ON in dark/dim conditions, OFF in bright.
05
220Ω ResistorCurrent limiter for the LED — prevents burnout.
06
Jumper WiresConnect components. Color-coded for clarity.

// 03 — Circuit Setup

Wiring the Circuit

Two ways to set up — paste the diagram.json instantly, or wire manually using the tables below.

  1. Quick Setup: Paste the diagram.json

    The fastest way — paste the complete circuit JSON directly into Wokwi:

    • Click the diagram.json tab in the Wokwi editor
    • Select all (Ctrl+A) and delete existing content
    • Paste the full JSON below, then press Ctrl+S
    diagram.json — paste into Wokwi
    {
      "version": 1,
      "author": "Wokwi Photoresistor Project",
      "editor": "wokwi",
      "parts": [
        {
          "type": "wokwi-arduino-uno",
          "id": "uno", "top": 0, "left": 0, "attrs": {}
        },
        {
          "type": "wokwi-photoresistor-sensor",
          "id": "ldr1", "top": -57.6, "left": 124.8, "attrs": {}
        },
        {
          "type": "wokwi-resistor",
          "id": "r1", "top": 105.55, "left": 182.4, "rotate": 90,
          "attrs": { "value": "10000" }
        },
        {
          "type": "wokwi-led",
          "id": "led1", "top": -76.8, "left": 278.2,
          "attrs": { "color": "red" }
        },
        {
          "type": "wokwi-resistor",
          "id": "r2", "top": 9.55, "left": 268.8, "rotate": 90,
          "attrs": { "value": "220" }
        }
      ],
      "connections": [
        [ "uno:GND.1", "r1:1",    "black",  ["v0"] ],
        [ "ldr1:VCC", "uno:5V",  "red",    ["v0"] ],
        [ "ldr1:AO",  "uno:A0",  "green",  ["v0"] ],
        [ "ldr1:GND", "r1:2",    "black",  ["v0"] ],
        [ "uno:13",   "r2:1",    "orange", ["v0"] ],
        [ "r2:2",     "led1:A", "orange", ["v0"] ],
        [ "led1:C",   "uno:GND.2", "black", ["v0"] ]
      ],
      "dependencies": {}
    }

    ⚠️ Copy the entire JSON including the outer { } braces, or the circuit won't load correctly.

  2. LDR (Photoresistor) Pin Connections

    ComponentArduino PinNotes
    LDR VCC5VPower supply
    LDR GNDGND via 10kΩVoltage divider pull-down
    LDR Output (AO)A0Green wire

    💡 The LDR's output pin sits between the LDR and the 10kΩ resistor — this is the voltage divider mid-point that the Arduino reads.

  3. LED Pin Connections

    ComponentConnectionWire
    LED Anode (+)Pin 13 via 220ΩOrange
    LED Cathode (−)GNDBlack
    220Ω ResistorBetween Pin 13 and LED anodeCurrent limiter

// 04 — The Code

Arduino Code — Explained

Paste the full code into the sketch.ino tab in Wokwi. Here's each section broken down:

① Pin Setup & Initialization

Runs once at power-on. Configures pins, opens the Serial Monitor at 9600 baud, and prints a header.

sketch.ino — full code
/*
 * Photoresistor Light Sensor with Arduino
 * Reads light levels from an LDR and controls an LED automatically.
 */

// Pin definitions
const int PHOTORESISTOR_PIN = A0;  // Analog pin for LDR
const int LED_PIN = 13;            // LED output pin

// Variables
int lightValue = 0;    // Stores the analog reading (0–1023)
int threshold  = 500;  // Threshold for LED switching

void setup() {
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);

  Serial.println("Photoresistor Light Sensor Initialized");
  Serial.println("Light Level | Status");
  Serial.println("-------------------------");
}

void loop() {
  // 1. Read the LDR value (0–1023)
  lightValue = analogRead(PHOTORESISTOR_PIN);

  // 2. Print to Serial Monitor
  Serial.print("Light: ");
  Serial.print(lightValue);
  Serial.print(" | ");

  // 3. Classify and control LED
  if (lightValue < 300) {
    Serial.println("DARK");
    digitalWrite(LED_PIN, HIGH);  // ON in dark

  } else if (lightValue < 700) {
    Serial.println("DIM");
    digitalWrite(LED_PIN, HIGH);  // ON in dim light

  } else {
    Serial.println("BRIGHT");
    digitalWrite(LED_PIN, LOW);   // OFF in bright light
  }

  delay(500);  // Read every 500ms
}

② Serial Monitor Output

Open the Serial Monitor in Wokwi to see live readings like this:

Photoresistor Light Sensor Initialized
Light Level | Status
-------------------------
Light: 245 | DARK
Light: 188 | DARK
Light: 612 | DIM
Light: 534 | DIM
Light: 850 | BRIGHT
Light: 978 | BRIGHT

// 05 — System Logic

Light Zones & LED Behaviour

The code classifies the analog reading into three zones, each triggering a different LED state.

0 – 299
🌑 DARK

Very little or no ambient light. LDR resistance is very high.

LED ON · analogRead < 300
300 – 699
🌗 DIM

Partial light. LDR resistance is moderate. Still triggers the LED.

LED ON · 300 ≤ value < 700
700 – 1023
☀️ BRIGHT

Strong ambient light. LDR resistance drops. LED turns off automatically.

LED OFF · value ≥ 700

How the Threshold Values Work

The values 300 and 700 are thresholds that work well in Wokwi. In real hardware, you'd calibrate these based on your environment — for instance, a room that's "bright enough" at noon might read 600, while a dark room reads 80. Always test and adjust the thresholds for your specific conditions.

💡 Try changing 300 and 700 in the code to see how the LED behaviour changes. Lowering 300 means only very dark conditions trigger the LED.


// 06 — Running the Simulation

Testing in Wokwi

Adjust the LDR light slider in real-time and watch the LED respond instantly.

  1. Paste Code and Press Play

    Paste the full code into sketch.ino, then click the green ▶ Play button. The Serial Monitor immediately starts printing readings.

  2. Adjust the LDR Light Slider

    Click on the photoresistor in the simulation — a light intensity slider appears! Try these values:

    • 10 lux → Very dark, LED ON, Serial: DARK
    • 100 lux → Dim, LED ON, Serial: DIM
    • 500 lux → Bright, LED OFF, Serial: BRIGHT
    • 1000 lux → Very bright, LED stays OFF

    💡 Watch the red LED turn on and off in real-time as you drag the slider — no delay because readings happen every 500ms.

  3. Open the Serial Monitor

    Click the Serial Monitor tab at the bottom of Wokwi. You'll see live printouts showing the raw ADC value and the classified zone (DARK / DIM / BRIGHT) updating every 500ms.

    💡 This is exactly how real engineers debug sensors — print the raw value, check if it matches expectations, then tune your thresholds.

  4. Try Changing the Threshold

    In the code, change int threshold = 500 and the comparison values 300 / 700. Re-run and notice how the LED now responds to different light levels. This is the core skill in sensor-based programming.

    ⚠️ If the LED never turns off or never turns on, your thresholds may be too high or too low for the simulator's lux range. Adjust them until behaviour feels right.


// 07 — Learning Outcomes

Key Concepts Covered

By completing this project you'll have practical experience with these foundational electronics and coding skills.

What is an LDR and how it changes resistance with light
Voltage Divider — converting resistance to readable voltage
ADC — 10-bit analog to digital conversion (0–1023)
Using analogRead() to read sensor values
Threshold-based decision making with if/else if
Output control — digitalWrite() HIGH / LOW
Serial Monitor for live data monitoring and debugging
Basic automation — sensor input drives output behaviour

// 08 — Extend It

Extension Challenges

Finished the core project? Try these to go further.

  • Add a second LED that lights up only in BRIGHT conditions
  • Display the ADC value and zone on a 16×2 LCD screen
  • Use PWM to dim the LED proportionally to darkness level
  • Add a buzzer that beeps when switching from BRIGHT to DARK
  • Log all readings to Serial with timestamps using millis()
  • Add hysteresis — prevent LED flickering near threshold boundaries
// 09 — Knowledge Check

Quick Quiz

Test your understanding before moving to the next project.

Q1. What does an LDR (photoresistor) do in bright light?

Q2. Why is a 10kΩ resistor paired with the LDR?

Q3. What range of values does analogRead() return on Arduino UNO?

Q4. In this project, when does the LED turn OFF?

Q5. Which Arduino pin reads the analog voltage from the LDR circuit?


// 10 — More Projects

Explore More Arduino Projects

Continue learning with more Wokwi simulations from MakeMindz — organized by difficulty.

Module 7 · LDR Light Sensor · Arduino + Wokwi

Simulate free at wokwi.com · No hardware needed · MakeMindz

Comments

try for free