PAGE 1 | GREEN ENERGY HANDBOOK © 2008-2018 M I C H A E L E T T E R S H A N K
https://www.robotscience.co.za
GREEN ENERGY: WORKSHOP 8.4 SUN TRACKING HARDWARE AND ARDUINO CODE
Now we can set up our hardware for the sun tracker base that will support the solar panel as it tracks the ‘sun’ without
actually worrying about the solar panel at this stage. We can fit a solar panel later.
Note in the above diagram, as per schematic convention, wires that cross over each other are not connected. Note also
how one side of each LDR is connected to +5v. The other side of each LDR connects to an analogue input on the Arduino
and a 10 kilo ohm PULL DOWN resistor that is connected to common ground [GND] or -0v.
The LDRs are fitted into a light baffle that separates them so that when the sun moves it will cast a shadow over some of
the LDRs and not others creating a differential that when the code detects an LDR is in shade it knows which way to move
the servo in order to restore a situation in which the solar panel or mount area where the solar panel will go is facing as
directly as possible towards the sunlight.
PAGE 2 | GREEN ENERGY HANDBOOK © 2008-2018 M I C H A E L E T T E R S H A N K
https://www.robotscience.co.za
LIGHT SENSITIVE PHOTO TRANSISTOR DEMO ROBOT USING THE ANALOGUE INPUT PINS
#include <Servo.h>
Servo myservo1, myservo2;
int LDR1 = A0, LDR2 = A1, LDR3 = A2, LDR4 = A3;
int rRDL1 = 0, rRDL2 = 0, rRDL3 = 0, rRDL4 = 0;
int max1=0, max2=0, max3=0;
int ser1 = 80, ser2=0;
void setup() {
myservo1.attach(9);
myservo2.attach(8);
Serial.begin(9600);
myservo1.write(ser1);
myservo2.write(100);
}
void loop() {
rRDL1 = analogRead(LDR1) / 100;
rRDL2 = analogRead(LDR2) / 100;
rRDL3 = analogRead(LDR3) / 100;
rRDL4 = analogRead(LDR4) / 100;
max1 = max(rRDL1, rRDL2);
max2 = max(rRDL3, rRDL4);
max3 = max(max1, max2);
//Serial.println(String(max3));
//Serial.println(String(rRDL1) +", "+String(rRDL2) +", "+String(rRDL3) +", "+String(rRDL4));
if(rRDL1<max3 && rRDL2<max3){
if(ser1<140)
ser1+=1;
myservo1.write(ser1);
}
if(rRDL3<max3 && rRDL4<max3){
if(ser1>0)
ser1-=1;
myservo1.write(ser1);
}
if(rRDL2<max3 && rRDL3<max3){
Serial.println("servo2 +" + String(ser2));
if(ser2<180)
ser2+=1;
myservo2.write(ser2);
}
if(rRDL1<max3 && rRDL4<max3){
Serial.println("servo2 -" + String(ser2));
if(ser2>0)
ser2-=1;
myservo2.write(ser2);
}
delay(15);
}