Pages: [1]   Go Down
Author Topic: LDR SENSING RELAY CIRCUIT  (Read 2725 times)
0 Members and 1 Guest are viewing this topic.
HK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Micro
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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));
}
}
-----------------------------------------------------------------------------------------------------------------------
Logged

Peter.

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 91
Posts: 4262
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

HK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Micro
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Peter.

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

HK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Micro
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Peter.

Offline Offline
God Member
*****
Karma: 12
Posts: 583
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

HK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Micro
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Peter.

South Africa
Offline Offline
Newbie
*
Karma: 1
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

HK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Micro
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for code I will Check it.
Logged

Peter.

Pages: [1]   Go Up
Jump to: