#define relay 12 // Relay IN pin connected to pin 12
#define pir 3 // PIR sensor OUT connected to pin 3
void setup() {
pinMode(relay, OUTPUT); // Set relay pin as output
pinMode(pir, INPUT); // Set PIR sensor pin as input
digitalWrite(relay, LOW); // Initialize relay in OFF (Normally Open) state
Serial.begin(9600);
Serial.println(\"Relay Module Initialized. Waiting for motion...\");
}
void loop() {
int pir_value = digitalRead(pir); // Read PIR sensor value
if (pir_value == HIGH) { // Motion detected
digitalWrite(relay, HIGH); // Turn ON relay
Serial.println(\"Motion detected - Relay ON\");
}
else { // No motion detected
digitalWrite(relay, LOW); // Turn OFF relay
Serial.println(\"No motion - Relay OFF\");
}
delay(200); // Small delay for stability
}
Instruction Details
Wiring: Connect relay IN to Digital Pin 12, VCC to 5V, GND to GND. Connect PIR OUT to Digital Pin 3, VCC to 5V, GND to GND. Connect load to relay COM and NO/NC terminals.
Library: Not required (built-in).
Upload Code: Tools → Board → Arduino Uno. Tools → Port → select your COM port. Click Upload button.
View Output: Relay turns ON when PIR detects motion. Open Serial Monitor for status. Use COM/NO/NC for loads. Caution with AC power.
How to Use
Connect the required components as per the Pin Configuration above
Open Arduino IDE and create a new sketch
Copy and paste the code above
Select your Arduino board and COM port from Tools menu
Click the Upload button to upload the code to your Arduino
Open Serial Monitor (if applicable) to see the output