Temporized PIR Sensor+Relay module

Hi guys.... I have a problem programing Arduino

I have a PIR sensor and a relay. I want When the PIR sensor send the signal the relay turns on for 3 minutes. and the time reset every time the PIR sensor send the signal again, like the sensor of light commonly works. when the PIR sensor doesn't send signal for 3 minutes......the relay is off until the sensor send signal again. Here's the code, What's wrong?

int Relay = 8;  
int Pir = 4;                 

void setup()
{
  pinMode(Relay, OUTPUT); 
  pinMode(Pir, INPUT);    
}

void loop(){
if (Pir==HIGH){  
  digitalWrite(Relay, HIGH);
  delay(10000);}
else {
  digitalWrite(Relay,LOW);
}}

With this code the relay doesn't off

Get rid of the diode, it is not need here. It should be across the relay coil and the board probably has one already.

Is the relay off before the PIR is triggered?

Just to test the system, replace the relay with a LED and series 270 ohm or similar. Does the LED turn on and off?

Weedpharma

if (Pir==HIGH)

if (digitalRead(Pir) == HIGH)

I assume you have a relay board with transistor, base resistor, diode, etc.
Then that diode is not needed.

Relay boards usually have "reverse logic".
HIGH is off, and LOW is on.

The PIR module also has a delay, so make sure the "time" adjust pot on the PIR module is fully anti-clockwise (short delay). Leave the sensitivity pot in the middle.

You could use a countdown timer, like this.

int Relay = 8;
int Pir = 4;
int timeDelay;

void setup() {
  Serial.begin(9600); // debugging
  pinMode(Relay, OUTPUT);
  pinMode(Pir, INPUT);
}

void loop() {
  if (digitalRead(Pir) == HIGH) { // PIR starts the counter
    timeDelay = 180; // in seconds
  }
  if (timeDelay > 0) { // if countdown is still running
    digitalWrite(Relay, LOW);
    Serial.print(timeDelay);
    Serial.println("  Relay ON");
    timeDelay --; // -1
  }
  else {
    digitalWrite(Relay, HIGH);
    Serial.println("Relay OFF");
  }
  delay(1000); // one sec loop delay
}

Leo..

Wawa:
I assume you have a relay board with transistor, base resistor, diode, etc.
Then that diode is not needed.

Relay boards usually have "reverse logic".
HIGH is off, and LOW is on.

The PIR module also has a delay, so make sure the "time" adjust pot on the PIR module is fully anti-clockwise (short delay). Leave the sensitivity pot in the middle.

You could use a countdown timer, like this.

int Relay = 8;

int Pir = 4;
int timeDelay;

void setup() {
  Serial.begin(9600); // debugging
  pinMode(Relay, OUTPUT);
  pinMode(Pir, INPUT);
}

void loop() {
  if (digitalRead(Pir) == HIGH) { // PIR starts the counter
    timeDelay = 180; // in seconds
  }
  if (timeDelay > 0) { // if countdown is still running
    digitalWrite(Relay, LOW);
    Serial.print(timeDelay);
    Serial.println("  Relay ON");
    timeDelay --; // -1
  }
  else {
    digitalWrite(Relay, HIGH);
    Serial.println("Relay OFF");
  }
  delay(1000); // one sec loop delay
}


Leo..

Pal.....you're the best!

it works!

very simple code, thanks!