Go Down

Topic: LDR SENSING RELAY CIRCUIT (Read 3151 times) previous topic - next topic

peterengland is Here

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));
}
}
-----------------------------------------------------------------------------------------------------------------------
Peter.

JimboZA

Maybe you could experiment with the threshold value which is 600 at the moment?
My hovercraft is full of eels.

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.

peterengland is Here

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 ?
Peter.

seadanzig

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

peterengland is Here

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... :) 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-------------------------------------------------------------------------
Peter.

Klaus_ww

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

peterengland is Here

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

Christo


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:
Code: [Select]

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...

peterengland is Here

Thanks for code I will Check it.
Peter.

Go Up