Help with coding dual-axis solar tracking robot using Arduino Uno, 2 servo motors, 4 LDRs, RTC, and SolarCalculator.h library

Hi everyone,

I'm trying to build a solar tracking robot using an Arduino Uno. I have two servo motors that I want to use to control the movement of the robot in both horizontal and vertical directions. I also have four LDRs (Light Dependent Resistors) to detect the intensity of sunlight from different angles.

I want to use the RTC (Real Time Clock) module to keep track of time and make the robot move accordingly. I have found the SolarCalculator.h library, which I believe can help me calculate the position of the sun based on the current time and location.

However, I'm new to coding with Arduino and I'm not sure how to combine all these components to make the robot work as intended. Can anyone provide me with some guidance or sample code to get started?

Here are the things I have:

Arduino Uno
Two servo motors
Four LDRs
RTC module
SolarCalculator.h library

Any help would be greatly appreciated. Thank you in advance!

The code is here below:

#include <Servo.h>
#include <Wire.h>
#include "RTClib.h"
#define LDR1 A0
#define LDR2 A1
#define LDR3 A2
#define LDR4 A3

RTC_DS1307 rtc;

Servo horizontalServo;
Servo verticalServo;

void setup() {
  pinMode(LDR1, INPUT);
  pinMode(LDR2, INPUT);
  pinMode(LDR3, INPUT);
  pinMode(LDR4, INPUT);
  
  horizontalServo.attach(9);
  verticalServo.attach(10);

  Wire.begin();
  rtc.begin();


void loop() {
  int ldr1Value = analogRead(LDR1);
  int ldr2Value = analogRead(LDR2);
  int ldr3Value = analogRead(LDR3);
  int ldr4Value = analogRead(LDR4);
  
}

Work on one device at a time until you fully understand how and why it works with sample code. That will usually be from the sample code that comes with your IDE, your Arduino developemtn package when you install it.
What you have exercised each device, begin to work on your final software program and add one device at a time.

1 Like

I suggest to first learn how to control the servos (use a separate power supply for them), then build the solar tracker framework.

Figuring out how to position the servos, so that the tracker accurately points in a specified direction, is not trivial.

Incidentally, in order to track the sun using the SolarPosition library (which I recommend over SolarCalculator), the tracker needs to be manually and accurately aligned with the altitude and azimuth coordinate system, i.e. true North and the horizon.

Your void setup() needs a close brace. --> }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.