Stuck on activating a relay

I am working on a simple code that uses 2 motion sensors. The first sensor lights a red led when it detects motion. The second sensor lights a blue led and it's supposed to trigger a relay that activates a spray switch on then off. The led lights are doing what they are supposed to do, but the relay is not. Everything is wired correctly and all components work. /*
Arduino with PIR motion sensor
For complete project details, visit: Arduino with PIR Motion Sensor | Random Nerd Tutorials
Modified by Rui Santos based on PIR sensor by Limor Fried
*/

int led = 13; // the pin that the LED is atteched to
int sensor = 2; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)

int ledblue = 10; // the pin that the LED is atteched to
int sensorblue = 5; // the pin that the sensor is atteched to
int stateblue = LOW; // by default, no motion detected
int valblue = 0; // variable to store the sensor status (value)

int spray = 8; // the pin that the spray bottle is atteched to
int valspray = 0; // by default, no motion detected

void setup() {
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
pinMode(spray, OUTPUT); // initialize spray as an output
Serial.begin(9600); // initialize serial

pinMode(ledblue, OUTPUT); // initalize LED as an output
pinMode(sensorblue, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
}

void loop(){
val = digitalRead(sensor); // read sensor value

if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON

delay(100);                // delay 100 milliseconds 

if (state == LOW) {
  Serial.println("Motion detected!"); 
  state = HIGH;       // update variable state to HIGH
}

}
else {
digitalWrite(led, LOW); // turn LED OFF

  delay(200);             // delay 200 milliseconds 
  
  if (state == HIGH){
    Serial.println("Motion stopped!");
    state = LOW;       // update variable state to LOW
}

}

valblue = digitalRead(sensorblue); // read sensor value
valspray = digitalRead(sensorblue);

if (valblue == HIGH) { // check if the sensor is HIGH

digitalWrite(ledblue, HIGH);   // turn LED ON
digitalWrite(spray, HIGH);   // turn spray ON

digitalWrite(spray, LOW);   // turn spray ON
delay(100);

digitalWrite(spray, HIGH);   // turn spray ON

digitalWrite(spray, LOW);   // turn spray ON

delay(100);                // delay 100 milliseconds 
 
if (stateblue == LOW) {
  Serial.println("Motion detected!"); 
  stateblue = HIGH;       // update variable state to HIGH
}

}
else {

  digitalWrite(ledblue, LOW); // turn LED OFF
delay(200);                // delay 200 milliseconds
  

  if (state == HIGH){
    Serial.println("Motion stopped!");
    state = LOW;       // update variable state to LOW
}

}

{
}

}``

Show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.


LEDs require a series dropping resistor, try 220R,

It looks like the two wires connected to the spray switch are attached to different relays.

Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

no they are both on relay 1

However much I zoom in, I can't convince myself that's correct. Perhaps another picture would help.

Hi @paulhook
This is very very very fast to use with relays.

RV - mineirin

  digitalWrite(spray, HIGH);   // turn spray ON
  digitalWrite(spray, LOW);   // turn spray ON   <<<< ----- OFF?
  delay(100);
  digitalWrite(spray, HIGH);   // turn spray ON
  digitalWrite(spray, LOW);   // turn spray ON   <<<< ----- OFF?

yes, the comment is wrong. It should be turn spray off

Powering your relay board from the Arduino is not recommended.

Suggest you get a separate 5V power supply.

1 Like

Hi
It's not just the comment that's wrong.
Turning an output on and off immediately does not work with mechanical devices such as a relay.

  digitalWrite(spray, HIGH);   // turn spray ON
  digitalWrite(spray, LOW);   // turn spray ON   <<<< ----- OFF?
  delay(100);
  digitalWrite(spray, HIGH);   // turn spray ON
  digitalWrite(spray, LOW);   // turn spray ON   <<<< ----- OFF?

RV - mineirin

As stated by @ruilviana


How long do you think it stays HIGH ?

  delay(100);
  digitalWrite(spray, HIGH);
  digitalWrite(spray, LOW);

1 millisecond?

Hi
relay Operate time(Under nominal voltage) .......... ≤10ms

Ref: http://www.songlerelay.com/Public/Uploads/20161104/581c81ac16e36.pdf

RV mineirin

Hi
I don't agree with this, It's much less than 1 mSec.

This time is <= 8 micros seconds
RV - mineirin

I tried delay(10); and it works 1 time, so it activates the switch once, but not again

Show the new code, but use </> to post it.

ArduinoForum

I added some code for a servo motor. So the led's work properly, the servo motor works properly but the relay just goes click click when activated



/*  
    Arduino with PIR motion sensor
    For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
    Modified by Rui Santos based on PIR sensor by Limor Fried
*/
 
int led = 13;                // the pin that the LED is atteched to
int sensor = 2;              // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected
int val = 0;                 // variable to store the sensor status (value)

int ledblue = 10;                // the pin that the LED is atteched to
int sensorblue = 5;              // the pin that the sensor is atteched to
int stateblue = LOW;             // by default, no motion detected
int valblue = 0;                 // variable to store the sensor status (value)

int spray = 8;                // the pin that the spray bottle is atteched to
int valspray = 0;             // by default, no motion detected

// Include the Servo library 
#include <Servo.h> 
// Declare the Servo pin 
int servoPin = 6; 
// Create a servo object 
Servo Servo1; 

void setup() {
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  pinMode(spray, OUTPUT);    // initialize spray as an output
  Serial.begin(9600);        // initialize serial

  pinMode(ledblue, OUTPUT);      // initalize LED as an output
  pinMode(sensorblue, INPUT);    // initialize sensor as an input
  Serial.begin(9600);        // initialize serial

     // We need to attach the servo to the used pin number 
   Servo1.attach(servoPin); 
   
}

void loop(){
  val = digitalRead(sensor);   // read sensor value
  
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
   // Make servo go to 0 degrees 
   Servo1.write(0); 
   delay(1000); 
        digitalWrite(spray, HIGH);   // turn spray ON
    
    digitalWrite(spray, LOW);   // turn spray ON
    
    delay(100);                // delay 100 milliseconds 
    
    if (state == LOW) {
      Serial.println("Motion detected!"); 
      state = HIGH;       // update variable state to HIGH


   // Make servo go to 90 degrees 
   Servo1.write(90); 
   delay(1000); 
   
   
    }
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
      
      delay(200);             // delay 200 milliseconds 

          digitalWrite(spray, HIGH);   // turn spray ON
    
    digitalWrite(spray, LOW);   // turn spray ON
      
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }

    
  }

  valblue = digitalRead(sensorblue);   // read sensor value
  valspray = digitalRead(sensorblue);

  if (valblue == HIGH) {           // check if the sensor is HIGH
    
   // Make servo go to 180 degrees 
   Servo1.write(180); 
   delay(1000); 
   
    digitalWrite(ledblue, HIGH);   // turn LED ON
    digitalWrite(spray, HIGH);   // turn spray ON
    delay(10);
    digitalWrite(spray, LOW);   // turn spray ON
    delay(10);
    delay(100);
    
    digitalWrite(spray, HIGH);   // turn spray ON
    delay(10);
    digitalWrite(spray, LOW);   // turn spray ON
   delay(10);
    delay(100);                // delay 100 milliseconds 
     
    if (stateblue == LOW) {
      Serial.println("Motion detected!"); 
      stateblue = HIGH;       // update variable state to HIGH

         // Make servo go to 90 degrees 
   Servo1.write(90); 
   delay(1000); 
   
    }
  } 
  else {
    
      
       
      digitalWrite(ledblue, LOW); // turn LED OFF
    delay(200);                // delay 200 milliseconds
      
    
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }

    
  }
    
    {

    }
  
}

Hi
It doesn't affect the functionality but you initialized the serial 2 times.

And has two unnecessary brackets " { and } " at end code.

RV mineirin

Hi
Increase the time from 10 to 100 for diagnostic purposes and see if it works 2 times.

RV - mineirin