Relay switching ON/OFF rapidly by the arduino digital signal ..how to controll?

Kindly, my circuit is (Sensor===>Arduino===>Relay) the relay still switching ON/OFF rapidly ...i tried to calibrate the input voltage range and made
delay time to read the new value ....but the problem not solved ... any idea about how to solve this problem?

dany151:
Kindly, my circuit is (Sensor===>Arduino===>Relay) the relay still switching ON/OFF rapidly ...i tried to calibrate the input voltage range and made
delay time to read the new value ....but the problem not solved ... any idea about how to solve this problem?

You probably need to post your code. Probably a lack of using a software hysteresis in your switch command.

Lefty

int analog_in = A0;
int dig1_out = 9;
int dig2_out = 10;

void setup () {
  Serial.begin (9600);
  pinMode(dig1_out, OUTPUT);
  pinMode(dig2_out, OUTPUT);
  DDRD=0xFF;

  PORTD=0;


  delay (5000);
}

void loop () {
  int value=analogRead(analog_in);    //value= 1024 * voltage / 5


  if  ((value >= 380) && (value <= 656))   // refrence range (2.8v - 3.2v)
  { 

    digitalWrite(dig1_out,LOW); 
    digitalWrite(dig2_out,LOW);
  } 

  else   if ((value >= 710) && (value <= 860)) {   //up range (3.8v - 4 v)


    digitalWrite(dig1_out,LOW); 
    digitalWrite(dig2_out,HIGH);
  } 



  else  if (value <= 260)  {  //down range (2v - 2.2v)
    digitalWrite(dig1_out,HIGH); 
    digitalWrite(dig2_out,LOW);
  }
  else {                                    // for none of the above states

    digitalWrite(dig1_out,LOW); 
    digitalWrite(dig2_out,LOW);
  } 

  delay(20);
}

What sensor are you using?

AD620 instrumentation amplifier

You have "holes" in your range, so if V = 3.3 you set the pins to LOW. This may be what you want, but why then define a reference range?

More importantly, when the reading is close to a threshold it may oscillate above and below it. You need overlapping ranges and some way to keep track of the previous state.

Some serial.print calls in that code to tell you what "value" is would be a good start in tracking down what's going wrong. Killing some of that whitespace wouln't be a bad idea either.

i removed the reference range and defined the up state with 3.8 volt and above
and down state with 2.2 volt and below....and the problem is still

Did you include a transistor and a feedback preventing diode on the relay?

Please post the new code and the results of the debugging prints you (hopefully) added.

And it's possibly an electrical problem. How are you wiring up the relay? How much voltage and current does the relay coil require?

Lefty

it is 5v relay and no transistor nor diode included , the arduino output connected directly to the relay ....

dany151:
it is 5v relay and no transistor nor diode included , the arduino output connected directly to the relay ....

NO! You can NOT power it directly from the arduino ESPECIALLY with no diode.

int analog_in = A0;
int dig1_out = 9;
int dig2_out = 10;

void setup () {
  Serial.begin (9600);
  pinMode(dig1_out, OUTPUT);
  pinMode(dig2_out, OUTPUT);
  DDRD=0xFF;

  PORTD=0;


  delay (5000);
}

void loop () {
  int value=analogRead(analog_in);    //value= 1024 * voltage / 5


  if (value >= 710)  {   //up range 3.8v


    digitalWrite(dig1_out,LOW); 
    digitalWrite(dig2_out,HIGH);
  } 



  else  if (value <= 260)  {  //down range (2v - 2.2v)
    digitalWrite(dig1_out,HIGH); 
    digitalWrite(dig2_out,LOW);
  }
  else {                                    // for none of the above states

    digitalWrite(dig1_out,LOW); 
    digitalWrite(dig2_out,LOW);
  } 

  delay(20);
}

dany151:
it is 5v relay and no transistor nor diode included , the arduino output connected directly to the relay ....

Well the key thing is what is the relay coils current demand? So post a link to your specific relay as some can be driven directly by a output pin but most cannot. Also the diode across the relay coil terminals is an important safety component, you should add it.

Lefty

i powered it by arduino signal through testing code ... and it is worked well

I'm sure it appears to work well, but it is not good on the arduino. It's a bad habit that many newbies overlook and wonder why their arduino stops working correctly, days or even weeks later.

dany151:
i powered it by arduino signal through testing code ... and it is worked well

Do you want help or just validation of what you are presently doing? Post a link to your specific relay so we can look at it's electrical requirements.

Lefty

Have you tried increasing the delay to 300 and printing the analog value to the serial monitor to see what the values are?