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