Arduino Solar Tracker

Updates: 3

Hey guys. This is my first post on this forum and I am sharing with you guys my arduino solar tracking project. I am building a solar panel that will track the sun and adjust its position accordingly. I plan to use:
4 photo-resistor sensors
2 servo motors (lynx motion pan and tilt) - bought from robotshop
arduino uno and a few other things.

UPDATE:2
Ok, so I have 4 photo resistor sensors hooked up (top, bottom, left and right) and i have them hooked up to my 2 servos (2 each respectively).Basically my code works by finding the highest input from the sensors and the servos will move accordingly. This is so I can balance my servos at roughly the position of the light source.It is working correctly and I am going into a showcase for my school later today.

Update 3:
So I havent changed the code much since my last update but I have been working on plotting the data on graphs so i can more easily troubleshoot a few of the small problems ive been having. Ive attached a picture below of graphs I have made. One issue I am having is that my sensors are not consistent. One is really jumpy and another will display different values each time it is reset. Its kind of annoying but it might be because of how cheap they are. I am thinking of getting some more to see if that is problem. Any way, the software to plot the data is called bridge control panel from Cypress. Seems to work well and was easy enough to install and get working.

Heres a picture of it and the code is below


Let me know what you guys think or if you guys have any suggestions as this is my first arduino project. I will be updating this thread as the project develops

ignore the comments in the code as they may not be correct.

//Leathels code

#include <Servo.h> 
 
Servo topServo;  // create servo object to control a servo 
Servo bottomServo;
                // a maximum of eight servo objects can be created 
int lightPinLeft = 1;  //define a pin for Photo resistor 
int lightPinRight = 4;  //define a pin for Photo resistor
int lightPinTop = 5;  //define a pin for Photo resistor 
int lightPinBottom = 0;  //define a pin for Photo resistor
int topPos = 0;    // variable to store the servo position 
int bottomPos =0; 
int reset = 0;
void setup() 
{ 
  Serial.begin(9600);  //Begin serial communcation
  bottomServo.attach(3);  // attaches the servo on pin 9 to the servo object 
  topServo.attach(2);
} 
 
 
void loop() 
{ 
  delay(150);
  int LSleft =analogRead(lightPinLeft)-20;
  int LSright =analogRead(lightPinRight);
  int LStop =analogRead(lightPinTop)-320;
  int LSbottom =analogRead(lightPinBottom);
  
  // RX8 [h=43] @1Key1 @0Key1
    Serial.print("C");
    Serial.write(LStop>>8);
    Serial.write(LStop&0xff);
    
    Serial.write(LSbottom>>8);
    Serial.write(LSbottom&0xff);
    
    Serial.write(LSright>>8);
    Serial.write(LSright&0xff);
    
    Serial.write(LSleft>>8);
    Serial.write(LSleft&0xff);
  
   if  (LSright < LSleft&& bottomPos <180 && LSleft-LSright >25 && reset ==0) // while right is less than left pos goes down 1
  {     
  
    bottomPos++;
    bottomServo.write(bottomPos);   
  } 
  
  if  (LStop < LSbottom&& topPos>100 && LSbottom-LStop >15 && reset ==0) // while right is less than left pos goes down 1
  {     
  
    topPos--;
    topServo.write(topPos);   
  } 
  
  if (LSright > LSleft && bottomPos > 0 && LSright-LSleft >25 && reset ==0)    // while right is  more than left  pos goes up 1
  {  
  
    bottomPos--;    
    bottomServo.write(bottomPos);    
        
  } 
  
  if (LStop > LSbottom && topPos < 170 && LStop-LSbottom >15 && reset ==0)    // while right is  more than left  pos goes up 1
  {  
  
    topPos++;    
    topServo.write(topPos);    
        
  } 
  
  if (LStop-LSbottom <30 || LSbottom-LStop <30 && topPos >170&& reset ==0)
    {
      topPos--;
      topServo.write(topPos);
    }
    
    if (LStop-LSbottom <30 || LSbottom-LStop <30 && topPos <170 && reset ==0)
    {
      topPos++;
      topServo.write(topPos);
    }
    
    if(topPos >=169 && LStop > LSbottom)
    {
      reset ==1;
    }
    
    if (reset==1 && bottomPos >=10)
      {
        bottomPos--;
      }
      
    if (bottomPos <=10)
    {
      reset==0;
    }

    Serial.print ("top=");
    Serial.println (LStop);
    Serial.print ("bottom=");
    Serial.println (LSbottom);
    Serial.print ("rightpos=");
    Serial.println(LSright);
    Serial.print("leftpos=");
    Serial.println(LSleft);
    
}
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created

Naming an instance to match it's purpose is more useful. If the servo is for pan or tilt, name it accordingly.

I notice that you don't have any variables named myint or myfloat or mybyte or mychar, so myservo needs to go, too.

   if  (LSright < LSleft&& pos>0) // while right is less than left pos goes down 1

Consistent indenting is good. Consistent spacing is good.

Hey thanks very much for the tips!

Updated!

      reset==0;

No.

PaulS:

      reset==0;

No.

Yea that particular function doesnt work and I cant seem to figure out why. Should it just be =0? or should it be = reset-1? because that would be a handy feature to have

Yea that particular function doesnt work and I cant seem to figure out why. Should it just be =0?

Yes.

or should it be = reset-1? because that would be a handy feature to have

You only have two values, 0 and 1, that you assign to reset or that you expect reset to have. I guess I don't see a lot of difference between reset = 0 and reset = reset - 1, when reset is currently 1.

But, if you want to use reset = reset - 1 (or reset--), feel free.

Don't forget to fix the reset == 1; statement, too.

I see you have bottomPos = bottomPos++; and others like it
You just need bottomPos++;

Thanks for the tips guys, I'll update the code soon. I'm working on something that will record the light values and position of the servos on a graph. Does anyone have any good software to recommend for that?

Post has been updated!!

Post has been updated!!

Wrong. When you do that, the people that commented on mistakes in the original code look like idiots.

Why do people use sensors to track the position of the sun when we know where it is and accurate equations have been developed to calculate the position of it.

I would recommend google to find the equations that will give you the position of the sun in the sky based on your location on the earth and time of day and point you mirror that direction. Granted this way you will have to do some geometry work on paper, or similar, but once that is done and verified you are done. I believe this is a safer, and faster method in addition your part count and circuit complexity drops and this is always a great. Getting the same thing done with a lower part count is my idea of a very good thing.

One cautionary tip is remember that all of the standard trig functions take the angular argument in radians and also return the angular result in radians, so you will need to convert from the degrees you probably use to radians. Failure to use the correct angular unit in trig functions will cause you a nightmare or two, I started making that mistake about 35 years ago and seem to make it about every 7 years since.

PaulS:

Post has been updated!!

Wrong. When you do that, the people that commented on mistakes in the original code look like idiots.

@PaulS- That is not my intention at all. Its easier for me to sum up what I have done if its in the original post. I apologize if people feel that way :(. I mentioned in the original post that I would be "updating" it as it progressed, so that's what I have been doing. I truly do appreciate all tips given to me by you guys.

@wwbrown- You know I thought about doing that originally, but because I was moving it around a lot and for demonstrational purposes, that would have been tough. However I agree with you about the complexity of it all going down(when it comes to hardware). Because of how tricky and inaccurate these light sensors can be I am thinking of going a different route with this tracker, perhaps I'l try what you suggested.

Another method I really want to try is using an xbox kinect to control it. I just need to buy a kinect first haha, Il update this post if I end up doing something like that :slight_smile: