makecademy
ArduinoBeginner25 min

Smart Lighting using Arduino

Smart Lighting using Arduino

In this project, you will learn how to switch a light on or off depending on exterior light levels using an Arduino board and a photocell sensor (light-dependent resistor). This automated lighting system can be scaled from a simple LED prototype to a full home automation solution. By the end of this tutorial, you'll understand voltage dividers, analog sensing, and threshold-based control logic.

What you'll need

ComponentQty
Arduino UNO R3 board1Buy
Small breadboard and jumper wires1Buy
Red LED1Buy
470 ohms resistor1Buy
Photocell1Buy
10K resistor1Buy

Step by step

1

Hardware Requirements

For this project, you will need an Arduino UNO R3 board (other Arduino boards will also work), a small breadboard, a red LED with a 470 ohms current-limiting resistor, a photocell (light-dependent resistor), and a 10K ohms resistor for the voltage divider circuit. All these components are readily available from electronics suppliers and have been linked in the components list above.

2

Software Setup

You only need the Arduino IDE installed on your computer to program the board. Download it from https://arduino.cc/en/Main/Software. The Arduino IDE is free, open-source, and works on Windows, macOS, and Linux platforms.

3

Wire the LED

Connect a wire from Arduino pin 2 to one leg of the 470 ohms resistor. Connect the other leg of the resistor to the anode (long leg) of the LED. Finally, connect the cathode (short leg) of the LED to the ground pin on the Arduino. This configuration protects the LED from overcurrent.

4

Configure the Voltage Divider Circuit

The photocell acts as a variable resistor. We use a voltage divider circuit to convert its resistance changes into measurable voltage changes. Connect your 5V power supply to one end of the photocell, then connect the other end to one leg of the 10K resistor. Connect the other leg of the 10K resistor to ground. The junction between the photocell and the 10K resistor connects to Arduino analog input pin A0. This configuration creates a voltage output proportional to light levels: Vout = Vin × R1/(R1+R2), where R1 is the photocell resistance and R2 is the 10K resistor.

5

Test the Photocell

Before integrating everything, test the photocell to understand its output range. Upload this code to read the sensor values:

// Define analog pin
int sensorPin = 0;

// Setup
void setup() {
 // Init serial
 Serial.begin(9600);
}

// Main loop
void loop() {

 // Get temperature
 int sensorValue = analogRead(sensorPin);

 // Put temperature on the serial port
 Serial.println(sensorValue,DEC);

 // Wait for 1 sec
 delay(1000);
}

Open the Serial Monitor (Tools > Serial Monitor) and observe the values. Cover the photocell to get darkness readings (typically around 300-400) and expose it to bright light to get bright readings (typically around 800-900). Record these values as you'll use them to calibrate the thresholds.

6

Implement Threshold Logic

Define two thresholds in your code to prevent the light from flickering near the transition point:

int lowThreshold = 500;
int highThreshold = 600;

These values depend on your photocell's calibration values. Adjust them based on your recorded measurements. Using two thresholds (hysteresis) prevents oscillation: the light turns on when the value falls below 500 and turns off when it exceeds 600.

7

Read Sensor and Control Output

Read the analog input and compare it to your thresholds:

int sensorValue = analogRead(sensorPin);

if (sensorValue < lowThreshold){
  digitalWrite(lightPin, HIGH);
}

else if (sensorValue > highThreshold){
  digitalWrite(lightPin, LOW);
}

When ambient light is low (below 500), the LED turns on. When ambient light is high (above 600), the LED turns off. The gap between thresholds prevents unwanted switching.

8

Test and Deploy

Upload the complete code to your Arduino and test by covering and uncovering the photocell. The LED should smoothly transition between on and off states. For a real-world deployment, place the photocell outside your home or window to detect day/night cycles, and connect the Arduino output to a relay module that controls your actual lights instead of the LED.

Wrapping up

Congratulations! You have successfully built an automatic smart lighting system using Arduino. This project demonstrates fundamental concepts in electronics including voltage dividers, analog sensing, and conditional logic. Scale this up by replacing the LED with a relay to control real household lights, or integrate it with a home automation system for comprehensive control. The principles you've learned here apply to many other sensor-based Arduino projects. Feel free to experiment with different threshold values and sensor placements to optimize the system for your environment.