Advertisement
safwan092

Untitled

Apr 14th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.83 KB | None | 0 0
  1.  
  2.  
  3. ////////////////////////////////ARDUINO PWM SOLAR CHARGE CONTROLLER V-2.02///////////////////////////////////////////////////////////////////////////////
  4. //----------------------------------------------------------------------------------------------------
  5.  
  6. // Author: Debasish Dutta
  7. // www.opengreenenergy.com
  8. // This code is for an arduino based Solar PWM charge controller ( V-2.0)
  9. // Credit :logic of 3 stage charging taken from http://jamesoid.com/pwm_charger_project/pwm_charger.ph
  10. // Last updated on 14/04/2022
  11. //----------------------------------------------------------------------------------------------------
  12. // Arduino pins Connections----------------------------------------------------------------------------
  13.  
  14. // A0 - Voltage divider to measure solar panel voltage
  15. // A1 - Voltage divider to measure battery voltage
  16. // A2 - LDR input
  17. // A3 - ACS712 to monitor solar current
  18. // A4 - LCD+RTC SDA
  19. // A5 - LCD+RTC SCL
  20.  
  21. // D0 - Not used
  22. // D1 - Not used
  23. // D2 - Control Load Relay S pin
  24. // D3 - PWM Output to control MOSFET Q1
  25. // D4 - Not Used
  26. // D5 - Battery Red LED
  27. // D6 - Battery Green LED
  28. // D7 - Battery Blue LED
  29. // D8 - Load Red Led
  30. // D9 - Load Green Led
  31. // D10- Solar Red LED
  32. // D11- Solar Green LED
  33. // D12- DS18B20 Temp. Sensor
  34. // D13- Not Used
  35.  
  36. #include <Wire.h>
  37. #include <OneWire.h>
  38. #include "RTClib.h"
  39. #include <DallasTemperature.h>
  40. #include <LiquidCrystal_I2C.h>
  41.  
  42. #define SOL_ADC A0 // Solar panel side voltage divider is connected to pin A0
  43. #define BAT_ADC A1 // Battery side voltage divider is connected to pin A1
  44. #define ldr A2
  45. //#define LOAD_CURRENT_ADC A2 // ACS 712 current sensor is connected to pin A2 for load curremt
  46. #define SOL_CURRENT_ADC A3 // ACS 712 current sensor is connected to pin A3 for solar current
  47. #define AVG_NUM 10 // number of iterations of the adc routine to average the adc readings
  48. #define BAT_MIN 10.5 // minimum battery voltage for 12V system
  49. #define BAT_MAX 15.0 // maximum battery voltage for 12V system
  50. #define BULK_CH_SP 14.4 // bulk charge set point for sealed lead acid battery // flooded type set it to 14.6V
  51. #define FLOAT_CH_SP 13.6 //float charge set point for lead acid battery
  52. #define LVD 11.5 //Low voltage disconnect setting for a 12V system
  53. #define PWM_PIN 3 // pin-3 is used to control the charging MOSFET //the default frequency is 490.20Hz
  54. #define LOAD_PIN 2 // pin-2 is used to control the load
  55. #define BAT_RED_LED 5
  56. #define BAT_GREEN_LED 6
  57. #define BAT_BLUE_LED 7
  58. #define LOAD_RED_LED 8
  59. #define LOAD_GREEN_LED 9
  60. #define SOL_RED_LED 10
  61. #define SOL_GREEN_LED 11
  62. #define ONE_WIRE_BUS 12 // Data wire of DS18B20 temp. sensor is connected to pin 12
  63.  
  64. //--------------------------------------------------------------------------------------------------------------------------
  65. ///////////////////////DECLARATION OF ALL BIT MAP ARRAY FOR FONTS////////////////////////////////////////////////////////////////
  66. //--------------------------------------------------------------------------------------------------------------------------
  67.  
  68. byte solar[8] = //icon for solar panel
  69. {
  70. 0b11111, 0b10101, 0b11111, 0b10101, 0b11111, 0b10101, 0b11111, 0b00000
  71. };
  72. byte battery[8] = //icon for battery
  73. {
  74. 0b01110, 0b11011, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b11111
  75. };
  76.  
  77. byte energy[8] = // icon for power
  78. {
  79. 0b00010, 0b00100, 0b01000, 0b11111, 0b00010, 0b00100, 0b01000, 0b00000
  80. };
  81. /*byte alarm[8] = // icon for alarm
  82. {
  83. 0b00000,0b00100,0b01110,0b01110,0b01110,0b11111,0b00000,0b00100
  84. };*/
  85. byte temp[8] = //icon for termometer
  86. {
  87. 0b00100, 0b01010, 0b01010, 0b01110, 0b01110, 0b11111, 0b11111, 0b01110
  88. };
  89.  
  90. byte charge[8] = // icon for battery charge
  91. {
  92. 0b01010, 0b11111, 0b10001, 0b10001, 0b10001, 0b01110, 0b00100, 0b00100,
  93. };
  94. byte not_charge[8] =
  95. {
  96. 0b00000, 0b10001, 0b01010, 0b00100, 0b01010, 0b10001, 0b00000, 0b00000,
  97. };
  98.  
  99.  
  100. //--------------------------------------------------------------------------------------------------------------------------
  101. ///////////////////////DECLARATION OF ALL GLOBAL VARIABLES//////////////////////////////////////////////////////////////////
  102. //--------------------------------------------------------------------------------------------------------------------------
  103. float solar_volt = 0;
  104. float bat_volt = 0;
  105. float load_current = 0;
  106. float solar_current = 0;
  107. float offsetVoltage = 2.5; // for ACS712 sensor
  108. float Sensitivity = 0.185; // 185mV/A for ACS712-5A variant
  109. int temperature = 0;
  110. int temp_change = 0;
  111. float system_volt = 0;
  112. float bulk_charge_sp = 0;
  113. float float_charge_sp = 0;
  114. int charge_stage = 0;
  115. float load_status = 0;
  116. double PWM_Duty = 0; // PWM_Duty varies from 0 to 255
  117. float lvd;
  118. int ldr_status = 0;
  119.  
  120. // Variables for power and energy measurements
  121.  
  122. long unsigned time = 0;
  123. long unsigned msec = 0;
  124. long unsigned last_msec = 0;
  125. long unsigned elasped_msec = 0;
  126. long unsigned elasped_time = 0;
  127. float load_ampSecs = 0;
  128. float load_ampHours = 0;
  129. float load_watts = 0;
  130. float load_wattSecs = 0;
  131. float load_wattHours = 0;
  132. float solar_ampSecs = 0;
  133. float solar_ampHours = 0;
  134. float solar_watts = 0;
  135. float solar_wattSecs = 0;
  136. float solar_wattHours = 0;
  137.  
  138. OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
  139. DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature sensor
  140. RTC_DS3231 rtc;
  141.  
  142. // Set the pins on the I2C chip used for LCD connections:
  143. // addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
  144. LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD I2C address // In my case 0x27
  145. //******************************************************* MAIN PROGRAM START ************************************************
  146. void setup()
  147. {
  148.  
  149. Serial.begin(9600);
  150. rtc.begin();
  151. sensors.begin(); // // Start up the library
  152. TCCR2B = TCCR2B & B11111000 | 0x03; // pin 3 PWM frequency of 980.39 Hz
  153.  
  154. pinMode(ldr, INPUT);
  155. pinMode(SOL_RED_LED , OUTPUT);
  156. pinMode(SOL_GREEN_LED, OUTPUT);
  157. pinMode(BAT_RED_LED, OUTPUT);
  158. pinMode(BAT_GREEN_LED, OUTPUT);
  159. pinMode(BAT_BLUE_LED, OUTPUT);
  160. pinMode(LOAD_RED_LED , OUTPUT);
  161. pinMode(LOAD_GREEN_LED, OUTPUT);
  162. pinMode(PWM_PIN, OUTPUT);
  163. pinMode(LOAD_PIN, OUTPUT);
  164. digitalWrite(PWM_PIN, LOW); // default value of pwm duty cycle
  165. digitalWrite(LOAD_PIN, LOW); // default load state is OFF
  166. lcd.init(); // initialize the lcd for 16 chars 2 lines, turn on backlight
  167. lcd.backlight(); // finish with backlight on
  168. lcd.createChar(1, solar);
  169. lcd.createChar(2, battery);
  170. lcd.createChar(3, energy);
  171. //lcd.createChar(4,alarm);
  172. lcd.createChar(5, temp);
  173. lcd.createChar(6, charge);
  174. lcd.createChar(7, not_charge);
  175. lcd.clear();
  176. if (rtc.lostPower()) {
  177. Serial.println("RTC lost power, let's set the time!");
  178. // When time needs to be set on a new device, or after a power loss, the
  179. // following line sets the RTC to the date & time this sketch was compiled
  180. rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  181. // This line sets the RTC with an explicit date & time, for example to set
  182. // January 21, 2014 at 3am you would call:
  183. // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  184. }
  185. }
  186.  
  187. void loop()
  188. {
  189. read_data(); // read different sensors data from analog pin of arduino
  190. system_voltage(); // detect the system voltage according to battery voltage
  191. setpoint(); // decide the charge set point according to system voltage
  192. charge_cycle(); // pwm charging of battery
  193. power(); // calculate the load power and energy
  194. load_control(); //control the load
  195. led_indication(); // led indica
  196. print_data(); // print in serial monitor
  197. lcd_display(); // lcd display
  198. }
  199. //************************************************************ PROGRAM END *************************************************
  200.  
  201.  
  202. //------------------------------------------------------------------------------------------------------
  203. ////////////////// READS AND AVERAGES THE ANALOG INPUTS (SOLRAR VOLTAGE,BATTERY VOLTAGE)////////////////
  204. //------------------------------------------------------------------------------------------------------
  205. int read_adc(int adc_parameter)
  206. {
  207.  
  208. int sum = 0;
  209. int sample ;
  210. for (int i = 0; i < AVG_NUM; i++)
  211. { // loop through reading raw adc values AVG_NUM number of times
  212. sample = analogRead(adc_parameter); // read the input pin
  213. sum += sample; // store sum for averaging
  214. delayMicroseconds(50); // pauses for 50 microseconds
  215. }
  216. return (sum / AVG_NUM); // divide sum by AVG_NUM to get average and return it
  217. }
  218. //-------------------------------------------------------------------------------------------------------------
  219. ////////////////////////////////////READ THE DATA//////////////////////////////////////////////////////////////
  220. //-------------------------------------------------------------------------------------------------------------
  221. void read_data(void)
  222. {
  223. //5V = ADC value 1024 => 1 ADC value = (5/1024)Volt= 0.0048828Volt
  224. // Vout=Vin*R2/(R1+R2) => Vin = Vout*(R1+R2)/R2 R1=100 and R2=20
  225. ldr_status = analogRead(A0);
  226. solar_volt = read_adc(SOL_ADC) * 0.00488 * (120 / 20);
  227. bat_volt = read_adc(BAT_ADC) * 0.00488 * (120 / 20);
  228. //load_current = ((read_adc(LOAD_CURRENT_ADC)*0.00488 - offsetVoltage-2.5)/Sensitivity ); // 2.4V offset when no load is connected
  229. solar_current = ((read_adc(SOL_CURRENT_ADC) * 0.00488 - offsetVoltage) / Sensitivity );
  230. // if (load_current <0)
  231. //{
  232. // load_current = 0;
  233. //}
  234. if (solar_current < 0)
  235. {
  236. solar_current = 0;
  237. }
  238.  
  239. sensors.requestTemperatures(); // get temperature readings
  240. temperature = sensors.getTempCByIndex(0) ; // 0 refers to the first IC on the wire
  241.  
  242. }
  243. //------------------------------------------------------------------------------------------------------------
  244. /////////////////////////////////POWER AND ENERGY CALCULATION //////////////////////////////////////////////
  245. //------------------------------------------------------------------------------------------------------------
  246. void power(void) {
  247.  
  248. msec = millis();
  249. elasped_msec = msec - last_msec; //Calculate how long has past since last call of this function
  250. elasped_time = elasped_msec / 1000.0; // 1sec=1000 msec
  251. //load_watts = load_current * bat_volt; //Watts now
  252. solar_watts = solar_current * solar_volt; //Watts now
  253.  
  254. //load_ampSecs = (load_current*elasped_time); //AmpSecs since last measurement
  255. solar_ampSecs = (solar_current * elasped_time); //AmpSecs since last measurement
  256.  
  257. //load_wattSecs = load_ampSecs * bat_volt; //WattSecs since last measurement
  258. solar_wattSecs = solar_ampSecs * solar_volt; //WattSecs since last measurement
  259.  
  260. //load_ampHours = load_ampHours + load_ampSecs/3600; // 1 hour=3600sec //Total ampHours since program started
  261. solar_ampHours = solar_ampHours + solar_ampSecs / 3600; // 1 hour=3600sec //Total ampHours since program started
  262.  
  263. //load_wattHours = load_wattHours + load_wattSecs/3600; // 1 hour=3600sec //Total wattHours since program started
  264. solar_wattHours = solar_wattHours + solar_wattSecs / 3600; // 1 hour=3600sec //Total wattHours since program started
  265.  
  266. last_msec = msec; //Store 'now' for next time
  267.  
  268.  
  269. /*
  270.  
  271. last_time = current_time;
  272. current_time = millis();
  273. load_watts = load_current * bat_volt; //load Watts now
  274. solar_watts = solar_current * solar_volt; //solar Watts now
  275. load_wattHours = load_wattHours + load_watts*(( current_time -last_time) /3600000.0) ; // calculating energy in Watt-Hour
  276. solar_wattHours = solar_wattHours+ solar_watts*(( current_time -last_time) /3600000.0) ; // calculating energy in Watt-Hour
  277. */
  278. }
  279.  
  280. //------------------------------------------------------------------------------------------------------------
  281. /////////////////////////////////PRINT DATA IN SERIAL MONITOR/////////////////////////////////////////////////
  282. //------------------------------------------------------------------------------------------------------------
  283. void print_data(void)
  284. {
  285. //delay(100);
  286. Serial.print(" Solar Voltage: ");
  287. Serial.print(solar_volt);
  288. Serial.print(" Battery Voltage: ");
  289. Serial.print(bat_volt);
  290. Serial.print("Temp: " );
  291. Serial.print (temperature);
  292. Serial.print(" Bulk Voltage Setpoint: ");
  293. Serial.print(bulk_charge_sp);
  294. Serial.print(" Float Voltage Setpoint: ");
  295. Serial.print(float_charge_sp);
  296. Serial.print(" PWM Duty: ");
  297. Serial.print((PWM_Duty / 255 ) * 100); // convert a number between 0 -1024 to 0-1024 then convert float to intiger
  298. Serial.print("%");
  299. Serial.print(" Charge Stage: ");
  300. Serial.println(charge_stage);
  301. Serial.print("Solar Current: ");
  302. Serial.print(solar_current);
  303. Serial.println("A");
  304. }
  305. //----------------------------------------------------------------------------------------------------------------------
  306. //////////////////////////////////SYSTEM VOLTAGE AUTO DETECT ///////////////////////////////////////////////////////////
  307. //----------------------------------------------------------------------------------------------------------------------
  308. void system_voltage(void)
  309. {
  310. if ((bat_volt > BAT_MIN) && (bat_volt < BAT_MAX))
  311. {
  312. system_volt = 12;
  313. }
  314. /*
  315. else if ((bat_volt > BAT_MIN*2 ) && (bat_volt < BAT_MAX*2))
  316. {
  317. system_volt=24;
  318. }*/
  319. else if ((bat_volt > BAT_MIN / 2 ) && (bat_volt < BAT_MAX / 2))
  320. {
  321. system_volt = 6;
  322. }
  323.  
  324. }
  325. //---------------------------------------------------------------------------------------------------------------------------
  326. ////////////////////////////////////CHARGE SET POINT ///////////////////////////////////////////////////////////////////////
  327. //---------------------------------------------------------------------------------------------------------------------------
  328.  
  329. void setpoint(void)
  330. {
  331. temp_change = temperature - 25.0; // 25deg cel is taken as standard room temperature
  332. // temperature compensation = -5mv/degC/Cell
  333. // If temperature is above the room temp ;Charge set point should reduced
  334. // If temperature is bellow the room temp ;Charge set point should increased
  335. if (system_volt == 12)
  336. {
  337. bulk_charge_sp = BULK_CH_SP - (0.050 * temp_change) ;
  338. float_charge_sp = FLOAT_CH_SP - (0.050 * temp_change) ;
  339. lvd = LVD;
  340. }
  341.  
  342. else if (system_volt == 6)
  343. {
  344. bulk_charge_sp = (BULK_CH_SP / 2) - (0.015 * temp_change) ;
  345. float_charge_sp = (FLOAT_CH_SP / 2) - (0.015 * temp_change) ;
  346. lvd = LVD / 2;
  347. }
  348. /*
  349. else if (system_volt == 24)
  350. {
  351. bulk_charge_sp = (BULK_CH_SP*2)-(0.060*temp_change) ;
  352. float_charge_sp= (FLOAT_CH_SP*2)-(0.060*temp_change) ;
  353. lvd=LVD*2;
  354. }
  355. */
  356.  
  357. }
  358. //--------------------------------------------------------------------------------------------------------------------------------
  359. ///////////////////////////////////////////////////PWM CHARGE CYCLE @500 HZ //////////////////////////////////////////////////
  360. //-------------------------------------------------------------------------------------------------------------------------------
  361. void charge_cycle(void)
  362. {
  363. if (solar_volt <= 10) {
  364. read_data(); // Reading voltage temp from the sensors
  365. setpoint(); // Reading temp compensated charging set point
  366. PWM_Duty = 0;
  367. analogWrite(PWM_PIN, PWM_Duty); //generate PWM from D9 @ 0% duty // Shut down the charger
  368.  
  369. }
  370.  
  371.  
  372. //--------------------------------------------------------------------------------------------------------------------------------
  373. ///////////////////////////////////////////////////PWM CHARGE CYCLE @500 HZ //////////////////////////////////////////////////
  374. //-------------------------------------------------------------------------------------------------------------------------------
  375.  
  376. /////////////////////////////////// STAGE-1 (BULK CHARGING)//////////////////////////////////////////////////////
  377. // During this stage the MOSFET is fully on by setting the duty cycle to 100%
  378. // Constant Current Charging
  379. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  380.  
  381.  
  382.  
  383. if ((bat_volt < bulk_charge_sp) && (solar_volt > (bat_volt + .5)) && charge_stage == 1) {
  384. read_data(); // Reading voltage and temp from the sensors
  385. setpoint(); // Reading temp compensated charging set point
  386. PWM_Duty = 252.45;
  387. analogWrite(PWM_PIN, PWM_Duty); // 99 % duty cycle // rapid charging
  388. }
  389. charge_stage = 2;
  390.  
  391.  
  392. /////////////////////////////////// STAGE-2 (TOPPING CHARGE)//////////////////////////////////////////////////////
  393. // During this stage the MOSFET is fully on by setting the duty cycle to 100%
  394. // Constant voltage
  395. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  396.  
  397. // 25.5 is 10 % duty cycle
  398. if ((PWM_Duty > 25.5 ) && (solar_volt > (bulk_charge_sp + .5)) && charge_stage == 2) {
  399. time = millis();
  400. read_data(); // Reading voltage and temp from the sensors
  401. setpoint(); // Reading temp compensated charging set point
  402.  
  403. if (bat_volt >= bulk_charge_sp) { // if the battery voltage rise above the bulk charge setpoint
  404. PWM_Duty--; // Slow down the charging rate by reducing the pwm duty cycle
  405. if (PWM_Duty < 0) { // if during slow down process duty cycle falls below 0% then set to 0%
  406. PWM_Duty = 0;
  407. }
  408. analogWrite(PWM_PIN, PWM_Duty); //generate PWM from D3 @ 0% duty // MOSFET Q1 is OFF
  409. }
  410. if (bat_volt < bulk_charge_sp) { // if the battery voltage falls below the bulk charge setpoint, then go to stage-1
  411. charge_stage = 1;
  412. }
  413. else {
  414. analogWrite(PWM_PIN, PWM_Duty);
  415. }
  416.  
  417. /* while( millis()-time > 3600000){ // Ensure the charger stay 1hour in bulk charging
  418. charge_stage = 3;
  419. }
  420. */
  421.  
  422. }
  423. charge_stage = 3;
  424.  
  425.  
  426. /////////////////////////////////// STAGE-3 (FLOAT CHARGING)//////////////////////////////////////////////////////
  427. // During this stage the MOSFET is fully on by setting the duty cycle to 100%
  428. // Constant Current Charging
  429. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  430.  
  431. if ((solar_volt > ( float_charge_sp + .5)) && charge_stage == 3) {
  432. read_data(); // Reading voltage and temp from the sensors
  433. setpoint(); // Reading temp compensated charging set point
  434. if (bat_volt >= float_charge_sp) { // if the battery voltage is more than float charge setpoint then shut down the charger
  435. PWM_Duty = 0; // setting duty cycle =0 to turn off the MOSFET Q1
  436. analogWrite(PWM_PIN, PWM_Duty); //generate PWM from D9 @ 0% duty // MOSFET Q1 is OFF
  437. }
  438. else if (bat_volt < float_charge_sp - 0.5) { // if the battery voltage falls below the float charge setpoint then go to stage -1
  439. charge_stage = 1;
  440. }
  441. else {
  442. PWM_Duty = 252.25; // setting duty cycle = 5% for trickle charge
  443.  
  444. analogWrite(PWM_PIN, PWM_Duty); //generate PWM from D9 @ 5% duty // Q1 is driving @ 5% duty cycle
  445.  
  446. }
  447.  
  448. }
  449. }
  450. //----------------------------------------------------------------------------------------------------------------------
  451. /////////////////////////////////////////////LOAD CONTROL/////////////////////////////////////////////////////
  452. //----------------------------------------------------------------------------------------------------------------------
  453.  
  454. void load_control()
  455. {
  456. DateTime now = rtc.now();
  457. if (ldr_status < 700) {
  458. load_status = 1;
  459. digitalWrite(LOAD_PIN, HIGH); // load is ON
  460. }
  461. else {
  462. // 18 - 19 - 20 - 21 - 22 - 23 - 0 - 1 - 2 - 3 - 4 - 5 - 6
  463. if (now.hour() == 18 || now.hour() == 19 || now.hour() == 20 || now.hour() == 21 || now.hour() == 22 || now.hour() == 23 || now.hour() == 0 || now.hour() == 1 || now.hour() == 2 || now.hour() == 3 || now.hour() == 4 || now.hour() == 5 ) {
  464. load_status = 1;
  465. digitalWrite(LOAD_PIN, HIGH); // load is ON
  466. }
  467. else {
  468. load_status = 0;
  469. digitalWrite(LOAD_PIN, LOW); //load is OFF
  470. }
  471. }
  472. }
  473.  
  474.  
  475. //-------------------------------------------------------------------------------------------------
  476. //////////////////////////LED INDICATION////////////////////////////////////
  477. //-------------------------------------------------------------------------------------------------
  478. void led_indication(void)
  479. {
  480. solar_led(); //Solar led indication
  481. battery_led(); //Battery status led indication
  482. load_led(); //Load led indication
  483. }
  484. //----------------------------------------------------------------------------------------------------------------------
  485. /////////////////////////////////////////////SOLAR LED INDICATION/////////////////////////////////////////////////////
  486. //----------------------------------------------------------------------------------------------------------------------
  487.  
  488. void solar_led()
  489.  
  490. {
  491. if (solar_volt > float_charge_sp)
  492. {
  493. digitalWrite(SOL_RED_LED, LOW);
  494. digitalWrite(SOL_GREEN_LED, HIGH); // Sun Light is available and charger is ready for charging
  495. }
  496. else
  497. {
  498. digitalWrite(SOL_GREEN_LED, LOW);
  499. digitalWrite(SOL_RED_LED, HIGH); //Sun light is not available for charging
  500.  
  501. }
  502. }
  503. //----------------------------------------------------------------------------------------------------------------------
  504. /////////////////////////////////////////////BATTERY LED INDICATION/////////////////////////////////////////////////////
  505. //----------------------------------------------------------------------------------------------------------------------
  506. void battery_led(void)
  507. {
  508.  
  509. if ( (bat_volt > system_volt) && ( bat_volt < bulk_charge_sp))
  510. {
  511. leds_off_all();
  512. digitalWrite(BAT_GREEN_LED, HIGH); // battery voltage is healthy
  513. }
  514. else if (bat_volt >= bulk_charge_sp)
  515. {
  516. leds_off_all();
  517. digitalWrite(BAT_BLUE_LED, HIGH); //battery is fully charged
  518. }
  519. else if (bat_volt < system_volt)
  520. {
  521. leds_off_all();
  522. digitalWrite(BAT_RED_LED, HIGH); // battery voltage low
  523. }
  524. }
  525. //----------------------------------------------------------------------------------------------------------------------
  526. /////////////////////////////////////////////LOAD LED INDICATION/////////////////////////////////////////////////////
  527. //----------------------------------------------------------------------------------------------------------------------
  528.  
  529. void load_led()
  530.  
  531. {
  532. if (load_status == 1)
  533. {
  534. digitalWrite(LOAD_RED_LED, LOW);
  535. digitalWrite(LOAD_GREEN_LED, HIGH);
  536. }
  537. else if (load_status == 0)
  538. {
  539. digitalWrite(LOAD_GREEN_LED, LOW);
  540. digitalWrite(LOAD_RED_LED, HIGH);
  541. }
  542. }
  543.  
  544. //------------------------------------------------------------------------------------------------------
  545. //////////////////////// TURN OFF ALL THE LED///////////////////////////////////////////////////////////
  546. //------------------------------------------------------------------------------------------------------
  547. void leds_off_all(void)
  548.  
  549. {
  550.  
  551. digitalWrite(BAT_RED_LED, LOW);
  552. digitalWrite(BAT_GREEN_LED, LOW);
  553. digitalWrite(BAT_BLUE_LED, LOW);
  554.  
  555.  
  556. }
  557. //------------------------------------------------------------------------------------------------------
  558. //////////////////////// LCD DISPLAY///////////////////////////////////////////////////////////////////
  559. //------------------------------------------------------------------------------------------------------
  560. void lcd_display()
  561. {
  562. // Display Solar Panel Parameters
  563. lcd.setCursor(0, 0);
  564. lcd.write(1);
  565. lcd.setCursor(2, 0);
  566. lcd.print(solar_volt, 1);
  567. lcd.print("V");
  568. lcd.setCursor(8, 0);
  569. lcd.print(solar_current, 1);
  570. lcd.print("A");
  571. lcd.setCursor(14, 0);
  572. lcd.print(solar_watts, 1);
  573. lcd.print("W");
  574.  
  575. // Display Battery Parameters
  576. lcd.setCursor(0, 1);
  577. lcd.write(2);
  578. lcd.setCursor(2, 1);
  579. lcd.print(bat_volt, 1);
  580. lcd.print("V");
  581. lcd.setCursor(8, 1);
  582. lcd.write(5);
  583. lcd.setCursor(9, 1);
  584. lcd.print(temperature);
  585. lcd.write(0b11011111);
  586. lcd.print("C");
  587.  
  588. lcd.setCursor(14, 1);
  589. lcd.write(2);
  590. if ((charge_stage == 1) | (charge_stage == 2) | (charge_stage == 3))
  591. {
  592. lcd.write(6);
  593. }
  594. else
  595. {
  596. lcd.write(7);
  597. }
  598.  
  599. // Display Load Parameters
  600.  
  601. lcd.setCursor(0, 2);
  602. lcd.print("L");
  603. lcd.setCursor(2, 2);
  604. lcd.print(ldr_status, 1);
  605. //lcd.print("A");
  606. //lcd.setCursor(8, 2);
  607. //lcd.print(load_watts, 1);
  608. //lcd.print("W");
  609. lcd.setCursor(14, 2);
  610. if (load_status == 1)
  611. {
  612. lcd.print(" ");
  613. lcd.setCursor(14, 2);
  614. lcd.print("ON");
  615. }
  616. else if (load_status == 0)
  617. {
  618. lcd.print("OFF");
  619. }
  620.  
  621. // Display Energy
  622. lcd.setCursor(0, 3);
  623. lcd.write(3);
  624. lcd.setCursor(2, 3);
  625. lcd.print(solar_wattHours, 1);
  626. lcd.print("Wh");
  627. //lcd.setCursor(8, 3);
  628. //lcd.print(load_wattHours, 1);
  629. //lcd.print("Wh");
  630.  
  631. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement