I'm using LDR's for a single axis solar tracker, and it appears that I need some LDR sensor guidance to one point.
I've been through multiple tutorials that have the resistor wired differently. It seems like just about every other one is doing it differently, and it has seemed at times that the analog pins behave differently from one device to another.
Some tutorials/projects have the resistor wired from analog pin to ground, and some analog pin to 5v.
After working on many different combinations, it appears that analog pin to 5v is returning the most consistent results, with my LDR's returning a lower serial value as light is increased.
Wiring from analog pin to ground, seems to return varying results on different boards, where I have tried nano, nano 33 IOT, and most recently, a new R4 wifi.
Is there a right way to wire this ? This that follows was actually working code on a nano that was wired analog - resistor - ground and that moved a linear actuator, pushing the panel up or down. There is a divider between the LDR's aligned with the sun path.
It was prototype soldering, so I was in the process of rebuilding it, and moving from a base nano to the nano 33 IOT, as I want to be able to save the LDR readings, and add web buttons to manually move the panels from the phone app as well, and after re-soldering, started getting unexpected results.
int sensorValue1;
int sensorValue2;
const int Extend = 9;
const int Retract = 6;
int sensorPin1 = A6;
int sensorPin2 = A3;
int counter = 0;
//int valAverage1 = 0;
//int valAverage2 = 0;
int numAverage = 10;
int threshold = 50;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
}
void loop() {
// Your code here
//valAverage1 =0;
//valAverage2 =0;
for (counter = 0; counter < numAverage; counter++) {
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
//valAverage1 = valAverage1 + sensorValue1;
//valAverage2 = valAverage2 + sensorValue2;
}
//valAverage1 = valAverage1/numAverage;
//valAverage2 = valAverage2/numAverage;
//Serial.print(valAverage1, DEC);
//Serial.print(", ");
//Serial.println(valAverage2, DEC);
if (sensorValue1 > sensorValue2 + threshold)
{
digitalWrite(Extend, LOW);
digitalWrite(Retract, HIGH);
delay(2000);
}
else if (sensorValue2 > sensorValue1 + threshold)
{
digitalWrite(Retract, LOW);
digitalWrite(Extend, HIGH);
delay(2000);
}
else(sensorValue2 + sensorValue1 < 5);
digitalWrite(Extend, LOW);
digitalWrite(Retract, HIGH);
delay(1000);
Serial.print("sens_1 ");
Serial.print(analogRead(sensorPin1));
Serial.println();
Serial.print("sens_2 ");
Serial.print(analogRead(sensorPin2));
Serial.println();
delay(5000);
}
