Course Content
📘 MODULE 11 – Edge Avoiding Robot
📦 MODULE 12 – Smart Multi-Function Robot (Mega Project)
Arduino Hands-On Programming and Robotics Course

📖 Lesson 3.3 – Automatic Light Control Code

🎯 Learning Objectives

After completing this lesson, students will be able to:

✅ Create an Automatic Light Control System

✅ Read LDR sensor values using Arduino

✅ Control an LED automatically

✅ Use analogRead() and digitalWrite()

✅ Understand decision-making using sensor values

✅ Build a basic Smart Street Light System


1. Introduction

In the previous lesson, we connected the LDR Sensor Module with Arduino UNO and displayed light intensity values on the Serial Monitor.

Now we will use those values to automatically control an LED.

Our goal is simple:

Bright Environment

LED OFF


Dark Environment

LED ON

This is the basic principle behind automatic street lights and smart lighting systems.


2. Project Overview

The system continuously monitors light intensity.

 
LDR Sensor

Arduino Reads Light Level

Decision Making

LED ON / OFF
 

3. Components Required

Component Quantity
Arduino UNO 1
LDR Sensor Module 1
LED 1
220Ω Resistor 1
Breadboard 1
Jumper Wires As Required
USB Cable 1

4. Circuit Connections

LDR Module

LDR Pin Arduino UNO
VCC 5V
GND GND
AO A0
DO Not Connected

LED Connection

LED Arduino
Anode (+) D13 through 220Ω resistor
Cathode (-) GND

Circuit Diagram

 
LDR Module

VCC -----> 5V
GND -----> GND
AO -----> A0

LED

D13 -----> Resistor -----> LED -----> GND
 

5. Understanding Automation Logic

Suppose our LDR readings are:

Light Condition Reading
Bright Light 800
Room Light 600
Dim Light 300
Darkness 100

We can choose a threshold value:

 
400
 

Now:

Reading < 400

Dark Environment

LED ON


Reading > 400

Bright Environment

LED OFF


6. Algorithm

Step 1

Read LDR Value

Step 2

Compare with Threshold

Step 3

Is Value Less Than 400?

YES

Turn LED ON

NO

Turn LED OFF

Repeat Forever


7. Arduino Program

 
int ldrValue;

void setup()
{
pinMode(13, OUTPUT);

Serial.begin(9600);
}

void loop()
{
ldrValue = analogRead(A0);

Serial.print("LDR Value = ");
Serial.println(ldrValue);

if(ldrValue > 400)
{
digitalWrite(13, HIGH);
}
else
{
digitalWrite(13, LOW);
}

delay(200);
}
 

8. Program Explanation

Read Sensor Value

 
ldrValue = analogRead(A0);
 

Reads light intensity from the LDR module.


Display Value

 
Serial.println(ldrValue);
 

Shows sensor readings on Serial Monitor.


Decision Making

 
if(ldrValue < 400)
 

Checks whether the environment is dark.


Turn LED ON

 
digitalWrite(13, HIGH);
 

LED glows in darkness.


Turn LED OFF

 
digitalWrite(13, LOW);
 

LED turns OFF in bright light.


9. Testing the System

Test 1 – Bright Light

Place sensor under room light.

Expected:

 
LDR Value = 700
LED OFF
 

Test 2 – Flashlight

Shine mobile flashlight.

Expected:

 
LDR Value = 900
LED OFF
 

Test 3 – Cover Sensor

Cover LDR using your finger.

Expected:

 
LDR Value = 100
LED ON
 

10. How This Works in Real Life

The same principle is used in:

Street Lights

Day

Light Available

Street Light OFF


Night

Darkness

Street Light ON


Garden Lights

Automatically turn ON after sunset.


Smart Home Lighting

Lights activate when rooms become dark.


11. Understanding Threshold Value

The threshold value:

 
400
 

is not fixed.

Different environments may require:

 
300
 

or

 
500
 

or

 
600
 

Proper threshold selection is called:

Calibration


Example

Threshold Sensitivity
200 Very Dark Only
400 Moderate
600 More Sensitive

12. Enhanced Program with Message Display

 
int ldrValue;

void setup()
{
pinMode(13, OUTPUT);

Serial.begin(9600);
}

void loop()
{
ldrValue = analogRead(A0);

if(ldrValue < 400)
{
digitalWrite(13, HIGH);

Serial.println("Darkness Detected");
}
else
{
digitalWrite(13, LOW);

Serial.println("Bright Environment");
}

delay(500);
}
 

13. Common Beginner Mistakes

Mistake 1

Connecting AO to Digital Pin.

Wrong:

 
AO → D2
 

Correct:

 
AO → A0
 

Mistake 2

Using digitalRead().

Wrong:

 
digitalRead(A0);
 

Correct:

 
analogRead(A0);
 

Mistake 3

Wrong Threshold Value.

May cause LED to remain always ON or OFF.


Mistake 4

Loose Wiring.

Produces unstable readings.


14. Troubleshooting

LED Always ON

Check:

  • Threshold Value
  • LDR Reading

LED Never Turns ON

Check:

  • LED Connection
  • AO Connection
  • Sensor Readings

Random Behavior

Check:

  • Loose Jumper Wires
  • Sensor Exposure

No Serial Output

Check:

 
Serial.begin(9600);
 

and Serial Monitor baud rate.


15. Real-World Applications

Automatic Street Lights

Smart Garden Lights

Solar Street Lamps

Smart Home Systems

Light Monitoring Systems

Security Systems


📊 Summary

In this lesson, we learned:

✅ Automatic Light Control

✅ Reading LDR Values

✅ Threshold-Based Decisions

✅ LED Automation

✅ Smart Lighting Logic

This project is the foundation of many real-world automation systems that automatically respond to environmental light conditions.


📖 Key Terms

Threshold Value

The value used to trigger an action.

Automation

Automatic operation without human intervention.

Analog Reading

Sensor value between 0 and 1023.

Calibration

Adjusting threshold values for correct operation.

Smart Lighting

Lighting system that responds automatically to surroundings.


🎯 Quiz

1. Which function reads the LDR value?

A. digitalWrite()

B. analogRead() ✅

C. pinMode()

D. Serial.read()


2. Which function controls the LED?

A. analogRead()

B. Serial.begin()

C. digitalWrite() ✅

D. digitalRead()


3. What happens when the LDR value is below the threshold?

A. LED OFF

B. LED ON ✅

C. Arduino OFF

D. Sensor OFF


4. What is the purpose of a threshold value?

A. Power Supply

B. Sensor Protection

C. Decision Making ✅

D. Uploading Code


5. Which application commonly uses this logic?

A. Smart Street Light ✅

B. Calculator

C. Keyboard

D. Printer


🏠 Assignment

Task 1

Upload the automatic light control program and test it.

Task 2

Record LDR readings under different lighting conditions.

Task 3

Try threshold values of 300, 500, and 700 and observe the difference.

Task 4

Create a table showing LED status for different light levels.

Task 5

Explain how automatic street lights use the same logic as this project.

Scroll to Top