Category Archives: arduino

Solar Tracker Code

As requested, here is the .c Arduino code to the Solar Tracker.  I haven’t uploaded it and tested it in a while, but from what I remember, the program works well for a single axis rotation.  With indoor lighting, it should work fine, but outside the Sun light might be too bright and max out the reading from the photoresistors.

Stephane Juan made found a nice solution to this problem.  He placed tubes around the photoresistors to cast a shadow over the resistors when the sun isn’t directly above.  The angle of the shadow is determined by the height of the tubes.  Here’s a picture of his setup, http://stephane.jouin.free.fr/suiveur_solaire.JPG

And without further adieu, here’s the Solar Tracker Code

/*
Solar Tracker
This program controls a servo to allow 
solar cells to always face the source of light
Created 1/18/11
Ian Bohannon
Copyright 2011
*/

#include <Servo.h>

int ServoOut = 9;
int photoPinLeft = 0;
int photoPinRight = 5;
int LeftReading;
int RightReading;
int pos;
int tolerance = 5; /* +/- the total difference 
between the two photoresistors. You can test 
this by covering both the resistors in total darkness, 
and note the difference between the two readings. */

Servo myservo;

void setup()
{
myservo.attach(ServoOut);
Serial.begin(9600);
}

void loop()
{
LeftReading = analogRead(photoPinLeft);
RightReading = analogRead(photoPinRight);

Serial.print("Left Reading = ");
Serial.println(LeftReading);
Serial.print("Right Reading = ");
Serial.println(RightReading);

if(LeftReading > RightReading + tolerance)
{
pos = 58;
}
else if(LeftReading + tolerance < RightReading)
{
pos = 52;
}
else
{
pos = 55;
}

myservo.write(pos);
delay(120);
}
Advertisements