Motion activated AC switch using Arduino and PIR sensor

There is no doubt that home automation have become the norm. Apart from using electronic gadgets, like smart mobile phones, people now want to live in smart homes where systems operate automatically by themselves based on certain conditions of the environment. One of such smart systems is a motion activated AC switch using Arduino and PIR sensor. The motion activated AC switch proposed here will trigger an AC load via a relay where it notices the presents of humans or similar animals that emits passive infrared.

Required components

Arduino UNO x1Buy here
HC-SR312 Mini PIR sensor x1Buy here
1 Channel Relay Module x1Buy here
AC – DC (220v-5v) module x1Buy here
Solder-less Prototype board and wiresBuy here

Schematic of the motion activated AC switch

Motion Activated AC Switch

The circuit uses a HC-SR312 Passive Infrared Sensor. The sensor operate from 2.7V – 5V and use very little energy when operating in the non-tripped state. The PIR sensor is tripped by an infrared source, typically human body heat (or another animal with similar radiative emission). When the PIR sensor is tripped it sends a HIGH signal to its OUT pin, which will be read by digital pin 2 of the Arduino Uno board. The Vcc is connected to 3.3v pin of the Arduino and GND to any of the GND on the Arduino.

A 1-channel 5v relay is used in the circuit to with the AC load. The relay is capable of driving 250v/10A AC load. The S (sense) pin of the relay is connected to the Arduino digital pin 8, The Vcc to the 5v pin on the Arduino and GND to any of the GND on the Arduino. The S (sense) pin of the relay module is an Active High pin. Which means the relay will be energized when you pull the pin HIGH.

220v-5v isolated power supply module is used to power the Arduino board, relay module and the PIR sensor. Positive (+) and negative (-) of the power supply module connects to 5v/ GND or the Arduino board.

Components pin layouts

Pin-layout

The sketch (Code)

/*
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
MOTION ACTIVATED AC SWITCH USING ARDUINO AND PIR SENSOR
Version: 1.0
By: Izuchukwu Obiako 
For: Do It Yourself Nigeria (https://diymall.com.ng)
Dated: 16/05/2020
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*/

const byte relay = 8;
const byte pirSensor = 2;
volatile byte state = LOW;
void setup() {
  Serial.begin(9600);
  pinMode(relay,OUTPUT);
}
void loop() {
  state = digitalRead(pirSensor);
 if (state==HIGH){
    Serial.println("RELAY ON");
    digitalWrite(relay,HIGH);
    delay(20000);
    Serial.println("RELAY OFF");
  }
  if (state == LOW) {
    digitalWrite(relay,LOW);
  }
}

First we declared 3 constants variables to hold: the relay pin, PIR sensor pin and the initial state of the PIR sensor.

In the setup method, we set the relay (pin 8) as an OUTPUT pin.

First line of the loop() initializes the serial Monitor. Next we gets the PIR sensor state (pin 2) and saves the state in the variable “state”. Next we used an IF structure to check the returned value in the “state” variable. If the value is “HIGH”, that means the PIR sensor have sent a HIGH through its OUT pin to the Arduino pin 2, which also means that the sensor have detected  motion around it. So we set the S (sense) pin of the relay HIGH. Then we wait for 20 seconds using the delay() method. After that we print “RELAY OFF” in the serial Monitor. The IF structure ends.

Using another IF structure we check if the state have turned to LOW and if that’s true, we de-energize the relay by turning the relay (Arduino pin 8) to LOW. The IF ends.

The loop continues endlessly this way.

Application of the motion activated AC switch

Of course the motion activated AC switch can be used to switch ON/OFF any AC load within the capability of the relay. But in my case I installed it in my bathroom so that when I enter the bathroom it switches the light ON and as long as am there, it remains on and when I leaves the bathroom, the light goes off after 20 seconds. So the switch can be used in any similar environment where power is only needed when somebody is around.

If you like this tutorial, you can share it with your friends using the social media buttons and if you have built the circuit or have any question, tell us using the comment box below. Thanks for reading.