Solar Tracker Servo Repositioning

Hi,
I made solar tracker and I want to make a switch to on and off solar tracker.
When I on that tracker it will start following sun and when I off the Tracker so servo will come to their original position.
I am Using ESP32, Servo motor MG946R - 4 no’s, External power source for Servo motor from Battery Shield 2 cell which gives duel output
3.3 volt = 4 no’s and
5 volt = 4 nos.
Servo connected with 5 volt output.

Can anyone tell me what code should I use

You haven't said what tracks the sun.
Did you pre-calculate based on lat/long?

Welcome to the forum

This forum is not a code writing service although there is a category in which yo can post and ask for paid help

The forum works best when you do your best to get your project working and come here to seek help for problems that you have

The best advice I can give is to write your code in sections and test each component rather than trying to do everything at once

What programming and electronics experience do you have ?

To track Sun I am using 4 LDR

And yet, you didn't think to mention them.
Or your code.

have you tried doing a search for "solar tracker"? this has come up a few times

See this project:

" https://www.instructables.com/Arduino-Solar-Tracker/

This WEB has it all, just look for it.........

below Is my Code

#define BLYNK_TEMPLATE_NAME "{redacted}"
#define BLYNK_AUTH_TOKEN "{redacted}"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <WiFiClientSecure.h>

char ssid[] = "{redacted}"
char pass[] = "{redacted}"

#include <ESP32Servo.h>
#include <time.h>
#include "ACS712.h"
#include "DHT.h"
#define DHTTYPE DHT22
#define DHTPIN 4 // Temparture and Humididty Sensor of Boost Converter
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

unsigned long previousMillis = 0;
unsigned long interval = 30000;

const char* host = "script.google.com"; // Host & httpsPort
const int httpsPort = 443;

WiFiClientSecure Client;
const char* fingerprint = "{redacted}";
String GAS_ID = "{redacted}"; //--> spreadsheet script ID

int solarvoltage = 34; // Solar Voltage Sensor
int batteryvoltage = 39; // Battery Voltage Sensor
float correctionfactor = 1.1;
float vout = 0.0;
float vin = 0.0;
float R1 = 30000 + 150000;
float R2 = 7500;

int value = 0.0;

int solarcurrent = 35; // Solar Current Sensor

int batterycurrent = 36; // Battery Current Sensor

ACS712  ACS(35, 5.0, 4095, 66);

double voltage = 0.0;

double current = 0.0;


int ldrlt =36; //LDR top left 

int ldrrt =39; //LDR top  Right 

int ldrld =34; //LDR down left

int ldrrd =35; //ldr down Right 

Servo horizontal; // horizontal servo

int servoh = 90;  // start at degree    

int servohLimitHigh = 180;

int servohLimitLow = 0;

// 65 degrees MAX

Servo vertical; // vertical servo

int servov = 90;  // start at degree    

int servovLimitHigh = 180;

int servovLimitLow = 0;

void initWiFi() {

  WiFi.mode(WIFI_STA);

  WiFi.begin(ssid, pass);

  Serial.print("Connecting to WiFi ..");

  while (WiFi.status() != WL_CONNECTED) {

    Serial.print('.');

    //delay(1000);

  }

  unsigned long currentMillis = millis();

  //if WiFi is down, try reconnecting every CHECK_WIFI_TIME seconds

  if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >=interval)) {

    Serial.print(millis());

   Serial.println("Reconnecting to WiFi...");

   WiFi.disconnect();

   WiFi.reconnect();

   previousMillis = currentMillis;

  }

  Serial.println(WiFi.localIP());

}

BLYNK_WRITE(V0)

{

  int value = param.asInt();

  Serial.print(value);

  if(value == 0)

  {

    digitalWrite(23, LOW); // Solar Power On Off

    Serial.println(" : Solar Off : ");

  }

  if(value == 1)

  {

     digitalWrite(23, HIGH);

     Serial.println(" : Solar On : ");

  }

}

BLYNK_WRITE(V1)

{

  int value = param.asInt();

  Serial.print(value);

  if(value == 0)

  {

    digitalWrite(22, LOW); // Battery Charging Power On Off

    Serial.println(" : Battery Off");

  }

  if(value == 1)

  {

     digitalWrite(22, HIGH);

     Serial.println(" : Battery On");

  }

}

BLYNK_WRITE(V2)

{

  int value = param.asInt();

  Serial.print(value);

  if(value == 0)

  {

    digitalWrite(21, LOW); // Cooling Fan for Boost Converter Power On Off

    Serial.println(" : Fan Off");

  }

  if(value == 1)

  {

     digitalWrite(21, HIGH);

     Serial.println(" : Fan On");

  }

}

BLYNK_WRITE(V3)

{

  int value = param.asInt();

  Serial.print(value);

  if(value == 0)

  {

  digitalWrite(36, LOW); // LDR Top Left Solar Tracker Power On off

  digitalWrite(39, LOW); // LDR Top Right Solar Tracker Power On off

  digitalWrite(34, LOW); // LDR Bottom Left Solar Tracker Power On off

  digitalWrite(35, LOW); // LDR Bottom Right Solar Tracker Power On off

  Serial.println(" : Solar Tracker Off");

  }

  if(value == 1)

  {

  digitalWrite(36, HIGH); // LDR Top Left Solar Tracker Power On off

  digitalWrite(39, HIGH); // LDR Top Right Solar Tracker Power On off

  digitalWrite(34, HIGH); // LDR Bottom Left Solar Tracker Power On off

  digitalWrite(35, HIGH); // LDR Bottom Right Solar Tracker Power On off

  Serial.print(" : Solar Tracker On");

 }

   //delay(100);

}

void Sensorvalue1()

{

  int sdata = 0.0;

  value = analogRead(solarvoltage);

  vout = (value * 5.0) / 4096.0;

  vin = vout / (R2/(R1+R2));

  vin = vin - correctionfactor;

if (vin < 0.01) {

  vin = 0.0;

}

  Serial.print("Solar Voltage= ");

  Serial.println(vin, 2);

  sdata = vin;

  Blynk.virtualWrite(V4, sdata);

  //delay(100);

}

void Sensorvalue2()

{

  int sdata = 0.0;

  value = analogRead(batteryvoltage);

  vout = (value * 3.3) / 4096.0;

  vin = vout / (R2/(R1+R2));

  vin = vin - correctionfactor;

if (vin < 0.01) {

  vin = 0.0;

}

  Serial.print("Battery Voltage= ");

  Serial.println(vin, 2);

  sdata = vin;

  Blynk.virtualWrite(V5, sdata);

  //delay(100);

}

void Sensorvalue3(){

  for(int i = 0.0; i < 1000; i++);

  {

      voltage = (voltage + (0.0264 * analogRead(35)));

  //delay (100);

      }

  voltage = voltage / 1000;

current = (voltage - 2.5) / 66;

 

  if (current < 0.01) {

  current = 0.0;

  }

  Serial.print("Solar Current= ");

  Serial.println(current, 2);

  Blynk.virtualWrite(V6, current);

  //delay(100);

}

void Sensorvalue4(){

  for(int i = 0.0; i < 1000; i++);

    {

      voltage = (voltage + (0.0264 * analogRead(36)));

  //delay (100);

      }

  voltage = voltage / 1000;

current = (voltage - 2.5) / 66;

 

  if (current < 0.01) {

  current = 0.0;

}

  Serial.print("Battery Current= ");

  Serial.println(current, 2);

  Blynk.virtualWrite(V7, current);

  //delay(100);

  }

 

void Sensorvalue5()

{

  float t = dht.readTemperature();

  float h = dht.readHumidity();

  if (isnan(h) || isnan(t)) {

  Serial.println("Failed to read from DHT sensor!");

  return;

  }

    Blynk.virtualWrite(V8, t);

    Blynk.virtualWrite(V9, h);

    Serial.print("Temperature : ");

    Serial.println(t, 2);

    Serial.print("Humidity : ");

    Serial.println(h, 2);

    //delay(100);

}

void Sensorvalue7()

{

int lt = analogRead(ldrlt); // top left

int rt = analogRead(ldrrt); // top right

int ld = analogRead(ldrld); // down left

int rd = analogRead(ldrrd); // down right

int dtime = 10; // dtime=diffirence time,

int tol = 100; // LDR Value tol=toleransi

int avt = (lt + rt) / 2; // average value top

int avd = (ld + rd) / 2; // average value down

int avl = (lt + ld) / 2; // average value left

int avr = (rt + rd) / 2; // average value right

int dvert = avt - avd; // check the diffirence of up and down

int dhoriz = avl - avr;// check the diffirence og left and rigt

  if (-1*tol > dvert || dvert > tol)

 {

 if (avt > avd)

 {

   servov = ++servov;

 if (servov > servovLimitHigh)

 {servov = servovLimitHigh;}

 }

 else if (avt < avd)

 {servov= --servov;

 if (servov < servovLimitLow)

 { servov = servovLimitLow;}

 }

 vertical.write(servov);

 }

if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle

 {

 if (avl > avr)

 {

 servoh = --servoh;

 if (servoh < servohLimitLow)

 {

 servoh = servohLimitLow;

 }

 }

 else if (avl < avr)

 {

 servoh = ++servoh;

 if (servoh > servohLimitHigh)

 {

 servoh = servohLimitHigh;

 }

 }

 else if (avl = avr)

 {

 //delay(1000);

 }

 horizontal.write(servoh);

 }

   

Serial.print("Average Value Top : ");

Serial.println(avt);

Serial.print("Average Value Down : ");

Serial.println(avd);

Serial.print("Average Value Left : ");

Serial.println(avl);

Serial.print("Average Value Right : ");

Serial.println(avr);

//Serial.print("time diffirence ");

//Serial.println(dtime);

Serial.print("Vertical Servo Position : ");

Serial.println(servov);

Serial.print("Horizontal Servo Position : ");

Serial.println(servoh);

//Serial.print("LDR Value tolerans ");

//Serial.println(tol);

//delay(dtime);

}      

void setup()

{

Serial.begin(9600);

Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

WiFi.begin(ssid, pass);

initWiFi();

dht.begin(); // Initialize the DHT sensor

horizontal.attach(18);

horizontal.attach(16);

horizontal.write(90);

vertical.attach(17);

vertical.attach(5);

vertical.write(90);

pinMode(23,OUTPUT); // Solar Power On Off

pinMode(22,OUTPUT); // Battery Charging Power On Off

pinMode(21,OUTPUT); // Cooling Fan for Boost Converter Power On Off

pinMode(36,INPUT); // LDR Top Left Solar Tracker Power On off

pinMode(39,INPUT); // LDR Top Right Solar Tracker Power On off

pinMode(34,INPUT); // LDR Bottom Left Solar Tracker Power On off

pinMode(35,INPUT); // LDR Bottom Right Solar Tracker Power On off


pinMode(35,INPUT); // Solar Current Sensor

pinMode(36,INPUT); // Battery Current Sensor

pinMode(34,INPUT); // Solar Voltage Sensor

pinMode(39,INPUT); // Battery Voltage Sensor

pinMode(4,INPUT); // Temparture and Humididty Sensor of Boost Converter



  timer.setInterval(50L,  Sensorvalue7); // Solar Tracker LDR and Servo
  timer.setInterval(123L,  Sensorvalue1); // Solar VOltage Sensor
  timer.setInterval(143L,  Sensorvalue2); // battery Voltage Sensor
  timer.setInterval(163L,  Sensorvalue3); // Solar Current Sensor
  timer.setInterval(183L,  Sensorvalue4); // Battery Current Sensor
  timer.setInterval(5000L,  Sensorvalue5); // Temprature and Humidity Sensor
}

void loop()
{
  timer.run(),
  Blynk.run();
  }


ok, i will check this

thanks

What's with the crazy double line spacing?

Don't tell me - an AI wrote it, and ran away.

Comments are supposed to clarify - not confuse!

Better still, use names that don't need clarification!

1 Like

sorry for inconvenience, i am new in this forum and don't know how to post code, but i will improve soon.
AI not Write this code and Run away, nice Joke

those are Shortcut and comments are their full form to identify LDR top left or Right or Bottom Left or Right. it is easy to identify

I checked this project and some other project, but not found anything which explain how to Reposition Servo motor on their original position when require,and again start tracking to sun when require.

Hello ajinkya_udamale

Use a local clock to follow the sun simply.

Have a nice day and enjoy coding in C++.

This suggestion:

Intended this:

to be more like this:

int ldrTopLeft     =36;
int ldrTopRight    =39;
int ldrBottomLeft  =34;
int ldrBottomRight =35;

My tracker is working fine, I only need to On solar tracker when need and when I don't need I will off tracker and servo will come to their original position.
I don't know what code will work for this action

Ok,
Thanks for advice

You should have only one pin attached to horizontal servo (unless you have two trackers)

  horizontal.attach(18);
  horizontal.attach(16);

You should have only one pin attached to vertical servo (unless you have two trackers)

  vertical.attach(17);
  vertical.attach(5);

These two commands send your servos "home" (original position)

  horizontal.write(90);
  vertical.write(90);

past threads with east and west LDRs, recognized the last direction a move was made and when both LDRs indicated dark (i.e. sunset), the system moved back to the pre-dawn position

in that position and during the day, the tracker is repositioned when the west facing LDR is more active than the east LDR and the tracker moved until both LDRs were equal