solar tracker with RTC

hello everyone, i read the thread “how to use this forum”
i will do my best to be as clear as possible

summary; the solar tracker is an arduino duo that controls a linear actuator (2 relay board) on a timer(RTC), it extends for 2.5 seconds every hour from 4am to 9pm but it wont retract
the solar rack will sit on top of a pole on a bearing and the actuator is supposed to spin it around the pole during the day and then return it to its original position at night

a friend got this code for me and installed the wire library, im not sure where he got it from

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
 
#include <Wire.h>
#include "RTClib.h"
 
RTC_DS1307 RTC;
 bool MotorRunning = false;
 bool Nighttime = false;
void setup () {
    pinMode(10, OUTPUT);
      pinMode(11, OUTPUT);
  
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // uncomment it & upload to set the time, date and start run the RTC!
    //RTC.adjust(DateTime(__DATE, TIME__));
  }
 
}
 
 
 
void loop () {
    DateTime now = RTC.now();
  
  
  if(now.hour() >= 21 && now.hour() <= 04 )
  {
    if(Nighttime == false)
  {  
  digitalWrite(10, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(30000);              // wait for a second
  digitalWrite(10, LOW);    // turn the LED off by making the voltage LOW 
   Nighttime = true;  
  }  
}
  else
  {      
    Nighttime = false;  
  if (now.minute() >= 59)
    {
      if(MotorRunning == false)
     {
  digitalWrite(11, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(2500);              // wait for a second
  digitalWrite(11, LOW);    // turn the LED off by making the voltage LOW
      MotorRunning = true;
     } 
    }
    else
    {
     MotorRunning = false; 
    }
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
  }
}

this forum section seemed the most appropriate
i googled all last night and was playing with the code last night aswell to see if i could get it to enter “night time” mode and it just wont do it :frowning:
i have looked around at existing threads and none seem to have any possible solutions

i had it all hooked up correctly, all lights came on and it would extend the actuator every hour like it is supposed to but when it reaches the “night time” hour nothing happens
all parts are working correctly so it has to be a code issue
there are no errors

the solar tracker will be completly battery powered
i have 4x 200ah deep cycles for a nominal voltage of 48v and a 5kw 48v inverter to run the fridge and tv

i tried changing a few of the values around and swapping the HIGHs and LOWs around and also playing with the "true"s and "false"s but none of that helped
im actually pretty terrible with coding so its probably something obvious

if there is any more info i need to post please lemme know
thankyou

mspaint circuit

You need to tell us, and post a diagram (hand drawn is fine) showing how the actuator is powered, controlled and wired to the Arduino. I'm guessing that pins 10 and 11 are supposed to control the motor.

Comments like the following are wrong and misleading. What does this statement actually do (motor on time)?

 delay(30000);              // wait for a second

Edit: you posted the diagram just as I was posting, but it is not sufficient. What is the blue box connected to the motor?

welcome to forum.

1)I don’t know why u chosen due board.You could use arduino promini or arduino uno Board for the tracking project.

  1. While selecting RTC DS1307 ; i recommend go with ds3233 RTC because of temperature drift. If you using RTC ds1307 ; i recommend to use GPS sensor which can compensate your temperature drift.
    so you can use GPS & RTC ds1307 oe you can use DS3233 alone.

  2. If you writing code based on time ; first predetermined the length for example
    if it 600mm stroke ; around 12 PM~12:30Pm it must be around 300mm else if you trying to on for every 2.5 sec it get out of syn you may not get exact output

if(now.hour() >= 21 && now.hour() <= 04 )
  {
    if(Nighttime == false)
    {  
      digitalWrite(10, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(30000);              // wait for a second
      digitalWrite(10, LOW);    // turn the LED off by making the voltage LOW 
      Nighttime = true;  
    }  
  }

this night time Your actuator should me in OFF mode & 300MM length . i.e called stow position in solar tracking application.

  1. I recommended you to label your permeter properly So others can understand whats variable used for

for example in your code you declare as

pinMode(10, OUTPUT);// not mentioned what purpose pins are used for
pinMode(11, OUTPUT);//not mentioned what purpose pins are used for

you could Decalre as below given example

int Relay1_ON=10;
int Relay2_ON=11;
Now you can declare these variable in setup so people understand what purpose these pins used for
pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);

Please do these alteration

That is the 30 seconds it takes for the actuator to fully retract its supposed to enter 'night time' mode when the hour is 21 (9pm) which will turn on pin 10 for 30 seconds and then turn off when it comes to the script part im pretty lost

The blue square connected to the actuator motor and 12v battery is a 2 relay board

1: a due is what i already had 2: 1307 i already had also (I can order the different rtc and arduino if u really think it will help) 3 its 150mm stroke that should turn on for 2.5 seconds every hour, it should reset back to fully retracted every day when the tracker goes back to facing east 4 im not sure what u mean, i want it to be fully retracted at night 5 i will try to label stuff in the script as best I can and repost

Hi,
How can;

if(now.hour() >= 21 && now.hour() <= 04 )

now.hour be greater than or equal to 21 AND less than or equal to 4 at the same time?

Tom… :slight_smile:

1: a due is what i already had 2: 1307 i already had also (I can order the different rtc and arduino if u really think it will help) 3 its 150mm stroke that should turn on for 2.5 seconds every hour, it should reset back to fully retracted every day when the tracker goes back to facing east 4 im not sure what u mean, i want it to be fully retracted at night 5 i will try to label stuff in the script as best I can and repost

The idea given for future implementation. t should reset back to fully retracted every day when the tracker goes back to facing eastThis part valid in your case

4 im not sure what u mean, i want it to be fully retracted at night

This is because if tracker is not parallel to surface; assume your structure arbitrary angle load exerted by wind might break your structure,usually with linear actuator it can vary -45 deg to +45 degree in case single axis tracker. if you move to 0 degree then chances of braking structure are less.SO you consider this point while writing code. Then at morning when you start tracking you intialize the angle or retract your actuator.

5) yes you need to label it properly to understand your code.

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
 
#include <Wire.h>
#include "RTClib.h"
 
RTC_DS1307 RTC;
 bool MotorRunning = false;
 bool Nighttime = false;
void setup () {
    pinMode(10, OUTPUT);    // actuator retracts when on
      pinMode(11, OUTPUT);  // actuator extends when on
  
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // uncomment it & upload to set the time, date and start run the RTC!
    //RTC.adjust(DateTime(__DATE, TIME__));
  }
 
}
 
 
 
void loop () {
    DateTime now = RTC.now();
  
  
  if(now.hour() >= 21 && now.hour() <= 04 )
  {
    if(Nighttime == false)
  {  
  digitalWrite(10, HIGH);   // retract relay on
  delay(30000);              // this is how long the actuator will be retracting
  digitalWrite(10, LOW);    // retract relay turns off 
   Nighttime = true;  
  }  
}
  else
  {      
    Nighttime = false;  
  if (now.minute() >= 59)  // actuator is on every hour at this minute
    {
      if(MotorRunning == false)
     {
  digitalWrite(11, HIGH);   // actuator extends
  delay(2500);              // this is how much time the actuator will be extending
  digitalWrite(11, LOW);    // actuator stops
      MotorRunning = true;
     } 
    }
    else
    {
     MotorRunning = false; 
    }
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
  }
}

i hope this is ok

wow i never thought of wind loading as a factor
in that case it should stay facing east during the night and then fully retract back to the east in the morning at 4am, the wind usually comes from west
the panels will be at a 45degree angle there abouts (will manually adjust this in winter and summer)

now.hour be greater than or equal to 21 AND less than or equal to 4 at the same time?

forgive my ignorance, the hours lower than 4 are early morning and the hours higher than 21 are late evening. is this why the actuator isnt retracting?

solved it

so the script was saying; it had to be before 4 and after 21 at the same time to retract i had a play and now its set to before 4

the relay will keep coming on until midnight but the actuator will have already run out of length so its ok

really want to thank you all :)

Congrats with your code.I have doubt code here.During night time When it is fully retracted I.e on Zero postion you are trying to set OFF . Even it reaches final position the linear actuator might on/off for 30S.

Allow your tracker to run continuously for two days as per me you can see the drfit in angle . that u can make ensure using solar curve.Around 12-12:30Pm time it almost reach horizontal to surface. If it diverted that means you code may not give proper value. This you can test setting time in your code.

just a quick question can i power the 2 channel relay board with more than 5v? can i power it with 6.4v ? thanks

arklan: just a quick question can i power the 2 channel relay board with more than 5v? can i power it with 6.4v ? thanks

post the link to the data sheet for the relay. you might want to just put a diode in front of the relay. it will have a voltage drop. but before we can offer any technical answers, the we need the data sheet.