loading
The wife is into raising chickens and now that the daylight savings is getting near she wanted a way to keep the egg laying production in high gear.  According to the material that she reads, chickens will maintain their egg laying production if they get 12 – 15 hours of daylight.

I thought to myself, what a great opportunity for me to put together a DIY electronics project that would also be green.  I wanted a quick and inexpensive solution so I decided to go with the Arduino as the MCU and build everything from scratch.
This document will document my project from beginning to end.

I had picked up a battery powered LED light that would be great for this project.

I have a bunch of ATMega chips lying around as well as a few Arduino clone boards, so I was well on to my way of building this project with material and parts that are in my parts bins in my shop.

I started to think about what kind of design I wanted and decided to go simple, lean and mean.
I have done some work with latching relays in the past and really like the idea of using the MCU to latch the relay on and off without having to keep constant power on the ATMega ports all of the time. 

The chicken coop is located in an area where there is no easy access to AC power and I wanted a battery solution that I could, at a later date, add solar panel access for charging.
First off I had to determine the volts and amp requirements of the LED light.

I took the LED light apart and soldered some wires to the existing switch circuit so I could trigger the light with the relay.
The LED light consumed 6 volts at around 500 milliamps.

D-cell batteries originally powered the LED light.  I did not want two different battery sources and I did not want to change batteries all of the time.  So I decided on a single battery source to power the MCU and LED light.

Below is image of LED light mounted in Chicken coop, two wires for switch and two wires for power:

Step 1: Breadboard

Once I decided on a circuit I laid out the circuit on a breadboard using an Arduino clone as the microcontroller.  This way I could test the circuit using components that I knew worked and gave me a stable platform to write the code.

I also do not like to power external devices directly from the microcontroller ports so I decided to use some NPN transistors as part of the relay circuit.

To provide an accurate time source for the relay timer I decided to use a DS1307 RTC, Real Time Clock chip.  LadyAda has a really good write up on using this chip and has created her own code branch of JeeLab’s RTCLib library.

Here are the links to the web sites:

http://www.ladyada.net/learn/breakoutplus/ds1307rtc.html

https://github.com/adafruit/RTClib

The breadboard shows a SparkFun DS1307 RTC, I have several of these that I use for testing purposes.

But the production version of the Light Relay board uses the same RTC board that LadyAda documents in her writeup.

The Arduino clone pictured is the Diavolino from Evil Mad Science Labs.  Nice board and very inexpensive.  I use this board as one of my prototype boards.

Since I needed two different voltages for this configuration, 5 volts for the microcontroller and 6 volts for the LED light, I decided to use two linear power regulators.  For low cost I decided to use 7805 and 7806 regulators to drive the microcontroller and the LED light.

Since I wanted to keep the price low, I decided to build the production version of the circuit board on solder based breadboards.

After building this board, I learned a lot of lessons, In the future, I more than likely take the time to build my own custom PCB boards. 

Building the solder breadboard was very time consuming and tedious.


<p>Hi Joe, thanks for this post. Been looking for a timer code and your code is by far the most elegant and efficient I have seen! really cool. My project is a bit different (I would like to control more lights and I will not use latching relays)</p><p>I would like to use yours (giving you credit) as a starting point and add some arrays for controlling more than one light but before getting there, I went ahead and modified the part for latching relays. I also modified the second IF statement in a way I hope will turn/keep the lights ON no matter when the program starts.</p><p>Because I am not a programmer and dont have all the hardware yet, I was wondering if you could take a look at my mods and let me know what you think.</p><p>I am not sure how to publish code here, so I will just copy paste it.</p><p>Thanks much in advance and again, thanks a lot for sharing this.</p><p>// Program .....: RTC_Relay_DHMS</p><p>// Author ......: Joe Pitz, Objetek Systems, objetek@gmail.com</p><p>// Copyright ...: Creative Commons, CC BY-SA </p><p>// Description .: Turn on and off a latching relay using a ATMega 328P</p><p>// Date Created.: 10/30/2011</p><p>// Usage and dependencies:</p><p>//</p><p>// Variables are used to set relay start time, Duration of relay on time</p><p>// is performed by setting day, hour, minute and second variables.</p><p>//</p><p>// Times are based on 24 hour clock, i.e.: </p><p>// 1:00 pm = 13:00 and Midnight is 00:00</p><p>//</p><p>// Duration can exceed 24 hour period.</p><p>//</p><p>// Code uses RTCLIB, a branch provided from:</p><p>// https://github.com/adafruit/RTClib</p><p>//</p><p>// Thanks to LadyAda for the great tutorial on DS1307</p><p>// http://www.ladyada.net/learn/breakoutplus/ds1307rtc.html</p><p>//</p><p>#include &lt;Wire.h&gt;</p><p>#include &quot;RTClib.h&quot;</p><p>RTC_DS1307 RTC;</p><p>// Setup Start and End Times</p><p>// This is where you set your relay start time</p><p>int StartHr = 16;</p><p>int StartMin = 13;</p><p>// Sleep is a measure of minutes and must be greater than 0</p><p>// is used to pause the loop() function. If you are checking </p><p>// for a start time of hours and minutes you do not want to use </p><p>// up processor cycles by checking the time every second. </p><p>// every minute or so is good unless you are performing a more </p><p>// frequent activity. In this program we are turning on a </p><p>// relay for a period of time every day, so minutes are fine for </p><p>// a sleep delay.</p><p>long Sleep = 1L;</p><p>// The delay() function takes a argument of milliseconds so</p><p>// we need a multiplier of 60,000 in order to convert our delay </p><p>// variable (sleep) to milliseconds. So a delay of 1 minute </p><p>// (1 * 60,000) = 60,000 milliseconds. </p><p>long MultiMinute = 60000L;</p><p>boolean LightOn = false;</p><p>// Relay will stay latched on until current time</p><p>// matches duration variables</p><p>// If duration is 1 day and 3 hours then</p><p>// relay will stay latched on 1 day and 3 hours from the start time</p><p>DateTime future;</p><p>DateTime DelayFuture;</p><p>DateTime Start;</p><p>int DurDay = 0;</p><p>int DurHour = 0;</p><p>int DurMinute = 1;</p><p>int DurSecond = 0;</p><p>// ledPin used for debug using Arduino on board LED.</p><p>int ledPin = 13;</p><p>// Pins used to control latching relay</p><p>//int RelayOn = 6; no latching relay</p><p>//int RelayOff = 7; no latching relay</p><p>// Pin used to control relay</p><p>int relayPin = 52;</p><p>void setup () {</p><p> Serial.begin(57600);</p><p> Wire.begin();</p><p> RTC.begin();</p><p> pinMode(relayPin, OUTPUT); // Set pin connected to Relay as an OUTPUT</p><p> RTC.adjust(DateTime(__DATE__, __TIME__));</p><p> if (! RTC.isrunning()) {</p><p> Serial.println(&quot;RTC is NOT running!&quot;);</p><p> // following line sets the RTC to the date &amp; time this </p><p> // sketch was compiled</p><p> RTC.adjust(DateTime(__DATE__, __TIME__)); </p><p> }</p><p> // Start the StartHr and StartMin for Relay times</p><p> DateTime now = RTC.now();</p><p> DateTime SetStart(now.year(),now.month(),now.day(),StartHr,StartMin,</p><p> now.second());</p><p> Start = SetStart;</p><p>}</p><p>void loop () {</p><p> DateTime now = RTC.now();</p><p> Serial.print(now.year(), DEC);</p><p> Serial.print('/');</p><p> Serial.print(now.month(), DEC);</p><p> Serial.print('/');</p><p> Serial.print(now.day(), DEC);</p><p> Serial.print(' ');</p><p> Serial.print(now.hour(), DEC);</p><p> Serial.print(':');</p><p> Serial.print(now.minute(), DEC);</p><p> Serial.print(':');</p><p> Serial.print(now.second(), DEC);</p><p> Serial.println();</p><p> // Check status of light</p><p> if (LightOn == false)</p><p> {</p><p> // Calculate current time + delay minutes, (Sleep variable time)</p><p> // to give a wide window, making sure we can hit the start time</p><p> // if you start the program after this window the relay will </p><p> // not start until the StartHr and StartMin comes around </p><p> // the next day.</p><p> // set future DateTime, used to determine duration for </p><p> // light on time</p><p> future = CalcFuture(now,DurDay,DurHour,DurMinute,DurSecond); </p><p> // MOVED from if below to use for the new if calc</p><p> // is there a problem having this outside the old if below?</p><p> //DelayFuture = CalcFuture(Start,0L,0L,Sleep,0L); I dont think I will need this anymore</p><p> if ((int)now.day() &lt;= (int)future.day() &amp;&amp; (int)now.hour() &gt;= StartHr &amp;&amp; </p><p> (int)now.hour() &lt;= (int)future.hour() &amp;&amp; (int)now.minute() &gt;= StartMin &amp;&amp;</p><p> (int)now.minute() &lt;= (int)future.minute())</p><p> //modified this if to make sure the light is ON at any point</p><p> //during the time it is supposed to be ON no matter when the</p><p> //program starts. Example if power goes down and back on.</p><p> //also added now.day &lt;= future.day at beginning in case power </p><p> //goes down after midnight</p><p> { </p><p> // turn on light</p><p> LightOn = true;</p><p> //digitalWrite(RelayOn, HIGH); no latching relay</p><p> digitalWrite(relayPin, HIGH);</p><p> Serial.print(&quot;\r\nLight On\r\n&quot;); </p><p> // only need a short time to trigger the latching relay</p><p> //then we can turn the port back off</p><p> //delay(1000); no latching relay</p><p> //digitalWrite(RelayOn, LOW); no latching relay</p><p> }</p><p> }</p><p> else</p><p> {</p><p> // Check current time, turn off light when conditions are met</p><p> if ((int)now.day() &gt;= (int)future.day() &amp;&amp; (int)now.hour() &gt;= </p><p> (int)future.hour() &amp;&amp; (int)now.minute() &gt;= (int)future.minute())</p><p> {</p><p> LightOn = false;</p><p> // turn light off</p><p> //digitalWrite(RelayOff, HIGH); no latching relay</p><p> digitalWrite(relayPin, LOW);</p><p> Serial.print(&quot;\r\nLight Off\r\n&quot;); </p><p> // only need a short time to trigger the latching relay</p><p> //then we can turn the port back off</p><p> //delay(1000); no latching relay</p><p> //digitalWrite(RelayOff, LOW); no latching relay</p><p> } </p><p> } </p><p> // for debugging purposes go ahead and set your delay(3000) so </p><p> // you can debug quickly then for production you can change </p><p> // your delay time to the calculation.</p><p> // delay(3000); </p><p> delay((Sleep*MultiMinute));</p><p>} </p><p>// CalcFuture() uses unixtime() to calculate proper DateTime for </p><p>// passed days, hours, minutes and seconds </p><p>DateTime CalcFuture(DateTime now, int Days, int Hours, int Minutes, </p><p> int Seconds)</p><p>{</p><p> DateTime future;</p><p> long DaySeconds = 86400L;</p><p> long HourSeconds = 3600L;</p><p> long MinuteSeconds = 60L;</p><p> future = (now.unixtime() + (Days * DaySeconds) + </p><p> (Hours * HourSeconds) + (Minutes * MinuteSeconds) +</p><p> Seconds);</p><p> return future;</p><p>}</p>
<p>buying a timer from Home Depot, requires no skill, designing a custom board and programming the logic does require skill. Go troll someone else.</p>
Great project. With regard to the 5 and 6 volt PSU, ofcourse it is all not expensive, but you could also decide to just run the LED's off of 5 V, or make a 6 V supply and feed the Arduino through a diode.<br><br>I do not know the exact specs of the Diavolino, but many Arduino boards have their own 5V stabilisre and will accept &gt; 5 volt.<br><br>Nevertheless, as said, adding another voltage to yr PSU will be peanuts in terms of cost<br>
Thanks diy_bloke for the tips on the PSU. I have been thinking of switching to a switching psu, but in the cold weather I guess the extra heat will keep the battery case warm. <br><br>Joe
Nice project. How long have you had this installed and have you noticed any results yet?
Hello randofo,<br><br>We just put the light in the coop. I have not noticed any difference as of yet.<br>But I did go down to the coop early yesterday morning when it was dark and the rooster was crowing.<br><br>Thanks<br><br>Joe
This is very impressive. I have been interested in reading about electronic circuits for a long time, but never got into anything like this. I am doing well if I can understand a transistor amplifier. Did you learn some of this in your training as a software developer or elsewhere?
Hello Phil,<br><br>I actually started playing around with electronics as a means to improve my software development skills. I enjoy working on projects where software communicates with hardware.<br><br>I just started reading some electronics web sites and books. Earlier this year I finished reading The Make Electronics book. <br><br>Great fun.<br><br>Thanks for the positive comments.<br><br>Joe

About This Instructable

16,187views

42favorites

License:

Bio: Software Developer, like to work with electronics, embedded systems, robots etc.
More by jpitz31:C Library for HD44780 LCD Display Controller Fix your NordicTrack Exercise Bike  Make:it Robotics Starter Kit - Sending Sensor Data Wirelessly 
Add instructable to: