Hello I have a problem in my project while I using ldr sensor firstly I used the ldr to detect the sun light then from this reading it should make the motor run and if there is no sun the motor should stop , now while I use the ldr sensor it’s readings vary with no cause and varies randomly even when I cover the ldr sensor it give me reading then when I in cover it suddenly give me 0 and make the motor stop suddenly and I don’t know where is the problem here is my code and even I used different resistors for my installing such as 8,2 and 15 ,18, 220, 10k ohms and nothing changed
Here is my code
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
light=analogRead(A0);
Serial.println(light);
delay(500);
}*emphasised text*
I assume that the top red jumper comes from 5V on the Arduino. If so, then it is the other side of the break in the power rails so it is not connected to the red wire from the LDR
Here's an LDR test program with smoothing, I used a 33k resistor.
uint32_t
tStart, // timer start
tEnd = 2000; // update time in mS
int
total, // sum of samples
current,
avg; // average
const byte
numSamples = 8, // number of samples for smoothing
aInPin = A0;
void setup()
{
Serial.begin(9600);
for(int i = 0;i < numSamples;i++) // for smoothing, fill total with
total += analogRead(aInPin); // numSamples * current reading
avg = total / numSamples;
}
void loop()
{
if(millis() - tStart > tEnd)
{
tStart = millis(); // reset timer
total -= avg; // make room for new reading
current = analogRead(aInPin);
Serial.print(current); Serial.print("\t");
total += current; // add new reading
avg = total / numSamples; ;
Serial.println(avg);
}
}