can someone help a newb with a relay question.

I have a small 5v relay that I want to switch on and off based on a smoke sensor (MQII I believe). I have bricked two small relays from one vendor just to try to see if the relay itself works.. ARRGGHH.. Being new, I'm sure its something I'm doing wrong... 8)

SO if someone can help it would be awesome thanks.. much luv...

my sensor is hooked up fine and reads fine with the serial monitor as I suspect it to. however I cannot get the relay to work. The relay itself is on a board with an LED and resistors etc.. So in theory, the led would come on when the relay closes the circuit. The VVC is to the 5v on the arduino uno rev 3 the ground to the ground etc. and the signal to digital 9 pin.. should this be analog? dunno..

the serial monitor shows the sensor reading fine, but the relay does not do anything. HELP please..

thanks. anyhow

here is my code- // Sensor value read from analog pin (range from 0 to 1023) int iSensorValue = 0; int relayPin = 9; // the number of the relay pin

// Mapped value from 0 to 100 byte bySensorVal = 0;

// Message string to be displayed on serial monitor char cMsg[124];

////////////////////////////////////////////////////// // // SETUP // void setup() { Serial.begin(9600); }

////////////////////////////////////////////////////// // // LOOP // void loop() {

// Read input value on A0 and map it from 0 to 100 iSensorValue = analogRead(A0); bySensorVal = map(iSensorValue, 0, 1023, 0, 100); digitalWrite(9,HIGH); delay(1000); digitalWrite(9,LOW); delay(1000); // Display input value and mapped value sprintf(cMsg, "MQ-2 Sensor Value : %d (%d)", iSensorValue, bySensorVal);

// Check for high value if (bySensorVal > 7) { Serial.print(cMsg); Serial.println(F(" *** DISTURBANCE IN THE FORCE! ***")); } else

{ Serial.println(cMsg); } if (bySensorVal > 7) { // turn relay on: digitalWrite(relayPin, HIGH); } else { // turn relay off: digitalWrite(relayPin, LOW); } // Loop 10 times per second delay(1000); }

void setup()
{
Serial.begin(9600);
pinMode(relayPin,OUTPUT); add this, to allow the pin to be toggled HIGH or LOW
}

Change
digitalWrite(9,HIGH);
To
digitalWrite(relayPin,HIGH);

Make sure you drive the relay with a transistor (place a snubbing diode across the relay).

OK so read the how to use this forum sticky and learn how to post code please.

How on earth can you brick a relay? They are one of the toughest thing there is.

We need to know what you have done, your descriptions are not enough.

The relay itself is on a board with an LED and resistors etc

Have you a link to the data sheet or did you make it? Normally you can't connect a relay directly to an arduino pin because it will take more current than the arduino should be permitted to supply. You normally drive them with a transistor. How are you wiring it up, and exactly what relay do you have? Part number and link to the data sheet.

Larry THANKS so much.. seems to have maybe done the trick.. BUT one small issue. Relay is clicking on and off now (good thing) but it is not reacting to the senor to do so. meaning that the relay is sequentially going on and off by itself. 3 seconds on, 1 second off.. ARRGGHH.. thanks for helping..

the code I changed is now-

// Sensor value read from analog pin (range from 0 to 1023)
int iSensorValue = 0;
int relayPin =  9;      // the number of the relay pin

// Mapped value from 0 to 100
byte bySensorVal = 0;

// Message string to be displayed on serial monitor
char cMsg[124];

//////////////////////////////////////////////////////
//
// SETUP
//
void setup() {
  Serial.begin(9600); 
  pinMode(relayPin,OUTPUT);   // add this, to allow the pin to be toggled HIGH or LOW 
}

//////////////////////////////////////////////////////
//
// LOOP
//
void loop() {
  
  // Read input value on A0 and map it from 0 to 100
  iSensorValue = analogRead(A0);
  bySensorVal = map(iSensorValue, 0, 1023, 0, 100);
  digitalWrite(relayPin,HIGH);
delay(1000);
digitalWrite(9,LOW);
delay(1000);
  // Display input value and mapped value
  sprintf(cMsg, "MQ-2 Sensor Value : %d (%d)", iSensorValue, bySensorVal);

  // Check for high value
  if (bySensorVal > 7) {
    Serial.print(cMsg);
    Serial.println(F(" *** DISTURBANCE IN THE FORCE! ***"));
  }
  else 




{
    Serial.println(cMsg);
  }
 if (bySensorVal > 7) {     
    // turn relay on:    
    digitalWrite(relayPin, HIGH);  
  } 
  else {
    // turn relay off:
    digitalWrite(relayPin, LOW); 
  }
  // Loop 10 times per second
  delay(1000);
}
digitalWrite(relayPin,HIGH);
delay(1000);
digitalWrite(9,LOW);
delay(1000);

This code is executed each time through loop() whatever the sensor value is.

THANKS TO UKHELI AND LARRY

although trivial to you, was a monster challenge for me..! but I did solve.. thanks SO much for your help and kindness..

best,

CC..

You are telling it to toggle:

void loop() {
some code 
 
  digitalWrite(relayPin,HIGH);                     <<<<relay is NO
  delay(1000);                                             
  digitalWrite(9,LOW);                                <<<<1 Second later relay is OFF
  delay(1000);                                            <<<<1 Second more goes by 

some more code
 
 if (bySensorVal > 7) {     
    // turn relay on:    
    digitalWrite(relayPin, HIGH);                  <<<< relay probably ON           if >7
  } 
  else {
    // turn relay off:
    digitalWrite(relayPin, LOW); 
  }
  delay(1000);                                            <<<<1 Second more goes by
}