This project is no longer supported. Please visit the latest version: Aquarium PWM LED V2
This project came up on my mind when I realized the exorbitant price of an aquarium LED light controller. The starting price for a professional equipment like this one can be as high as 1000€.
So, if you can build your own controller with the exact same functions (or even a better one) why spend a fortune in an equipment that, in my opinion, doesn’t worth what it costs.
For this project I used four LED strings. Each string comprises three LEDs. The LEDs have a forward voltage around ~4.0V and forward current around ~750mA meaning that a IRF3205 MOSFET is enough to switch each string. These LEDs are known as 3W High Power LED.
My estimative for a project with twelve 3 Watt LEDs is around 50 EUR (this value includes all electronics LEDs and driver).
To build this circuit you need the following items:
– 1x Arduino Nano 3.0
– 1x DS1307 real-time clock module
– 1x Hitachi HD44780 20×4 LCD
– 1x 74HC595 shift register
– 1x LM35 temperature sensor
– 4x IRF3205 N-Channel Power MOSFETs
– 3x 10kOhm resistors
– 1x 10kOhm trimmer
– 2x Push-buttons
– 12x 3 Watt LEDs
– 1x 12V 40 Watt LED driver
– Breadboard and wires
Software:
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 |
/***************************************************************************** * Copyright (C) 2012-2014 by Vasco Ferraz. All Rights Reserved. * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see <http://www.gnu.org/licenses/>. * * * * Author: Vasco Ferraz * * Contact: http://vascoferraz.com/contact/ * * Description: http://vascoferraz.com/projects/aquarium-pwm-led/ * *****************************************************************************/ //#include <Servo.h> <-- If you include Servo.h, which uses timer1, ShiftPWM will automatically switch to timer2 // Clock and data pins are pins from the hardware SPI, you cannot choose them yourself. // Data pin is MOSI (Uno and earlier: 11, Leonardo: ICSP 4, Mega: 51, Teensy 2.0: 2, Teensy 2.0++: 22) // Clock pin is SCK (Uno and earlier: 13, Leonardo: ICSP 3, Mega: 52, Teensy 2.0: 1, Teensy 2.0++: 21) // You can choose the latch pin yourself. const int ShiftPWM_latchPin=8; // ** uncomment this part to NOT use the SPI port and change the pin numbers. This is 2.5x slower ** //#define SHIFTPWM_NOSPI //const int ShiftPWM_dataPin = 11; //const int ShiftPWM_clockPin = 13; // If your LED's turn on if the pin is low, set this to true, otherwise set it to false. const bool ShiftPWM_invertOutputs = false; // You can enable the option below to shift the PWM phase of each shift register by 8 compared to the previous. // This will slightly increase the interrupt load, but will prevent all PWM signals from becoming high at the same time. // This will be a bit easier on your power supply, because the current peaks are distributed. const bool ShiftPWM_balanceLoad = false; #include <Wire.h> // I2C and TWI library #include <LiquidCrystal.h> #include <RTClib.h> #include <ShiftPWM.h> // Include ShiftPWM.h after setting the data, clock and latch pins! #include <EEPROM.h> // The microcontroller on the Arduino board has an EEPROM: memory whose values are kept when the board is turned off // (like a tiny hard drive). This library enables you to read and write those bytes. // The microcontrollers on the various Arduino boards have different amounts of EEPROM: 1024 bytes on the ATmega328P, // 512 bytes on the ATmega168 and ATmega8 and 4 kB (4096 bytes) on the ATmega1280 and ATmega2560. #define DS1307_I2C_ADDRESS 0x68 // Each I2C object has a unique bus address, the DS1307 (Real Time Clock) is 0x68 RTC_DS1307 RTC; LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // Create custom chars byte degree_char[8] = { 0b01110, 0b10001, 0b10001, 0b01110, 0b00000, 0b00000, 0b00000, 0b00000 }; byte upbar_char[8] = { 0b11111, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000 }; // Variables used to measure the temperature const int analogtemp=6; // This is the analog pin which is measuring the input voltage from the LM35CAZ temperature sensor double temp=0, Vin=0, ADCvar; unsigned int j=0, k=0; const double Vref=1100.0; const int ClockMode = 1; // Pushbutton to switch between modes unsigned char ClockModeState = 0; // Current LCD mode state unsigned char ClockModeFlag = 0; // Flag used for debouncing the ClockMode pushbutton const int SetClockPlus = 2; // Increment pushbutton to set the clock unsigned char second, minute, hour, dayOfWeek, day, month, year; unsigned char presunrise; // Hour when the pre-sunrise will start unsigned char sunrise; // Hour when the sunrise will start unsigned char sunset; // Hour when the sunset will start unsigned int presunriseMemoryBank = 0; // This is the position on ATmega328P's EEPROM where the hour of the pre-sunrise is stored. unsigned int sunriseMemoryBank = 1; // This is the position on ATmega328P's EEPROM where the hour of the sunrise is stored. unsigned int sunsetMemoryBank = 2; // This is the position on ATmega328P's EEPROM where the hour of the sunset is stored. const unsigned int settingdelay = 200; // The higher this value is, the longer it will take to increment day, month, year, hour, minute, second, pre-sunrise, sunrise and sunset variables. // If there is no input, automatically jump to main display when gotomain = gotomaincounter. Change this time by manipulating the gotomaincouter value. unsigned int gotomain=0; const int gotomaincounter=400; // Convert normal decimal numbers to binary coded decimal byte decToBcd(byte val) {return ( (val/10*16) + (val%10) );} // Convert binary coded decimal to normal decimal numbers byte bcdToDec(byte val) {return ( (val/16*10) + (val%16) );} // Here you set the number of brightness levels, the update frequency and the number of shift registers. // These values affect the load of ShiftPWM. // Choose them wisely and use the PrintInterruptLoad() function to verify your load. unsigned int maxBrightness = 240; // Don't forget that a signed char variable range is from [-128;127] and an unsigned char from [0;255]. unsigned int brightness; // If you want to get an higher resolution, you must set maxBrightness (here) and brightness variables as integers. unsigned int pwmFrequency = 100; unsigned char numRegisters = 1; const int outputEnable = 9; // If this port is LOW, shift register(s) is (are) enable. If this port is HIGH, shift register(s) is (are) disable. // Variables used in Copyright function unsigned char x=0, z=0, flag=0; // Variables used in Brightness and LED_PWM functions int y=-1; unsigned int percent; unsigned char WhiteString1 = 0; unsigned char WhiteString2 = 1; unsigned char BlueString = 2; unsigned char RedString = 3; unsigned char Brightness_WhiteString1 = 0; unsigned char Brightness_WhiteString2 = 0; unsigned char Brightness_BlueString = 0; unsigned char Brightness_RedString = 0; // Variable used to change between operation modes unsigned char operationMode = 0; // Start on normal mode void setup () { Serial.begin(9600); Wire.begin(); RTC.begin(); lcd.begin(20, 4); lcd.createChar(0, degree_char); lcd.createChar(1, upbar_char); analogReference(INTERNAL); pinMode(ClockMode, INPUT); // Initialize the clock mode pushbutton as an input. pinMode(SetClockPlus, INPUT ); // Initialize the increment pushbutton as an input. pinMode(outputEnable, OUTPUT); // Initialize the output enable shift register pin as an output. // Sets the number of 8-bit shift registers that are used. ShiftPWM.SetAmountOfRegisters(numRegisters); // Sets the pwmFrequency and maxBrightness ShiftPWM.Start(pwmFrequency,maxBrightness); // This set of instructions will disable all outputs from all shift registers to prevent the random (or last saved on/off state) activation of the LEDs. digitalWrite(outputEnable, HIGH); ShiftPWM.SetAll(0); delay(10); digitalWrite(outputEnable, LOW); // following line sets the RTC to the date & time this sketch was compiled //RTC.adjust(DateTime(__DATE__, __TIME__)); // If the clock is not running, execute the following code if (! RTC.isrunning()) { lcd.clear(); lcd.home(); lcd.print("Not Running!!"); lcd.setCursor(0,1); lcd.print("Restarting..."); delay(5000); lcd.clear(); // Start ticking the clock Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x00); // move pointer to 0x00 byte address Wire.write(0x00); // sends 0x00. The whole byte is set to zero (0x00). This also means seconds will reset!! Unless you use a mask -> homework :) Wire.endTransmission(); // following line sets the RTC to the date & time to: 2014 August 01 - 00:00:00 RTC.adjust(DateTime(2014,8,1, 0,0,0)); // sequence: year, month, day, hour, minute, second } // Pre-sunrise out of range. If for some reason the stored value (pre-sunrise) in ATmega328P's EEPROM is out of range it will be set to a valid hour presunrise = EEPROM.read(presunriseMemoryBank); if (presunrise >= 6 && presunrise <= 9) {presunrise=presunrise;} else (presunrise = 9); // Default pre-sunrise value EEPROM.write(presunriseMemoryBank,presunrise); // Sunrise out of range. If for some reason the stored value (sunrise) in ATmega328P's EEPROM is out of range it will be set to a valid hour sunrise = EEPROM.read(sunriseMemoryBank); if (sunrise >= 10 && sunrise <= 18) {sunrise=sunrise;} else (sunrise = 14); // Default sunrise value EEPROM.write(sunriseMemoryBank,sunrise); // Sunset out of range. If for some reason the stored value (sunset) in ATmega328P's EEPROM is out of range it will be set to a valid hour sunset = EEPROM.read(sunsetMemoryBank); if (sunset >= 19 && sunset <= 23) {sunset=sunset;} else (sunset = 23); // Default sunset value EEPROM.write(sunsetMemoryBank,sunset); } void loop () { while (ClockModeState == 0) { PrintTimeOnLCD(); PrintDateOnLCD(); PrintWeekDayOnLCD(); PrintTemperatureOnLCD(); PrintCopyrightOnLCD(); ReadTime(); if(!Serial.available()) { Serial.println(operationMode); switch(operationMode) { case 0: LED_PWM(); break; case 1: LED_PWM_Debug(); break; default: LED_PWM(); break; } } if(Serial.available()) { operationMode = Serial.parseInt(); // read a number from the serial port to set the operation mode switch(operationMode) { case 0: LED_PWM(); break; case 1: LED_PWM_Debug(); break; default: LED_PWM(); break; } } PrintBrightnessOnLCD(); SwitchClockMode (1); } while (ClockModeState == 1) { SetHour(); } while (ClockModeState == 2) { SetMinute(); } while (ClockModeState == 3) { SetSecond(); } while (ClockModeState == 4) { SetDay(); } while (ClockModeState == 5) { SetMonth(); } while (ClockModeState == 6) { SetYear(); } while (ClockModeState == 7) { SetPresunrise(); } while (ClockModeState == 8) { SetSunrise(); } while (ClockModeState == 9) { SetSunset(); } } // Print time on LCD void PrintTimeOnLCD (void) { lcd.home(); DateTime now = RTC.now(); if (now.hour() < 10) { lcd.print('0'); lcd.print(now.hour(), DEC); lcd.print(':'); } else { lcd.print(now.hour(), DEC); lcd.print(':'); } if (now.minute() < 10) { lcd.print('0'); lcd.print(now.minute(), DEC); lcd.print(':'); } else { lcd.print(now.minute(), DEC); lcd.print(':'); } if (now.second() < 10) { lcd.print('0'); lcd.print(now.second(), DEC); } else { lcd.print(now.second(), DEC); } } // Print date on LCD void PrintDateOnLCD(void) { DateTime now = RTC.now(); lcd.setCursor(0,1); if (now.day() < 10) { lcd.print('0'); lcd.print(now.day(), DEC); lcd.print('/'); } else { lcd.print(now.day(), DEC); lcd.print('/'); } switch(now.month()) { case 1: lcd.print("Jan/"); break; case 2: lcd.print("Feb/"); break; case 3: lcd.print("Mar/"); break; case 4: lcd.print("Apr/"); break; case 5: lcd.print("May/"); break; case 6: lcd.print("Jun/"); break; case 7: lcd.print("Jul/"); break; case 8: lcd.print("Aug/"); break; case 9: lcd.print("Sep/"); break; case 10: lcd.print("Oct/"); break; case 11: lcd.print("Nov/"); break; case 12: lcd.print("Dec/"); break; default: lcd.print("Err/"); } lcd.print(now.year(), DEC); } // Print weekday on LCD void PrintWeekDayOnLCD(void) { DateTime now = RTC.now(); lcd.setCursor(11, 1); switch(now.dayOfTheWeek()) { case 0: lcd.print(" Sun"); // Noise can write random chars on the LCD. Adding two blank chars before the WeekDay will prevent these random chars. Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x03); Wire.write(decToBcd(0)); Wire.endTransmission(); break; case 1: lcd.print(" Mon"); // Noise can write random chars on the LCD. Adding two blank chars before the WeekDay will prevent these random chars. Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x03); Wire.write(decToBcd(1)); Wire.endTransmission(); break; case 2: lcd.print(" Tue"); // Noise can write random chars on the LCD. Adding two blank chars before the WeekDay will prevent these random chars. Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x03); Wire.write(decToBcd(2)); Wire.endTransmission(); break; case 3: lcd.print(" Wed"); // Noise can write random chars on the LCD. Adding two blank chars before the WeekDay will prevent these random chars. Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x03); Wire.write(decToBcd(3)); Wire.endTransmission(); break; case 4: lcd.print(" Thu"); // Noise can write random chars on the LCD. Adding two blank chars before the WeekDay will prevent these random chars. Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x03); Wire.write(decToBcd(4)); Wire.endTransmission(); break; case 5: lcd.print(" Fri"); // Noise can write random chars on the LCD. Adding two blank chars before the WeekDay will prevent these random chars. Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x03); Wire.write(decToBcd(5)); Wire.endTransmission(); break; case 6: lcd.print(" Sat"); // Noise can write random chars on the LCD. Adding two blank chars before the WeekDay will prevent these random chars. Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x03); Wire.write(decToBcd(6)); Wire.endTransmission(); break; default: lcd.print(" Err"); // Noise can write random chars on the LCD. Adding two blank chars before the WeekDay will prevent these random chars. } } // Print temperature and miliVolt on LCD void PrintTemperatureOnLCD(void) { if (k == 0) { ADCvar=0; Vin=0; temp=0; for(j=0 ; j<=99 ; j++) // Do "j" readings {ADCvar = ADCvar + (analogRead(analogtemp));} // Each sample is a value from 0 to 1023. Reading "j" values will help making the reading more accurate. ADCvar=ADCvar/100; // Calculate the average value from all "j" readings. Vin = ADCvar * (Vref/1024.0); // Here you convert from ADC to milivolt -> Voltage = ADCvar*Vref/1024. ADC = [0;1023] // For a 10-bit ADC with an 1100mV reference, the most you can measure without saturating the ADC would be (1100mV - 1100mV/1024) = 1098.9mV. // In other words, a reading of 1023 from your ADC equals (1023 * 1100/1024mV) equals 1098.9 mV. // Remember, 10 bits can only represent a value as large as b1111111111 or decimal 1023. lcd.setCursor(12, 3); lcd.print(Vin, 2); lcd.print("mV"); // Print miliVolt on LCD temp = (Vin/10.0); // Convert Vin into temperature. if ( (temp >=0) & (temp <10) ) { lcd.setCursor(8, 0); lcd.print(" "); lcd.print(temp, 1); } if ( (temp >=10) & (temp <100) ) { lcd.setCursor(8, 0); lcd.print(" "); lcd.print(temp, 1); } if (temp >=100) { lcd.setCursor(8, 0); lcd.print(" Hi"); } if ( (temp >-10) & (temp <0) ) { lcd.setCursor(8, 0); lcd.print(" "); lcd.print(temp, 1); } if (temp <=-10) { lcd.setCursor(8, 0); lcd.print(" Lo"); } lcd.setCursor(14, 0); lcd.write((uint8_t)0); lcd.print("C"); } k++; if (k >= 120){k=0;} } // Print brightness on LCD void PrintBrightnessOnLCD(void) { y++; if (y == 0) { lcd.setCursor(0, 3); if (Brightness_WhiteString1 >= 0 && Brightness_WhiteString1 < 10) {lcd.print("W1:00"); lcd.print(Brightness_WhiteString1); lcd.print("/"); lcd.print(maxBrightness);} else if (Brightness_WhiteString1 >= 10 && Brightness_WhiteString1 < 100) {lcd.print("W1:0"); lcd.print(Brightness_WhiteString1); lcd.print("/"); lcd.print(maxBrightness);} else if (Brightness_WhiteString1 >= 100 && Brightness_WhiteString1 < 1000) {lcd.print("W1:"); lcd.print(Brightness_WhiteString1); lcd.print("/"); lcd.print(maxBrightness);} } if (y == 100) { lcd.setCursor(0, 3); percent = 100*(Brightness_WhiteString1)/(maxBrightness); if (percent >= 0 && percent < 10) {lcd.print("W1:00"); lcd.print(percent); lcd.print("% ");} else if (percent >= 10 && percent < 100) {lcd.print("W1:0"); lcd.print(percent); lcd.print("% ");} else if (percent >= 100 && percent < 1000) {lcd.print("W1:"); lcd.print(percent); lcd.print("% ");} } if (y == 200) { lcd.setCursor(0, 3); if (Brightness_WhiteString2 >= 0 && Brightness_WhiteString2 < 10) {lcd.print("W2:00"); lcd.print(Brightness_WhiteString2); lcd.print("/"); lcd.print(maxBrightness);} else if (Brightness_WhiteString2 >= 10 && Brightness_WhiteString2 < 100) {lcd.print("W2:0"); lcd.print(Brightness_WhiteString2); lcd.print("/"); lcd.print(maxBrightness);} else if (Brightness_WhiteString2 >= 100 && Brightness_WhiteString2 < 1000) {lcd.print("W2:"); lcd.print(Brightness_WhiteString2); lcd.print("/"); lcd.print(maxBrightness);} } if (y == 300) { lcd.setCursor(0, 3); percent = 100*(Brightness_WhiteString2)/(maxBrightness); if (percent >= 0 && percent < 10) {lcd.print("W2:00"); lcd.print(percent); lcd.print("% ");} else if (percent >= 10 && percent < 100) {lcd.print("W2:0"); lcd.print(percent); lcd.print("% ");} else if (percent >= 100 && percent < 1000) {lcd.print("W2:"); lcd.print(percent); lcd.print("% ");} } if (y == 400) { lcd.setCursor(0, 3); if (Brightness_BlueString >= 0 && Brightness_BlueString < 10) {lcd.print("BL:00"); lcd.print(Brightness_BlueString); lcd.print("/"); lcd.print(maxBrightness);} else if (Brightness_BlueString >= 10 && Brightness_BlueString < 100) {lcd.print("BL:0"); lcd.print(Brightness_BlueString); lcd.print("/"); lcd.print(maxBrightness);} else if (Brightness_BlueString >= 100 && Brightness_BlueString < 1000) {lcd.print("BL:"); lcd.print(Brightness_BlueString); lcd.print("/"); lcd.print(maxBrightness);} } if (y == 500) { lcd.setCursor(0, 3); percent = 100*(Brightness_BlueString)/(maxBrightness); if (percent >= 0 && percent < 10) {lcd.print("BL:00"); lcd.print(percent); lcd.print("% ");} else if (percent >= 10 && percent < 100) {lcd.print("BL:0"); lcd.print(percent); lcd.print("% ");} else if (percent >= 100 && percent < 1000) {lcd.print("BL:"); lcd.print(percent); lcd.print("% ");} } if (y == 600) { lcd.setCursor(0, 3); if (Brightness_RedString >= 0 && Brightness_RedString < 10) {lcd.print("YL:00"); lcd.print(Brightness_RedString); lcd.print("/"); lcd.print(maxBrightness);} else if (Brightness_RedString >= 10 && Brightness_RedString < 100) {lcd.print("YL:0"); lcd.print(Brightness_RedString); lcd.print("/"); lcd.print(maxBrightness);} else if (Brightness_RedString >= 100 && Brightness_RedString < 1000) {lcd.print("YL:"); lcd.print(Brightness_RedString); lcd.print("/"); lcd.print(maxBrightness);} } if (y == 700) { lcd.setCursor(0, 3); percent = 100*(Brightness_RedString)/(maxBrightness); if (percent >= 0 && percent < 10) {lcd.print("YL:00"); lcd.print(percent); lcd.print("% ");} else if (percent >= 10 && percent < 100) {lcd.print("YL:0"); lcd.print(percent); lcd.print("% ");} else if (percent >= 100 && percent < 1000) {lcd.print("YL:"); lcd.print(percent); lcd.print("% ");} y=-1; // Reset counter. As "PrintBrightnessOnLCD()" runs in a pace of 100x, variable "y" must be set to -1 so when it rolls over it will be printed immediately. } } // Print Copyright on LCD void PrintCopyrightOnLCD (void) { x++; if (z == 5){flag=1;} if (z == 0){flag=0;} if (x == 1 && flag == 0) { lcd.setCursor(0, 2); lcd.print(" "); lcd.setCursor(z, 2); lcd.print("VascoFerraz.com"); z++; } if (x == 1 && flag == 1) { lcd.setCursor(0, 2); lcd.print(" "); lcd.setCursor(z, 2); lcd.print("VascoFerraz.com"); z--; } } // Debounce ClockMode pushbutton and jump between modes void SwitchClockMode (unsigned char x) { if (analogRead(ClockMode) >= 1004) { ClockModeFlag = 1; } if (analogRead(ClockMode) <= 20 && ClockModeFlag == 1) { ClockModeFlag=0; gotomain=0; lcd.clear(); ClockModeState=x; k=0; } } // This is the function which does the following: // if there is no input, automatically jump to main display when gotomain = gotomaincounter. Change this time by manipulating the gotomaincouter value. void gotomainfunction(void) { gotomain++; if (gotomain == gotomaincounter) {gotomain=0; lcd.clear(); ClockModeState=0; k=0; x=0; y=-1;} // As "copyright()" won't run each cycle, variable "x" must be zeroed (0) so when you jump into the main screen it will be printed immediately. // As "PrintBrightnessOnLCD()" runs in a pace of 100x, variable "y" must be set to -1 so when it rolls over it will be printed immediately. } // Read Time void ReadTime(void) { Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x00); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 7); second = bcdToDec(Wire.read()); minute = bcdToDec(Wire.read()); hour = bcdToDec(Wire.read()); dayOfWeek = bcdToDec(Wire.read()); day = bcdToDec(Wire.read()); month = bcdToDec(Wire.read()); year = bcdToDec(Wire.read()); } // Set hour void SetHour(void) { lcd.home(); PrintTimeOnLCD(); lcd.setCursor(0, 1); lcd.write((uint8_t)1); lcd.write((uint8_t)1); // If there is no input, automatically jump to main display when gotomain = gotomaincounter. Change this time by manipulating the gotomaincouter value. gotomainfunction(); if(analogRead(SetClockPlus) < 20) { gotomain=0; Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x02); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 1); hour = bcdToDec(Wire.read()); hour++; if (hour == 24){hour=0;} Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x02); Wire.write(decToBcd(hour)); Wire.endTransmission(); delay(settingdelay); } SwitchClockMode (2); } // Set minute void SetMinute(void) { lcd.home(); PrintTimeOnLCD(); lcd.setCursor(3, 1); lcd.write((uint8_t)1); lcd.write((uint8_t)1); // If there is no input, automatically jump to main display when gotomain = gotomaincounter. Change this time by manipulating the gotomaincouter value. gotomainfunction(); if(analogRead(SetClockPlus) < 20) { gotomain=0; Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x01); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 1); minute = bcdToDec(Wire.read()); minute++; if (minute == 60){minute=0;} Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x01); Wire.write(decToBcd(minute)); Wire.endTransmission(); delay(settingdelay); } SwitchClockMode (3); } // Set second void SetSecond(void) { lcd.home(); PrintTimeOnLCD(); lcd.setCursor(6, 1); lcd.write((uint8_t)1); lcd.write((uint8_t)1); // If there is no input, automatically jump to main display when gotomain = gotomaincounter. Change this time by manipulating the gotomaincouter value. gotomainfunction(); if(analogRead(SetClockPlus) < 20) { gotomain=0; Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x00); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 1); second = bcdToDec(Wire.read()); second++; if (second == 60){second=0;} Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x00); Wire.write(decToBcd(second)); Wire.endTransmission(); delay(settingdelay); } SwitchClockMode (4); } // Set day void SetDay(void) { lcd.setCursor(0, 1); PrintDateOnLCD(); lcd.setCursor(0, 2); lcd.write((uint8_t)1); lcd.write((uint8_t)1); // If there is no input, automatically jump to main display when gotomain = gotomaincounter. Change this time by manipulating the gotomaincouter value. gotomainfunction(); if(analogRead(SetClockPlus) < 20) { gotomain=0; Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x04); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 3); day = bcdToDec(Wire.read()); month = bcdToDec(Wire.read()); year = bcdToDec(Wire.read()); day++; if (day == 32 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)){day=1;} if (day == 31 && (month == 4 || month == 6 || month == 9 || month == 11)){day=1;} if (day == 30 && month == 2 && year%4 == 0){day=1;} if (day == 29 && month == 2 && year%4 != 0){day=1;} Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x04); Wire.write(decToBcd(day)); Wire.endTransmission(); delay(settingdelay); } SwitchClockMode (5); } // Set month void SetMonth(void) { lcd.setCursor(0, 1); PrintDateOnLCD(); lcd.setCursor(3, 2); lcd.write((uint8_t)1); lcd.write((uint8_t)1); lcd.write((uint8_t)1); // If there is no input, automatically jump to main display when gotomain = gotomaincounter. Change this time by manipulating the gotomaincouter value. gotomainfunction(); if(analogRead(SetClockPlus) < 20) { gotomain=0; Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x05); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 1); month = bcdToDec(Wire.read()); if (month == 1 && day == 31){month=3;} else if (month == 1 && day == 30){month=3;} else if (month == 1 && day == 29 && year%4 != 0){month=3;} else if (month == 1 && day == 29 && year%4 == 0){month=2;} else if (month == 1 && day <= 28){month=2;} else if (month == 2){month=3;} else if (month == 3 && day == 31){month=5;} else if (month == 3 && day <= 30){month=4;} else if (month == 4){month=5;} else if (month == 5 && day == 31){month=7;} else if (month == 5 && day <= 30){month=6;} else if (month == 6){month=7;} else if (month == 7){month=8;} else if (month == 8 && day == 31){month=10;} else if (month == 8 && day <= 30){month=9;} else if (month == 9){month=10;} else if (month == 10 && day == 31){month=12;} else if (month == 10 && day <= 30){month=11;} else if (month == 11){month=12;} else if (month == 12){month=1;} Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x05); Wire.write(decToBcd(month)); Wire.endTransmission(); delay(settingdelay); } SwitchClockMode (6); } // Set year void SetYear(void) { lcd.setCursor(0, 1); PrintDateOnLCD(); lcd.setCursor(7, 2); lcd.write((uint8_t)1); lcd.write((uint8_t)1); lcd.write((uint8_t)1); lcd.write((uint8_t)1); // If there is no input, automatically jump to main display when gotomain = gotomaincounter. Change this time by manipulating the gotomaincouter value. gotomainfunction(); if(analogRead(SetClockPlus) < 20) { gotomain=0; Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x06); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 1); year = bcdToDec(Wire.read()); if (day == 29 && year%4 == 0 && month == 2){year=year+4;} else year++; if (year >= 31){year=8;} Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0x06); Wire.write(decToBcd(year)); Wire.endTransmission(); delay(settingdelay); } SwitchClockMode (7); } // Set Pre-sunrise void SetPresunrise(void) { presunrise = EEPROM.read(presunriseMemoryBank); lcd.setCursor(0, 0); lcd.print("Pre-sunrise:"); lcd.setCursor(14, 0); lcd.print(":00"); lcd.setCursor(12, 1); lcd.write((uint8_t)1); lcd.write((uint8_t)1); lcd.setCursor(12, 0); if (presunrise >= 10) lcd.print(presunrise, DEC); if (presunrise < 10) { lcd.print ("0"); lcd.print(presunrise, DEC); } // If there is no input, automatically jump to main display when gotomain = gotomaincounter. Change this time by manipulating the gotomaincouter value. gotomainfunction(); if(analogRead(SetClockPlus) < 20) { gotomain=0; presunrise++; if (presunrise >= 10){presunrise=6;} EEPROM.write(presunriseMemoryBank, presunrise); delay(settingdelay); } SwitchClockMode (8); } // Set sunrise void SetSunrise(void) { sunrise = EEPROM.read(sunriseMemoryBank); lcd.setCursor(0, 0); lcd.print("Sunrise:"); lcd.setCursor(14, 0); lcd.print(":00"); lcd.setCursor(12, 1); lcd.write((uint8_t)1); lcd.write((uint8_t)1); lcd.setCursor(12, 0); if (sunrise >= 10) lcd.print(sunrise, DEC); if (sunrise < 10) { lcd.print ("0"); lcd.print(sunrise, DEC); } // If there is no input, automatically jump to main display when gotomain = gotomaincounter. Change this time by manipulating the gotomaincouter value. gotomainfunction(); if(analogRead(SetClockPlus) < 20) { gotomain=0; sunrise++; if (sunrise >= 19){sunrise=10;} EEPROM.write(sunriseMemoryBank, sunrise); delay(settingdelay); } SwitchClockMode (9); } // Set sunset void SetSunset(void) { sunset = EEPROM.read(sunsetMemoryBank); lcd.setCursor(0, 0); lcd.print("Sunset:"); lcd.setCursor(14, 0); lcd.print(":00"); lcd.setCursor(12, 1); lcd.write((uint8_t)1); lcd.write((uint8_t)1); lcd.setCursor(12, 0); if (sunset >= 10) lcd.print(sunset, DEC); if (sunset < 10) { lcd.print ("0"); lcd.print(sunset, DEC); } // If there is no input, automatically jump to main display when gotomain = gotomaincounter. Change this time by manipulating the gotomaincouter value. gotomainfunction(); if(analogRead(SetClockPlus) < 20) { gotomain=0; sunset++; if (sunset >= 24){sunset=19;} EEPROM.write(sunsetMemoryBank, sunset); delay(settingdelay); } SwitchClockMode (0); x=0; // As "copyright()" won't run each cycle, variable "x" must be zeroed (0), so when it jumps into the main screen it will be printed immediately. y=-1; // As "PrintBrightnessOnLCD()" runs in a pace of 100x, variable "y" must be set to -1 so when it rolls over it will be printed immediately. } // Below you will see the function where you can configure the PWM cycle: sunrise, daylight, sunset and moonlight. void LED_PWM(void) { // Pre-sunrise 1 if (hour == presunrise) { Brightness_WhiteString1 = 0.5*minute; Brightness_WhiteString2 = 0.5*minute; Brightness_BlueString = 18 + (0.2*minute); Brightness_RedString = 4*minute; ShiftPWM.SetOne(WhiteString1,Brightness_WhiteString1); ShiftPWM.SetOne(WhiteString2,Brightness_WhiteString2); ShiftPWM.SetOne(BlueString,Brightness_BlueString); ShiftPWM.SetOne(RedString,Brightness_RedString); } // Pre-sunrise 2 if (hour >= presunrise+1 && hour <= sunrise-1) { Brightness_WhiteString1 = 30; Brightness_WhiteString2 = 30; Brightness_BlueString = 30; Brightness_RedString = 240; //equals to maxBrightness ShiftPWM.SetOne(WhiteString1,Brightness_WhiteString1); ShiftPWM.SetOne(WhiteString2,Brightness_WhiteString2); ShiftPWM.SetOne(BlueString,Brightness_BlueString); ShiftPWM.SetOne(RedString,Brightness_RedString); } // Sunrise if (hour == sunrise) { Brightness_WhiteString1 = 30 + (0.875*4*minute); Brightness_WhiteString2 = 30 + (0.875*4*minute); Brightness_BlueString = 30 + (0.875*4*minute); Brightness_RedString = 240; //equals to maxBrightness ShiftPWM.SetOne(WhiteString1,Brightness_WhiteString1); ShiftPWM.SetOne(WhiteString2,Brightness_WhiteString2); ShiftPWM.SetOne(BlueString,Brightness_BlueString); ShiftPWM.SetOne(RedString,Brightness_RedString); } // Sunlight if (hour >= sunrise+1 && hour <= sunset-1) { Brightness_WhiteString1 = maxBrightness; Brightness_WhiteString2 = maxBrightness; Brightness_BlueString = maxBrightness; Brightness_RedString = maxBrightness; ShiftPWM.SetOne(WhiteString1,Brightness_WhiteString1); ShiftPWM.SetOne(WhiteString2,Brightness_WhiteString2); ShiftPWM.SetOne(BlueString,Brightness_BlueString); ShiftPWM.SetOne(RedString,Brightness_RedString ); } // Sunset if (hour == sunset) { Brightness_WhiteString1 = maxBrightness - (4*minute); Brightness_WhiteString2 = maxBrightness - (4*minute); Brightness_BlueString = maxBrightness - (0.925*4*minute); Brightness_RedString = maxBrightness - (4*minute); ShiftPWM.SetOne(WhiteString1,Brightness_WhiteString1); ShiftPWM.SetOne(WhiteString2,Brightness_WhiteString2); ShiftPWM.SetOne(BlueString,Brightness_BlueString); ShiftPWM.SetOne(RedString,Brightness_RedString); } // Moonlight 1 if (hour >= sunset+1 && hour <= 23) { Brightness_WhiteString1 = 0; Brightness_WhiteString2 = 0; Brightness_BlueString = 18; Brightness_RedString = 0; ShiftPWM.SetOne(WhiteString1,Brightness_WhiteString1); ShiftPWM.SetOne(WhiteString2,Brightness_WhiteString2); ShiftPWM.SetOne(BlueString,Brightness_BlueString); ShiftPWM.SetOne(RedString,Brightness_RedString); } // Moonlight 2 if (hour >= 0 && hour <= presunrise-1) { Brightness_WhiteString1 = 0; Brightness_WhiteString2 = 0; Brightness_BlueString = 18; Brightness_RedString = 0; ShiftPWM.SetOne(WhiteString1,Brightness_WhiteString1); ShiftPWM.SetOne(WhiteString2,Brightness_WhiteString2); ShiftPWM.SetOne(BlueString,Brightness_BlueString); ShiftPWM.SetOne(RedString,Brightness_RedString); } } // This is a function with debugging purposes void LED_PWM_Debug(void) { ShiftPWM.SetOne(WhiteString1,50); ShiftPWM.SetOne(WhiteString2,50); ShiftPWM.SetOne(BlueString,50); ShiftPWM.SetOne(RedString,50); } |
Some important remarks that should not be forgotten:
1) Before compiling the code you need to download, uncompress and install the following libraries: RTClib and ShiftPWM. To install simply copy them into the “libraries” folder. Alternatively you can read the official tutorial: Installing Additional Arduino Libraries
2) The pinout for the IRF3205 N-Channel Power MOSFETs is the following:
Gate | Drain | Source
If you’re using other transistors to switch the LEDs and you are not sure about its pinout, you should read the datasheet.
3) The ground of the LED driver (Driver V-) should be connected to the power-supply’s ground. The positive of the LED driver (Driver V+) should NOT be connected to the positive of the power-supply.
4) The circuit can be powered via Arduino’s USB port or applying a 12V voltage at Arduino’s Vin pin.
5) The only trimmer (variable resistor) in the circuit has a value of 10kOhm and it’s used to regulate contrast of the LCD.
6) In order to view all data available on the LCD I recommend you to use a 20×4 char LCD instead of a 16×2 char LCD.
7) Some LCD boards have the backlight LED pins inverted. So, before connecting it to the circuit, make sure to check the datasheet to make avoid damage on your LCD.
8) This project is no longer supported, so, if you have strange characters on the LCD please use an older version of the Arduino IDE (1.0.5 was reported to work).
This is the final result of the illumination system in my South American aquarium biotope: