Improved Arduino LED lighting code
Planted Tank Forums
Your Tanks Image Hosting *Tank Tracker * Plant Profiles Fish Profiles Planted Tank Guide Photo Gallery Articles

Go Back   The Planted Tank Forum > Specific Aspects of a Planted Tank > DIY


Reply
 
Thread Tools Display Modes
Old 09-06-2011, 11:49 PM   #1
sink
Algae Grower
 
PTrader: (2/100%)
Join Date: Jun 2011
Location: Boston, MA
Posts: 147
Default

Improved Arduino LED lighting code


I wasn't happy with any existing Arduino code for LED control so I wrote my own. I think it's quite nice though it needs polishing and probably some features added. It does light timing, sunrise/sunset fading and all that jazz.

Some improvements over existing code:
  • Shorter and simpler
  • Accurately set clock from a script on your PC. You don't need to modify any code or upload any sketches!
  • Much, much smoother dimming, especially at the low end (due to a combination of higher resolution PWM output and every-second dimming adjustment instead of every-minute).
  • Higher resolution PWM also means you can dim your LEDs down to a lower intensity than before if your driver is capable. I've found I can dim my LEDs down far enough to be used as moonlights.
  • Flexible timing logic. You can do anything you want (with minor modifications) so long as it's on a 24hr cycle.
There are also some limitations:
  • Your Arduino needs to have either a DS3231(eg: ChronoDot) or DS1307 Real Time Clock chip connected via I2C (because I have one of those). These are probably the two most common RTC chips.
  • It only provides 2 PWM outputs (because I only have 2 LED strings). Of course you can modulate as many strings as you want from a single PWM signal but they'll modulate in unison.
  • It should work with the Arduino Duimilanove/Uno and probably any other micro-controller that uses the Atmega168/328 chip. In particular it will not work as-is with the Arduino Mega due to a difference in the onboard timers.
  • It depends on a few libraries but I've bundled them up in a download link below.
  • Only compiles with versions 0022/0023 of the Arduino IDE, not version 1.0 (yet!)

It works great for me. I'd love feedback, error reports, suggestions, free beers, etc. Post in the thread or PM me.

I hope this will be useful to people!

Get the code:
Latest version
Libraries bundle

Installing libraries:
The libraries that don't come with the Arduino environment by default are bundled in the zipfile above. Just extract that zip into the libraries folder inside your sketchbook folder and then restart the Arduino IDE. If libraries doesn't exist, create it first.

Until I get around to writing documentation just post or PM installation questions/issues and I'll try to help.

Latest Version:
12/11/2011 (only documentation changes, no need to upgrade!)

Last edited by sink; 12-11-2011 at 04:46 PM..
sink is offline   Reply With Quote Quick reply to this message
Sponsored Links
Advertisement
 
Old 09-07-2011, 12:34 AM   #2
jcgd
Wannabe Guru
 
jcgd's Avatar
 
PTrader: (6/100%)
Join Date: Feb 2004
Location: Calgary, Alberta, Canada
Posts: 1,985
Send a message via MSN to jcgd
Default

I would try it. But I'm not sure how to hook up my rtc. Could you e plain how or link me to a deceit walkthrough?
jcgd is offline   Reply With Quote Quick reply to this message
Old 09-07-2011, 01:01 AM   #3
sink
Algae Grower
 
PTrader: (2/100%)
Join Date: Jun 2011
Location: Boston, MA
Posts: 147
Default

Quote:
Originally Posted by jcgd View Post
I would try it. But I'm not sure how to hook up my rtc. Could you e plain how or link me to a deceit walkthrough?
What does the RTC circuit that you have look like? Is it a breakout board? Mine is integrated into my driver because I built it myself, but the principle is the same either way. You just need to power and ground it somehow (5V and GND from the Arduino) and then connect the data pins (SCL and SDA) from the chip to the Arduino. On most Arduino boards (including mine) SDA (data line) is on analog pin 4, and SCL (clock line) is on analog pin 5.

Here's a tutorial that might be applicable Adafruit also sells an RTC breakout board kit with everything you need except wires for under $10. I don't have one but I modeled my RTC circuit after theirs and it works great, so might be an option if you want to add an RTC to an existing setup.
sink is offline   Reply With Quote Quick reply to this message
Old 09-07-2011, 03:53 PM   #4
O2surplus
Planted Tank Enthusiast
 
O2surplus's Avatar
 
PTrader: (4/100%)
Join Date: Jan 2010
Location: yorba linda, ca.
Posts: 538
Default

I'll give it a try! Took me nearly an hour to hunt down and install the extra libraries, but I got the code to compile without issue. I'll hook up the Dueminlanove this weekend and see what happens. Good job sink- Now if you could only show me how to integrate that coding into the controller that I built, that would be awesome!
O2surplus is offline   Reply With Quote Quick reply to this message
Old 09-07-2011, 05:31 PM   #5
sink
Algae Grower
 
PTrader: (2/100%)
Join Date: Jun 2011
Location: Boston, MA
Posts: 147
Default

Quote:
Originally Posted by O2surplus View Post
I'll give it a try! Took me nearly an hour to hunt down and install the extra libraries, but I got the code to compile without issue. I'll hook up the Dueminlanove this weekend and see what happens. Good job sink- Now if you could only show me how to integrate that coding into the controller that I built, that would be awesome!
You are welcome. Glad to know it compiles. Once past that step it should run fine. Please let me know what you think! As an aside, you'll probably want to change a couple of variables in the code to match your own needs regarding sunrise/sunset time and in particular how bright you want daylight to be because my default may be quite dim for others. The lines are all towards the beginning of the file and are commented pretty well as to what they do and possible values. I'll write up more detailed instructions when I have some time.

I'd love to make a bundle with the required libraries but I'm not sure if the licenses permit it. In the meantime I'll edit the original post with links to the needed libraries. Thanks for pointing out that task might not be trivial.

I'm willing to help with any driver integration you need. Do you mean the RTC or the code?
sink is offline   Reply With Quote Quick reply to this message
Old 09-07-2011, 06:15 PM   #6
O2surplus
Planted Tank Enthusiast
 
O2surplus's Avatar
 
PTrader: (4/100%)
Join Date: Jan 2010
Location: yorba linda, ca.
Posts: 538
Default

Sink,

I found the TimerOne and Time libraries at Arduino.cc, so I think you could zip them up together with your code and be good to go. There's one thing you need to fix though, your code cannot be unzipped and moved directly into the Arduino IDE. The IDE will not recognize the folder until it is renamed. I had to change the name of the folder to "akl_tank_control" and the file name to "akl_tank_control.pde" and then it would open and compile properly. This is detail that may cause trouble for people who are not familiar with some of the Arduino IDE's file naming requirements.

I'll have to shoot you a PM with the coding for my controller. It works great as is, but I like the fact that your code updates the PWM every second, instead of once per minute. I've got the hardware side of things nailed down but I could use your help to improve the software.

Here's a photo of the controller board- without the 16x2 LCD on top.



I've got enough boot loaded ATMEGA328's to build 30 more, and a newer design that supports the MeanWell ELN-P's as well as my Cat4101's.

Maybe a trade- Chips or a completed controller for software?
O2surplus is offline   Reply With Quote Quick reply to this message
Old 09-07-2011, 07:29 PM   #7
sink
Algae Grower
 
PTrader: (2/100%)
Join Date: Jun 2011
Location: Boston, MA
Posts: 147
Default

Quote:
Originally Posted by O2surplus View Post
I found the TimerOne and Time libraries at Arduino.cc, so I think you could zip them up together with your code and be good to go. There's one thing you need to fix though, your code cannot be unzipped and moved directly into the Arduino IDE. The IDE will not recognize the folder until it is renamed. I had to change the name of the folder to "akl_tank_control" and the file name to "akl_tank_control.pde" and then it would open and compile properly. This is detail that may cause trouble for people who are not familiar with some of the Arduino IDE's file naming requirements.
Clearly I too am unfamiliar with the IDE's file naming requirements . I just uploaded a version that should be better in that regard. I also bundled the libraries up and added a link above. Thanks much for the input.

Quote:
I'll have to shoot you a PM with the coding for my controller. It works great as is, but I like the fact that your code updates the PWM every second, instead of once per minute. I've got the hardware side of things nailed down but I could use your help to improve the software.

I've got enough boot loaded ATMEGA328's to build 30 more, and a newer design that supports the MeanWell ELN-P's as well as my Cat4101's
Driver looks great! I'd love to help on the software end.

Quote:
Maybe a trade- Chips or a completed controller for software?
Sounds very reasonable. I do love the barter system. I'll shoot you an email
sink is offline   Reply With Quote Quick reply to this message
Old 09-09-2011, 01:55 AM   #8
O2surplus
Planted Tank Enthusiast
 
O2surplus's Avatar
 
PTrader: (4/100%)
Join Date: Jan 2010
Location: yorba linda, ca.
Posts: 538
Default

Sink,

I loaded the code into my Duemilanove and connected it up to my Cat4101's, Looks good so far. I made some changes to the code to run the leds at max (1023) all day,and set the night time level to a minimum (20) for moon lighting at night. I think a pwm value of 20 works out to be a hair less than 2%. I should know by 10pm whether or not the leds will continue to run at such a low setting.
I did notice that there's something amiss with the time display in the serial monitor. The time displayed is 1 hour less than the correct time on my PC. Any idea what's causing that? Must be something to do with the DS1307 and daylight savings time?
O2surplus is offline   Reply With Quote Quick reply to this message
Old 09-09-2011, 02:34 AM   #9
sink
Algae Grower
 
PTrader: (2/100%)
Join Date: Jun 2011
Location: Boston, MA
Posts: 147
Default

Quote:
Originally Posted by O2surplus View Post
Sink,

I loaded the code into my Duemilanove and connected it up to my Cat4101's, Looks good so far. I made some changes to the code to run the leds at max (1023) all day,and set the night time level to a minimum (20) for moon lighting at night. I think a pwm value of 20 works out to be a hair less than 2%. I should know by 10pm whether or not the leds will continue to run at such a low setting.
Your LEDs should run all the way down to a setting of 1 without any problems. The CAT4101s OTOH don't seem to have particularly precise dimming below about 1% though it will still work. I've lowered the default PWM frequency from 500Hz to 150Hz in an effort to improve that so I'm curious to see how your results are.

Quote:
I did notice that there's something amiss with the time display in the serial monitor. The time displayed is 1 hour less than the correct time on my PC. Any idea what's causing that? Must be something to do with the DS1307 and daylight savings time?
That's an interesting one. The DS1307 is completely naive of timezones so the issue would be with the time your computer is putting out somehow not compensating for DST. I presume you set the clock using the timerset.pde sketch?
sink is offline   Reply With Quote Quick reply to this message
Old 09-09-2011, 02:48 AM   #10
O2surplus
Planted Tank Enthusiast
 
O2surplus's Avatar
 
PTrader: (4/100%)
Join Date: Jan 2010
Location: yorba linda, ca.
Posts: 538
Default

Quote:
Originally Posted by sink View Post
That's an interesting one. The DS1307 is completely naive of timezones so the issue would be with the time your computer is putting out somehow not compensating for DST. I presume you set the clock using the timerset.pde sketch?
Oh- I forgot about that separate sketch. My DS1307 must still be running from the last time that I set it. That says a lot since it's been out in a drawer collecting dust for the last six months,LOL. I guess the back up battery must have kept it alive.
I'll have to try that sketch. I just adjusted the start and stop time in the main sketch to compensate for now. hehe!
O2surplus is offline   Reply With Quote Quick reply to this message
Old 09-09-2011, 02:58 AM   #11
sink
Algae Grower
 
PTrader: (2/100%)
Join Date: Jun 2011
Location: Boston, MA
Posts: 147
Default

Quote:
Originally Posted by O2surplus View Post
Oh- I forgot about that separate sketch. My DS1307 must still be running from the last time that I set it. That says a lot since it's been out in a drawer collecting dust for the last six months,LOL. I guess the back up battery must have kept it alive.
I'll have to try that sketch. I just adjusted the start and stop time in the main sketch to compensate for now. hehe!
I figured that was the case . My fault for not getting around to writing proper documentation yet. As an aside, the timeset.pde isn't specific to the lighting control code in any way. It's just a convenient way to easily set the RTC time. It'll work for any Arduino/DS1307 pair.

On battery power the DS1307 shuts down all functions except for keeping the clock running so it can run for 5+ years on a single coin cell. Pretty nifty.
sink is offline   Reply With Quote Quick reply to this message
Old 09-09-2011, 04:28 PM   #12
Tbakes
Algae Grower
 
Tbakes's Avatar
 
PTrader: (4/100%)
Join Date: Mar 2011
Location: Schwenksville, PA
Posts: 77
Default

Very cool - I'd like to upgrade my controller with the new code once it is worked out!
Tbakes is offline   Reply With Quote Quick reply to this message
Old 09-09-2011, 04:54 PM   #13
O2surplus
Planted Tank Enthusiast
 
O2surplus's Avatar
 
PTrader: (4/100%)
Join Date: Jan 2010
Location: yorba linda, ca.
Posts: 538
Default

Quote:
Originally Posted by Tbakes View Post
Very cool - I'd like to upgrade my controller with the new code once it is worked out!

Well, we'll have to see what "coding magic" "Sink" can pull off! LOL Then I'll show you how to reprogram the controller. He's got a day job- so let's not rush him.LOL
O2surplus is offline   Reply With Quote Quick reply to this message
Old 09-10-2011, 06:38 AM   #14
O2surplus
Planted Tank Enthusiast
 
O2surplus's Avatar
 
PTrader: (4/100%)
Join Date: Jan 2010
Location: yorba linda, ca.
Posts: 538
Default

Sink,

I uploaded the TimeSet code.....Worked perfectly. I also uploaded the TankControl code again after lowering the minimum PWM state to "5" and stting the fade duration to 1 hour. All I can say is "WOW!" The dimming is completely linear, seamless, and totally natural looking. That has got to be the best Dimming Simulation that I've ever seen! I now have 90 little "moon lights" over my tank. Too cool! Thanks so much for creating and posting the codes!
O2surplus is offline   Reply With Quote Quick reply to this message
Old 09-10-2011, 06:49 AM   #15
jcgd
Wannabe Guru
 
jcgd's Avatar
 
PTrader: (6/100%)
Join Date: Feb 2004
Location: Calgary, Alberta, Canada
Posts: 1,985
Send a message via MSN to jcgd
Default

Wow, you are getting me excited to download this to my arduino. I need to get that RTC installed.
jcgd is offline   Reply With Quote Quick reply to this message
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the The Planted Tank Forum forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in

Human Verification

In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.



Thread Tools
Display Modes

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:09 AM.


Powered by vBulletin®
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright Planted Tank LLC 2012