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
| Component | Qty | |
|---|---|---|
| Arduino UNO R3 board | 1 | |
| Small breadboard and jumper wires | 1 | |
| Red LED | 1 | |
| 470 ohms resistor | 1 | |
| Photocell | 1 | |
| 10K resistor | 1 |
Step by Step
Hardware Requirements
<p style="text-align: justify;">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.</p>
Software Setup
<p style="text-align: justify;">You only need the Arduino IDE installed on your computer to program the board. Download it from <a href="https://arduino.cc/en/Main/Software">https://arduino.cc/en/Main/Software</a>. The Arduino IDE is free, open-source, and works on Windows, macOS, and Linux platforms.</p>
Wire the LED
<p style="text-align: justify;">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.</p>
Configure the Voltage Divider Circuit
<p style="text-align: justify;">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.</p><img src="https://s3-us-west-2.amazonaws.com/purepress/files/qXJkAXvZq3DuWY4r7-original.png">
Test the Photocell
<p style="text-align: justify;">Before integrating everything, test the photocell to understand its output range. Upload this code to read the sensor values:</p><pre style="text-align: justify;"><span>// Define analog pin</span> <span style="font-weight: 700;">int</span> sensorPin = <span>0</span>; <span>// Setup</span> <span><span style="font-weight: 700;">void</span> <span style="font-weight: 700;">setup</span><span>()</span> </span>{ <span>// Init serial</span> Serial.begin(<span>9600</span>); } <span>// Main loop</span> <span><span style="font-weight: 700;">void</span> <span style="font-weight: 700;">loop</span><span>()</span> </span>{ <span>// Get temperature</span> <span style="font-weight: 700;">int</span> sensorValue = analogRead(sensorPin); <span>// Put temperature on the serial port</span> Serial.println(sensorValue,DEC); <span>// Wait for 1 sec</span> delay(<span>1000</span>); }</pre><p style="text-align: justify;">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.</p><img src="https://s3-us-west-2.amazonaws.com/purepress/files/vjCmyXRuoQ6qRbJbv-original.png">
Implement Threshold Logic
<p style="text-align: justify;">Define two thresholds in your code to prevent the light from flickering near the transition point:</p><pre style="text-align: justify;"><span style="font-weight: 700;">int</span> lowThreshold = <span>500</span>; <span style="font-weight: 700;">int</span> highThreshold = <span>600</span>;</pre><p style="text-align: justify;">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.</p>
Read Sensor and Control Output
<p style="text-align: justify;">Read the analog input and compare it to your thresholds:</p><pre style="text-align: justify;"><span style="font-weight: 700;">int</span> sensorValue = analogRead(sensorPin); <span style="font-weight: 700;">if</span> (sensorValue < lowThreshold){ digitalWrite(lightPin, HIGH); } <span style="font-weight: 700;">else if</span> (sensorValue > highThreshold){ digitalWrite(lightPin, LOW); }</pre><p style="text-align: justify;">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.</p>
Test and Deploy
<p style="text-align: justify;">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.</p>
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.