My image

Smart Lighting using Arduino

In this project, I will show you how to switch a light on or off depending on exterior light levels, with an Arduino board and some components. This will be achieved by using a photocell sensor, which is a basically a light-dependent resistor. In this project, the “light” will be modelled with a LED, and the photocell will be placed next to the Arduino board. But it is easy to imagine that the photocell will for example be placed outside of your home to detect if we are currently during the night or during daytime, and the Arduino will then switch the lights accordingly.

I also want to announce here that this article is the kind of article you are going to find in my upcoming, which will talk of course about home automation, open-source hardware and will be focused on the Arduino platform. As always, I included affiliate links in this article to help you find the components I used, please follow them if you also want to support this website. Thanks !

Hardware requirements

This project requires an Arduino board, in this tutorial I used the usual Arduino UNO R3 board, but other Arduino boards should be fine as well. Next to the Arduino board, I also used a small breadboard. You will also need a LED, along with a 470 ohms resistor. You will also need one photocell. Photocells are basically little light sensor, which behave like resistors but the value of their resistance vary with the light level. I used this model, but any resistor that is sensible to light should be fine. To integrate it in our system, we will also need another 10K ohms resistor.

Software requirements

You don’t need anything more than the Arduino IDE installed on your computer, which you can find at the following link:

http://arduino.cc/en/Main/Software

Hardware configuration

You first have to plug in the LED. Just connect a wire from the Arduino pin number 2 to the 470 ohms resistor, then to the anode of the LED (the long part), then another wire from the cathode to the ground.

For the photocell, we already said it basically behaves like a resistor. What we will use here is a simple circuit called a voltage divider, which is commonly used in electronics. This is the schematic of a voltage divider:


With this circuit, the output voltage is directly proportional to the input voltage, with a factor of R1/(R1+R2). In our case, we will use the photocell to replace the resistor R1, and a fixed 10K ohms resistor to replace R2. By doing this, we obtain a voltage divider which is dependent of the ambient light through the photocell. Finally, we just have to measure Vout using the Arduino analog input to measure the ambient light level. This is the final schematic of this project:


Testing individual components

I won’t go over again the test of the LED, as there are already plenty examples for this on this website. We basically just want to see if the photocell is working correctly. For this, we just have to read out the value of the analog pin 0 using the following code:

// 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);
}

You can upload this code to the Arduino board, and then open the serial monitor. Then, you can play with the photocell by putting your hand around it to simulate the “dark” condition. Here is the output with my photocell:


Your task here is to note the values corresponding to the complete darkness and to bright light. In my case, it was around 329 for complete darkness, and 838 for bright light. Write down these values, we will use them later.

Putting it all together

Now that the photocell is working correctly, we can build the code for the project. The first thing we need to do is to define thresholds to switch the lights on or off:

int lowThreshold = 500;
int highTreshold = 600;

These values will depend on what you recorded at the previous paragraph, feel free to adapt them to your own recordings. At this point, you can ask yourself why we defined two thresholds, and not just one value where we will switch the lights on or off. The reason is quite simple: if we defined only one value of the threshold, then when we are around the threshold value there is a good chance that the lights would oscillate between the on and off states.

Again, we get the value of the ambient light via:

int sensorValue = analogRead(sensorPin);

Then, if the ambient light is low, we compare the value of the analog reading to the low threshold and we switch the lights on:

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

And if the ambient light is high, we compare the value of the analog reading to the high threshold we switch the lights off:

You can now play with the project by putting your hand around the sensor to switch the LED on. Of course, for a final project the photocell has to be placed outside, and the output that connects to the LED has to be connected to a relay that control your lights for example.

Here is a summary of all the components you will need for this project:

This is the end of this article, hoped you enjoyed it, as usual don’t hesitate to leave a comment and to share it !