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.1 – Automatic Light Control with Brightness Control

🎯 Learning Objectives

After completing this lesson, students will be able to:

✅ Control LED brightness automatically

✅ Use PWM (Pulse Width Modulation)

✅ Convert LDR readings into brightness values

✅ Use the map() function

✅ Combine digitalWrite() and analogWrite()

✅ Create a smart dimming light system


1. Introduction

In Lesson 3.3, the LED was either:

 
ON
or
OFF
 

However, real smart lighting systems do not work this way.

Instead:

 
Very Bright Environment

LED OFF

Moderate Light

LED Dim

Dark Environment

LED Bright

Complete Darkness

LED Maximum Brightness
 

This creates a smooth and intelligent lighting system.


2. PWM (Pulse Width Modulation)

PWM allows Arduino to control brightness.

Arduino UNO PWM Pins:

 
3
5
6
9
10
11
 

We will connect the LED to:

 
Pin 11
 

because Pin 11 supports PWM.


3. Components Required

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

4. Circuit Connections

LDR Module

LDR Pin Arduino
VCC 5V
GND GND
AO A0

LED

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

5. Working Logic

 
Read LDR Value

Convert to Brightness

Brightness < 10 ?

YES

LED OFF using digitalWrite()

NO

Control Brightness using analogWrite()
 

6. Arduino Program

 
int ldrPin = A0;
int ledPin = 11;

int ldrValue;
int brightness;

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

Serial.begin(9600);
}

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

// Convert LDR reading into PWM brightness
brightness = map(ldrValue, 0, 1023, 255, 0);

if(brightness < 10)
{
digitalWrite(ledPin, LOW);
}
else
{
analogWrite(ledPin, brightness);
}

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

Serial.print(" Brightness = ");
Serial.println(brightness);

delay(100);
}
 

7. Code Explanation

Read LDR

 
ldrValue = analogRead(A0);
 

Reads light intensity from the sensor.


Convert Reading

 
brightness = map(ldrValue, 0, 1023, 255, 0);
 

Example:

LDR Reading Brightness
0 255
250 192
500 128
750 64
1023 0

Turn OFF LED Completely

 
if(brightness < 10)
{
digitalWrite(ledPin, LOW);
}
 

If brightness becomes very small, the LED is switched OFF completely.


PWM Brightness Control

 
analogWrite(ledPin, brightness);
 

Controls LED intensity smoothly from:

 
0 → OFF
255 → Maximum Brightness
 

8. Testing

Test 1 – Mobile Flashlight

Expected:

 
LDR = 950
Brightness = 5

LED OFF
 

Test 2 – Room Light

Expected:

 
LDR = 650
Brightness = 90

LED Dim
 

Test 3 – Cover Sensor

Expected:

 
LDR = 120
Brightness = 225

LED Bright
 

9. Real-World Applications

  • Smart Street Lights
  • Automatic Garden Lights
  • Smart Home Lighting
  • Solar Street Lamps
  • Warehouse Lighting Systems
  • Parking Area Automation

📊 Summary

In this lesson we learned:

✅ PWM Brightness Control

analogWrite()

map() Function

✅ Combining digitalWrite() and analogWrite()

✅ Smart Dimming System

✅ Advanced Automatic Light Control

Scroll to Top