TSL12S control solar tracker

I tried to write code on arduino for controlling my solar tracker 5W by use TSL12S(Light-to-voltge) sensing just 1 piece and want solar tracker stop when it find the most light but it’s not stop. My solar tacker move by servo 2 piece 1 for x-axis and anoter one for y-axis. Is it beacause of my code or because of my sensor.
Give me suggestion, please. This is my senior project.


#include <Servo.h>

int servoxPin = 9;
int servoyPin = 10;

int sensorPin = 0;
int sensorValue = 0;
float voltage = 0;

Servo servox;
Servo servoy;

int posx = 0;
int posy = 0;
int anglex = 0;
int angley = 0;

int Max = 0;

void setup() {
Serial.begin(9600);
servox.attach(servoxPin);
servoy.attach(servoyPin);
}

void loop()
{
for (posx = 0; posx < 180; posx += 45);
{
servox.write(posx);
delay (1000);
sensorValue = analogRead(sensorPin);
float voltage = sensorValue * (5.0/1023.0);
Serial.println(voltage);
if (voltage > Max);
{
Max = voltage;
anglex = posx;
}
}

for (posy = 0; posy < 180; posy += 15);
{
servoy.write(posy);
delay(1000);
sensorValue = analogRead(sensorPin);
float voltage = sensorValue * (5.0/1023.0);
Serial.println(voltage);
if (voltage > Max);
{
Max = voltage;
angley = posy;
}
}
servoy.write(angley);
delay(1000);

for (posx = 180; posx >= 1; posx -= 45)
{
servox.write(posx);
delay (500);
sensorValue = analogRead(sensorPin);
float voltage = sensorValue * (5.0/1023.0);
Serial.println(voltage);
if (voltage > Max);
{
Max = voltage;
anglex = posx;
}
}
servox.write(anglex);
delay(1000);
sensorValue = analogRead(sensorPin);
voltage = sensorValue * (5.0/1023.0);
if (voltage > Max)
{

}
else
{
Max = 0;
loop();
}
}

First of all you dont need to call loop() at the end. Arduino code automatically runs loop() forever.

I suspect all your loop() code should be run once in setup() otherwise your servo will scan the whole sky back and forwards all the time.

Once you get the initial guess of max light position you need to move the servo position a little bit (1 or 2 steps) and see if the light gets more or less. If it gets more then move it a little more in that direction, if less then move it back the other way.

The sun doesnt move very fast so you can allow much longer between position updates once you have found the initial position.

If I move all code in loop() to setup() it will do this action just one time, right? But I want it to move when the light intensity down. How about I use this code but put delay() to make it stop and let sensor read after delay().
I test my sensor with sun light it sends value only 4.95V.

Do you think I should use more 1 sensor like another project that it used 4 LDRs? But I would like to change it by using 2 or 3 sensor(TSL12S)?

Hi,
Please do not double post.
http://forum.arduino.cc/index.php?topic=326316.0

If you use ONE sensor, you will have to do at say 30 minute intervals a sweep E to W and Azimuth to find where your maximum point is, this will probably not increase the efficiency of your array.
A pair for sensors for each axis is more efficient, its just extra code and that is cheap and efficient.

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom… :slight_smile:

Thank you, Tom.

I have another question. Can I use TSL12S instead of LDR that many people use it?

#include <Servo.h>

int servoxPin = 9;
int servoyPin = 10;

int sensorPin = 0;
int sensorValue = 0;
float voltage = 0;

Servo servox;
Servo servoy;

int posx = 0;
int posy = 0;
int anglex = 0;
int angley = 0;

int Max = 0;

void setup() {
  Serial.begin(9600);
  servox.attach(servoxPin);
  servoy.attach(servoyPin);
  
}

void loop() 
{
  for (posx = 0; posx < 180; posx += 45);
  {
    servox.write(posx);
    delay (1000);
    sensorValue = analogRead(sensorPin);
    float voltage = sensorValue * (5.0/1023.0);
    Serial.println(voltage);
    if (voltage > Max);
    {
      Max = voltage;
      anglex = servox.write();
    }
  }
    
  
  for (posy = 0; posy < 180; posy += 15);
  {
    servoy.write(posy);
    delay(1000);
    sensorValue = analogRead(sensorPin);
    float voltage = sensorValue * (5.0/1023.0);
    Serial.println(voltage);
    if (voltage > Max);
    {
      Max = voltage;
      angley = servoy.read();
    }
  }
  servoy.write(angley);
  delay(1000);
  
  
  for (posx = 180; posx >= 1; posx -= 45)
  {
    servox.write(posx);
    delay (500);
    sensorValue = analogRead(sensorPin);
    float voltage = sensorValue * (5.0/1023.0);
    Serial.println(voltage);
    if (voltage > Max);
    {
      Max = voltage;
      anglex = servox.read();
    }   
  }
  servox.write(anglex);
  delay(1000);
  sensorValue = analogRead(sensorPin);
  voltage = sensorValue * (5.0/1023.0);
  if (voltage > Max)
  {
    
  }
  else
  {
    Max = 4.95;
    loop();
  }
}



/*}
servox.write(anglex);
servoy.write(angley);
delay(10000);
sensorValue = analogRead(sensorPin);
voltage = sensorValue * (5.0/1023.0);
if (voltage >= Max)
{
  servox.write(anglex);
  servoy.write(angley);
}
else
{
  loop();
}*/

Hi, I'd would say yes, if you put tubes over them to give some direction to the sensing. You would have to check if they are going to saturate if exposed to the open sky. If they do then you will have problems finding the brightest heading, you may have to filter them to reduce their sensitivity.

Tom.... :)