Photoresistor (LDR) Light Sensor using Arduino in Wokwi Simulator

 


Build a simple Light Dependent Resistor (LDR) project using Arduino Uno in the Wokwi simulator to detect light intensity and control an LED.

 Short Description

This project uses a photoresistor (LDR) to measure light levels. When the surrounding light decreases, the LDR resistance increases, and the Arduino reads the change through an analog pin. Based on the light intensity, an LED turns ON or OFF automatically. It is a beginner-friendly project ideal for learning analog input and sensor interfacing in Arduino.

Components Required (Wokwi)

  • Arduino Uno

  • Photoresistor (LDR)

  • 10kΩ Resistor

  • LED

  • 220Ω Resistor

  • Jumper wires

code:
/*
 * Photoresistor Light Sensor with Arduino
 * This code reads light levels from a photoresistor (LDR)
 * and displays the values on the Serial Monitor
 */

// Pin definitions
const int PHOTORESISTOR_PIN = A0;  // Analog pin for photoresistor
const int LED_PIN = 13;             // Built-in LED pin

// Variables
int lightValue = 0;                 // Variable to store the light reading
int threshold = 500;                // Threshold value for LED control

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
 
  // Set LED pin as output
  pinMode(LED_PIN, OUTPUT);
 
  Serial.println("Photoresistor Light Sensor Initialized");
  Serial.println("Light Level | Status");
  Serial.println("-------------------------");
}

void loop() {
  // Read the photoresistor value (0-1023)
  lightValue = analogRead(PHOTORESISTOR_PIN);
 
  // Print light level to Serial Monitor
  Serial.print("Light: ");
  Serial.print(lightValue);
  Serial.print(" | ");
 
  // Determine light condition
  if (lightValue < 300) {
    Serial.println("DARK");
    digitalWrite(LED_PIN, HIGH);  // Turn LED ON in dark
  } else if (lightValue < 700) {
    Serial.println("DIM");
    digitalWrite(LED_PIN, HIGH);  // Turn LED ON in dim light
  } else {
    Serial.println("BRIGHT");
    digitalWrite(LED_PIN, LOW);   // Turn LED OFF in bright light
  }
 
  // Wait before next reading
  delay(500);
}

 How It Works

  • The LDR and 10kΩ resistor form a voltage divider.

  • Arduino reads analog values (0–1023) from A0.

  • If the light value is below a threshold (dark condition), the LED turns ON.

  • If the light value is above the threshold (bright condition), the LED turns OFF.

Diagram.json:
{
  "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": {}
}


 Applications

  • Automatic street lights

  • Smart home lighting systems

  • Light intensity monitoring projects

  • Energy-saving systems

This Wokwi simulation helps you test and understand LDR-based light sensing without physical hardware.


Comments