Automatic Room Lights using Arduino

Circuit Diagram of Automatic Room Lights using Arduino

The following image shows the circuit diagram of the project implemented using Arduino UNO, PIR Sensor and a Relay Module.


If you do not have a relay module, you can make one yourself using very simple hardware. The following circuit diagram shows the project being implemented with the help of discrete components for the Relay Module.



CAUTION: The project involves connection with 230V AC Mains (or 110V, depending on where you live!!!). Be extremely careful when connecting the bulb and Relay to mains supply. If you are unfamiliar with the connections, I strongly recommend having an adult supervision (or an expert supervision).  

Components Required for Automatic Room Lights using Arduino

  • Arduino UNO 
  • PIR Sensor 
  • 5V Relay Module (Relay Board) 
  • LED 
  • 100Ω Resistor (1/4 Watt)  
  • Connecting Wires 
  • Breadboard 
  • Power Supply 
If you do not have a Relay Module, use the following components:
  • 5V Relay  
  • 2N2222 (or BC547) NPN Transistor 
  • 1N4007 PN Junction Diode 
  • 1KΩ Resistor (1/4 Watt) 

Component Description

PIR Sensor

We have already seen about PIR Sensor in the PIR Motion Sensor Tutorial and also implemented in a variety of projects like Home Security System and Automatic Door Opener.

Relay Module

A Relay Module is a very useful component as it allows Arduino, Raspberry Pi or other Microcontrollers to control big electrical loads. We have used a 2-channel Relay Module in this project but used only one relay in it. The relay module used in this project is shown below.

In order to control a single relay on the board, we need to use three pins of the relay module: VCC, GND and IN1.


NOTE: The relay module used in this project is as active LOW one i.e. when the IN1 pin is HIGH, the relay is OFF and when it is LOW, the relay is activated. This point is important while programming the Arduino UNO.

Circuit Design

PIR Sensor’s Data OUT Pin is connected to Arduino’s Digital I/O Pin 8. An LED is connected to pin 13 of Arduino to indicate whether the light is turned ON or OFF.
The IN1 pin of the Relay Module is connected to Pin 9 of Arduino. A bulb is connected to mains supply through relay. One terminal of the bulb is connected to one wire of the mains supply. The other terminal of the bulb is connected to the NO (Normally Open) contact of the Relay Module.
COM (Common) contact of the Relay is connected to the other wire of the mains supply. Be careful when connecting this part of the project.

Code

The code for the Automatic Room Lights using Arduino and PIR Sensor is given below.




int in1 = 9;
int sensor = 8;
int led = 13;
unsigned long t=0;
void setup()
{
Serial.begin(9600);
pinMode(in1, OUTPUT);
pinMode(sensor, INPUT);
pinMode(led, OUTPUT);
digitalWrite(in1,HIGH);
digitalWrite(led,LOW);
while(millis()<13000)
{
digitalWrite(led,HIGH);
delay(50);
digitalWrite(led,LOW);
delay(50);
}
digitalWrite(led,LOW);
}
void loop()
{
digitalWrite(in1,HIGH);
digitalWrite(led,LOW);
if(digitalRead(sensor)==HIGH)
{
t=millis();
while(millis()<(t+5000))
{
digitalWrite(in1,LOW);
digitalWrite(led,HIGH);
if((millis()>(t+2300))&&(digitalRead(sensor)==HIGH))
{
t=millis();
}
}
}
}