Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Pir Motion Sensor (Im-21066)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Report: Connecting a PIR Motion Sensor with Arduino to

Detect Motion

Introduction

This report details the process of connecting a Passive Infrared (PIR) Motion Sensor with an
Arduino UNO to detect motion in a room or area surrounding the sensor. PIR sensors are
commonly used in security systems and automatic lighting because they can detect infrared
radiation from moving objects, particularly humans. This project will guide you through the setup
and programming necessary to create a basic motion detection system.

Components and Cost

To create this prototype, the following components are required:

1. Arduino UNO: A popular microcontroller board used for building digital devices and
interactive objects. (Cost: PKR 800)
2. PIR Motion Sensor: A sensor that detects infrared radiation from moving objects. (Cost:
PKR 200)
3. Jumper Wires: Used to make electrical connections between the Arduino and other
components. (Cost: PKR 30 for 5 pcs)
4. LED Light: An indicator that lights up when motion is detected. (Cost: PKR 10) Total

Cost: PKR 1040

Component Details

1. Arduino UNO

The Arduino UNO is a microcontroller board based on the


ATmega328P. It has 14 digital input/output pins, 6 analog
inputs, a 16 MHz quartz crystal, a USB connection, a
power jack, and a reset button. It is an essential tool for
beginners and professionals to create electronic projects.
2. PIR Motion Sensor

The PIR Motion Sensor detects motion by measuring


changes in infrared radiation levels. When an object with
a higher temperature (like a human) passes by, the sensor
detects the change and outputs a signal

3. Jumper Wires

Jumper wires are used to connect different components in a


circuit. They come in various lengths and are essential for
making quick and reliable connections

4. LED Light

An LED (Light Emitting Diode) is a small electronic component


that emits light when an electric current passes through it. In
this project, it is used to indicate when motion is detected.

Circuit Diagram
Project Snap

Steps to Connect the PIR Sensor with Arduino

1. Connect the PIR Sensor to the Arduino UNO:


o VCC Pin (PIR Sensor): Connect to the 5V pin on the Arduino. o GND Pin
(PIR Sensor): Connect to the GND pin on the Arduino. o OUT Pin (PIR
Sensor): Connect to a digital input pin on the Arduino (e.g., Pin 3
2. Connect the LED:
o Connect the anode (longer leg) of the LED to a digital output pin on the Arduino
(e.g., Pin 13). o Connect the cathode (shorter leg) of the LED to the GND pin on
the Arduino.
3. Wiring Diagram:
sql
Copy code
PIR Sensor Arduino UNO
----------- -----------
VCC ----------- 5V
GND ----------- GND
OUT ----------- Pin 3

LED Light
--------
Anode --------- Pin 13 Cathode
-------- GND

4. Circuit Diagram Explanation:


o The PIR sensor is connected to the Arduino UNO with three pins: VCC (power),
GND (ground), and OUT (signal). The VCC pin of the PIR sensor connects to the
5V pin on the Arduino to provide power. The GND pin of the PIR sensor connects
to one of the GND pins on the Arduino to complete the circuit. The OUT pin of the
PIR sensor is connected to a digital input pin on the Arduino (e.g., Pin 3) to read
the sensor’s signal.
o The LED is connected to the Arduino UNO with two legs: the longer leg (anode) and
the shorter leg (cathode). The anode connects to a digital output pin on the Arduino (e.g.,
Pin 13), and the cathode connects to a GND pin on the Arduino.

Arduino Code

The following Arduino code is used to detect motion and control the LED light accordingly. This
code also allows the sensor to recalibrate every 5 seconds to detect new motion.

//the time we give the sensor to calibrate (10-60 secs according to the
datasheet) int calibrationTime = 5;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;

//the amount of milliseconds the sensor has to be low


//before we assume all motion has stopped
long unsigned int pause = 5000;

boolean lockLow = true;


boolean takeLowTime;

int pirPin = 3; //the digital pin connected to the PIR sensor's output
int ledPin = 13;

/////////////////////////////
//SETUP
void setup(){ Serial.b
egin(9600); pinMode(pirPin,
INPUT); pinMode(ledPin,
OUTPUT);
digitalWrite(pirPin, LOW);

//give the sensor some time to calibrate


Serial.print("calibrating sensor "); for(int
i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000); }
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50); }
////////////////////////////
//LOOP void
loop(){
if(digitalRead(pirPin) == HIGH){ digitalWrite(ledPin,
HIGH); //the led visualizes the sensors output pin sta

if(lockLow){ //makes sure we wait for a transition to LOW before any


further output is made:

lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50); }
takeLowTime = true;
}

if(digitalRead(pirPin) == LOW){ digitalWrite(ledPin, LOW); //the led


visualizes the sensors output pin state

if(takeLowTime){ lowIn = millis(); //save the time of the transition


from high to LOW

takeLowTime = false; //make sure this is only done at the start of a


LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen if(!lockLow
&& millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec"); delay(50);
}
}
}
Explanation of the Code

• The code begins by defining the pins connected to the PIR sensor and the onboard LED.
• A calibration time of 5 seconds is set for the sensor to stabilize before it starts detecting
motion.
• In the setup function, the serial communication is initialized for debugging purposes, and
the PIR sensor pin is set as an input while the LED pin is set as an output. The PIR sensor
is also given time to calibrate.
• The loop function continuously reads the state of the PIR sensor. If motion is detected (the
sensor output is HIGH), a message is printed to the serial monitor, and the LED is turned
on. If no motion is detected (the sensor output is LOW), the LED is turned off. The code
also ensures that it waits for 5 seconds of no motion before assuming that motion has ended.

Conclusion

This project demonstrates how to interface a PIR Motion Sensor with an Arduino UNO to detect
motion. The Arduino reads the sensor's output and responds by turning an LED on or off, making
it a useful setup for various applications such as security systems and automated lighting.

By following the steps outlined above, you can create a basic motion detection system and modify
the code or hardware setup to suit more complex requirements. This project serves as a
foundational example of using sensors with microcontrollers to create interactive and responsive
systems.

You might also like