Question: const int SENSOR_PIN = 2;// the Arduino pin connected to output (middle) wire of sensor const int RELAY_PIN = 4;// the Arduino pin which is
const int SENSOR_PIN = 2;// the Arduino pin connected to output (middle) wire of sensor const int RELAY_PIN = 4;// the Arduino pin which is connected to control relay
void setup() { Serial.begin(9600);// setup Serial Monitor to display information Serial.println("Robojax Arduino Tutorial"); Serial.println("HC-SR501 sensor with relay"); pinMode(SENSOR_PIN, INPUT);// Define SENSOR_PIN as Input from sensor pinMode(RELAY_PIN, OUTPUT);// Define RELAY_PIN as OUTPUT for relay }
void loop() { // Robojax.com HC-SR501 motion sensor tutorial Aug 05, 2018 int motion =digitalRead(SENSOR_PIN);// read the sensor pin and stores it in "motion" variable // if motion is detected if(motion){ Serial.println("Motion detected"); digitalWrite(RELAY_PIN, LOW);// Turn the relay ON Serial.println("Relay on"); delay(10000); } else{ Serial.println("===nothing moves"); digitalWrite(RELAY_PIN,HIGH);// Turn the relay OFF Serial.println("Relay off"); } delay(500); }
How come the delay(10000) get the PIR sensor stuck in a loop? Without the delay(10000) the PIR sensor will turn on the the relay for around 3 seconds and then will not activate it again until there is motion, but when the delay(10000) is placed in then the motion sensors activates the relay for 10 seconds and then cuts of for 1 second and then activivates the relay again for 10 seconds without being prompted by motion. It will remain stuck in this pattern of behavior until I disconect power.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
