Introduction: Room Light Controlled Using PIR Sensor and Arduino

Today, we will be controlling your room lights through the detection of motion using an Arduino PIR Motion Sensor. This project is very fun to make and it has very practical use in your house and can save you some money by doing this project too. Just a quick DISCLAIMER, this project involves line voltage meaning you're going to be playing with 120V (it will shock you), if this is out of your comfort zone or haven't had much experience, you're probably should come back to this project later on.

This is the tutorial that will guide you from beginning to end with an easy step of course. Let's get started!

Supplies

  • Arduino Microcontroller
  • 1 x Breadboard
  • 1 x PIR Motion Sensor
  • 1 x SRD-05VDC-SL-C Relay
  • 1 x 1KΩ Resistor
  • 1 x 1N4007 Diode
  • 1 x 2N2222 Transistor (NPN)
  • 1 x Extention Cord
  • 1 x Light Bulb/Lamp
  • 2 x Wire Connecters
  • Electrical Tape
  • A couple of connecting wires

Step 1: Supply Purchase

If you don't have access to some of these supplies, I have provided links to where you can purchase each one for cheap prices.

Step 2: Working With the Extension Cord

Our first step is to set up the extension cord so we have it ready to connect to the circuit, from here on out do not plug in the extension cord into the wall until mentioned. First, we are going to grab our extension cord and cut it in half using clippers, then strip off the outer insulation of the cable about 2-3 inches. Make sure when stripping the outer insulation to not damage the inner wires. Next using strippers, strip off about half an inch of the inner wires of both ends. The particular extension cord I am using has 3 wires in it, the green wire is ground, the white wire is neutral and the black wire is line. Now using our wire connecters, reconnect the ground (green) and neutral (white) wires back together, you should now only have the 2 black wires exposed. So we are now done setting up the extension cord and we are going to keep it on the side until needed.

Step 3: Setting Up the Relay

Now we are going to set up the relay and connect it with the Arduino, but before we go ahead with the relay lets understand what a relay is and what it is used for. A relay is basically another form of a switch which is electrically operated, they control one electrical circuit by opening and closing contacts in another circuit. In most cases a relay basically allows a relatively low voltage to easily control higher power circuits, which is what we are doing in this project. Ok, now let's get to the part where we connect the relay!!!

There are 5 pins on the relay, common, normally open (NO), normally closed (NC), and 2 coils, refer to the diagram above for the pins. First, we need to connect one of the coil pins of the relay to the VCC rail on the breadboard, then connect a diode to the other coil and attach it to the VCC rail. The diode is in place to prevent voltage spikes or backward flow of current.

Now grab the NPN transistor and connect the collector of the transistor to the coil where the diode is connected to. Then connect the emitter side of the NPN transistor to the ground rail on the breadboard. Finally, using a 1KΩ connect the base of the transistor to the digital pin 2 of the Arduino.

Finally, we are going to do a really important connection. Take the extension cord we prepared and connect one black wire to the common pin in the relay and secure the connection with electrical tape. Then connect the other end to the NO pin of the relay.

Step 4: Connecting the PIR Motion Sensor

We also almost done, now we are going to connect the PIR motion sensor into the circuit but before we do lets understand what a PIR sensor is. The PIR sensor stands for Passive Infrared sensor, this sensor can detect the presence of human beings or animals, and send a signal saying it detected motion. The PIR sensor has 3 pins, VCC, Output and Ground.

First, we need to connect the VCC pin of the PIR sensor to VCC rail on the breadboard and connect the ground pin of the PIR sensor to the ground rail. Then we are going to connect the output pin to one of the Arduino pins, I used pin 4. You have successfully connected the PIR sensor now!!

Step 5: Writing the Code

Now we are done with all the circuitry and all we need to do now is to write the code. The code is relatively simple for this project and the logic is straight forward. I have attached to code for this circuit above, but let us understand what this code actually does.

We first initialize our relay pin and our PIR sensor pin, and we create an int variable called val. Then we declare the relay pin as an output (signal only go from Arduino) and we declare the PIR sensor pin as input (signal only goes into Arduino). Finally, we use digitalread to get a reading from the PIR sensor which is either 0 (no motion) or 1 (motion) and store it into the variable val. Then we use an if and else statement to use this value which we stored to turn on/off the lightbulb, and now we are done the code!!

Step 6: Enjoy

Hopefully, you enjoy it, and be proud of what you achieved by your self today!!