LDR SENSING RELAY CIRCUIT

I Need help in another project : i am using LDR and arduino uno and 2 relay module when LDR detects low light it trigger the relay my problem is when slight change in LDR value the relay gets flickering like fast on/ off how to get steady on or steady off ?? the relay module pin is connected to pin 12 on arduino uno... i am using the code below ..


void setup() {
pinMode(4, OUTPUT);
pinMode(12, OUTPUT);
//Serial.begin(9600);
}

void loop(){
digitalWrite(4, HIGH);
if(analogRead(0) < 600){
digitalWrite(12, LOW);
} else{
digitalWrite(12, HIGH);
//Serial.println(analogRead(0));
}
}

Maybe you could experiment with the threshold value which is 600 at the moment?

I have checked all values that mach but new success .. i need the output as on or off not fluctuating if so the relay also fluctuates and make flickering or chirping sound ..any idea ?

Hi try adding a delay at the end. The analogue read statement is reading it faster than yiu can blink

Hey !! seadanzig You given the perfect answer what i am looking for now my Relay project is perfectly does what i need you genius.. and i have added the delay for the ON state of relay also for 10 Secs... :slight_smile: by the way i am now using following revised code .. thanks for perfect solution thanks ... bye.

------------------------------------------code from here -------------------------------------------------------------------------------------------------
void setup() {
//pinMode(4, OUTPUT);
pinMode(12, OUTPUT);
//Serial.begin(9600);
}

void loop(){
//digitalWrite(4, HIGH);
if(analogRead(0) < 590){
digitalWrite(12, LOW);
delay (10000);
} else{
digitalWrite(12, HIGH);
delay (100);
//Serial.println(analogRead(0));
}
}

---------------------------------------------------------------code end-------------------------------------------------------------------------

Another way without blocking the arduino during delay is to program a hysteresis.

Hi , Klaus_ww please tell me some information about hysteresis its new concept for me thanks.

ganeshkubade:
Hi , Klaus_ww please tell me some information about hysteresis its new concept for me thanks.

Hysteresis is where there is a dead band in your logic, for instance you could switch on at a reading of 590 and switch off at say 610 giving a dead band of +-10. Your original code could be rewritten as follows:

int reading;
int deadband = 10;
int setpoint = 600;
reading = analogRead(0);
if(reading < (setpoint - deadband)){
digitalWrite(12, LOW);
} else if(reading > (setpoint + deadband)){
digitalWrite(12, HIGH);
}

Note: untested code...

Thanks for code I will Check it.