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.
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.
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.
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.
Components Used
All available as virtual parts in Wokwi — no shopping needed.
Wiring the Circuit
Two ways to set up — paste the diagram.json instantly, or wire manually using the tables below.
-
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
{ "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.
-
LDR (Photoresistor) Pin Connections
Component Arduino Pin Notes LDR VCC 5V Power supply LDR GND GND via 10kΩ Voltage divider pull-down LDR Output (AO) A0 Green 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.
-
LED Pin Connections
Component Connection Wire LED Anode (+) Pin 13 via 220Ω Orange LED Cathode (−) GND Black 220Ω Resistor Between Pin 13 and LED anode Current limiter
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.
/* * 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:
Light Zones & LED Behaviour
The code classifies the analog reading into three zones, each triggering a different LED state.
Very little or no ambient light. LDR resistance is very high.
LED ON · analogRead < 300Partial light. LDR resistance is moderate. Still triggers the LED.
LED ON · 300 ≤ value < 700Strong ambient light. LDR resistance drops. LED turns off automatically.
LED OFF · value ≥ 700How 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.
Testing in Wokwi
Adjust the LDR light slider in real-time and watch the LED respond instantly.
-
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.
-
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.
- 10 lux → Very dark, LED ON, Serial:
-
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.
-
Try Changing the Threshold
In the code, change
int threshold = 500and the comparison values300/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.
Key Concepts Covered
By completing this project you'll have practical experience with these foundational electronics and coding skills.
analogRead() to read sensor valuesif/else ifdigitalWrite() HIGH / LOWExtension 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
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?
Explore More Arduino Projects
Continue learning with more Wokwi simulations from MakeMindz — organized by difficulty.
Comments
Post a Comment