Last time I talked about modifying the LED decoration and creating my own LED string. Today I’ll talk about connecting a photo cell to an Arduino and take timed measurements of outdoor light. Based on the measurements from the photo cell, an ideal value is determined to switch the light automatically.
Wiring the photo cell
Wiring the photo cell is fairly easy. There are many blogs covering the exact wiring and I think the best example is Adafruits learning systems guide. A pull-down resistor is used for detecting more differences in darkness. When more differences in brightness are needed, use a lower value for the pull-down resistor.
I also added my micro-SD expansion bord so I could take timed measurements and save them in a text file on the SD card. Resulting in an automated system that measures the light outside and saves the value in a text file.
The Arduino code for the photo cell
Using my Arduino Mega 2560, I came up with the following code.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include #include // Set up the file // AnalogPin for the Photo cell // SDcardPin also needs to be defined for the library File myFile; int AnalogPin = 0; int SDcardPin = 53; // Timer, the interval is set at 2min, no delay's here! unsigned long lastTime; unsigned long interval = 120000; unsigned long counter = 0; // Setup, define the SD card and set the Time void setup() { pinMode(SDcardPin, OUTPUT); SD.begin(SDcardPin); // Change this to your current system time right before compiling it to the Arduino // This will make sure the time stamps are correct! setTime(16, 10, 00, 14, 8, 2012); } void loop() { // Get current millis(); unsigned long currentTime = millis(); // Check if its already time for our process if((currentTime - lastTime) > interval) { lastTime = currentTime; // make a data string containing the log String datastring=""; // We read the analog pin int sensor = analogRead(AnalogPin); // Create the datastring with a counter, the time and the value datastring = String(counter) + "," + digitalClockDisplay() + "," + String(sensor); // Open the file for writing myFile = SD.open("light.txt", FILE_WRITE); if(myFile) { myFile.println(datastring); // Close the file so changes are saved myFile.close(); } counter++; } } // Function so 9 minutes gets displayed as 09 String printDigits(int digits) { String result = ""; if(digits < 10) { result = "0" + String(digits); } else { result = String(digits); } return result; } // Gives back the time using the time lib String digitalClockDisplay() { // digital clock display of the time String time = String(year()) + "-" + String(month()) + "-" + String(day()) + " " + printDigits(hour()) + ":" + printDigits(minute()) + ":" + printDigits(second()); return time; } |
The code will take a light measurement, using the photo cell, every 2 minutes. It will then create a string and save the value. The time library is used for adding time information to the measurements. The time is set right before the code is compiled and uploaded to the Arduino, so there is no need for a internet connection or advanced time system. It’s not 100% accurate but does the job wonderfully. Also note that I did not use delay’s, but rather a timer function, delay’s should be avoided at any cost!
The result of the photo cell measurements
I did the measurements 2 times, always starting around the same time, leaving it on for the night and shutting it down in the morning. I exported the text files as CSV files in Excel and then plotted the result.
The first day we see a clear spike in the middle of the night, there was a car turning and the lights clearly spiked my photo cell. Around 21:00 I felt that it was time to turn on the lights, this time corresponds with a value of 350. This value can be used to turn on my lights automatically when its dark enough in the room.
Next time I’ll add bluetooth support to check if a person is in the room or not. Quite important not to switch the lights when I’m not at home.





Pingback: Light automation project - Part 1 | Fritz-Hut