Automating home stuff is fun. In this tutorial we are using PIR motion detection sensor to detect motion and turn ON LED bulb and it will remain ON for 2 minutes if no motion detected. Obviously you can increase this time from 0.3 seconds to 5 minutes to 10 minutes. For more information you can see your manufacturer data sheet. If you are continuously in detecting zone it will not turn off, it will increase its delay time. So let’s get started.

Components Required:
  1. Arduino,
  2. Relay module,
  3. PIR sensor (HC-SR501),
  4. Breadboard & Jumper Wire,
  5. 240V/120V light bulb ,
  6. Bulb holder, wire and plug.
Circuit for Motion Detection Automatic Light Bulb:

Motion detection automatic light on-off circuit diagram l

How to set delay time in PIR Sensor (Hardware Solution):

There is two trim port on PIR sensor, One is for Sensitivity and second is for Time trigger (Delay time) port. You can choose sensitivity according to your needs and you can also set delay time by moving trim port knob to the right or left. Left side is for low time that is 0.3 second and Right side is for HIGH, its time around 5 minutes. You can your manufacturer data sheet for accurate information. Set your PIR sensor trim port as shown in diagram.

Control PIR sensor delay time

Code for Motion Detection Automatic Light Bulb:
int Bulb = 8; 
int PIR = 3; 
int val = 0; 

void setup() {
pinMode(Bulb, OUTPUT); 
pinMode(PIR, INPUT); 
}

void loop(){
val = digitalRead(PIR); 
if (val == HIGH) { 
digitalWrite(Bulb, HIGH); 
delay(200);
} else {
digitalWrite(Bulb, LOW); 
delay(200);
}
}
Code Explanation:

Define LED bulb pin number, PIR sensor pin number.

int Bulb = 8;
int PIR = 3;

For storing sensor value we are creating a variable val.

int val = 0;

Set Bulb as an Output and PIR sensor as an Input.

pinMode(Bulb, OUTPUT);
pinMode(PIR, INPUT);

It will read PIR sensor value and store it in Val.

val = digitalRead(PIR);

If PIR sensor detect motion, it will give HIGH signal, if it is HIGH, Bulb will turn ON. It will remaining on until the delay time is set on PIR sensor, in our case it is approximately 2 minutes.

if (val == HIGH) { 
digitalWrite(Bulb, HIGH); 
delay(200);
}

If there is no motion it will turn off the bulb.

else {
digitalWrite(Bulb, LOW); 
delay(200);
}
Working of our project:

when it detects motion, bulb will turn ON, and remain ON until no motion detected within last two minutes. If there is motion within two minutes, for example after 1 minute of detection. Total remaining ON time will be 3 minutes.

Video: