0% found this document useful (0 votes)
10 views

Detectting Motion With PIR Sensor

Uploaded by

tarun.jampani45
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Detectting Motion With PIR Sensor

Uploaded by

tarun.jampani45
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

CO 214- (Fundamentals of Internet of

Things) 1
PIR Sensor Interfacing with Arduino UNO
Introduction

 The term PIR is the short form of the Passive InfraRed.

 The term “passive "indicates that the sensor does not actively take part in the
process, which means, it does not emit the referred IR signals itself, rather passively
detects the infrared radiations coming from the human body in the surrounding area.

2
PIR Sensor Interfacing with Arduino UNO
Introduction

• Interfacing an HC-SR501 Pyroelectric PIR infrared motion sensor with


an Arduino Uno is a powerful way to add motion detection capabilities
to your projects.

• The HC-SR501 PIR motion sensor is a small, low-cost device that utilizes
infrared technology to detect changes in temperature caused by the
movement of people or objects in front of the sensor.

• The sensor can detect motion within a certain range and angle, and
sends a signal to the Arduino microcontroller indicating whether
motion has been detected or not.

3
PIR Sensor Interfacing with Arduino UNO
Introduction

• The Arduino then reads this signal and performs the necessary logic to
determine if motion has been detected, and this information can be
used to trigger an action such as turning on a light or sending a
notification.

• The status of the motion detection can also be posted on the serial
monitor for debugging or for displaying the status.

• This process allows for the creation of interactive projects and systems
that can react to movement in the environment, such as home
automation, security systems, and robotics.

4
PIR Sensor Interfacing with Arduino UNO
Hardware Components

S.no Component Value Qty


1. Arduino UNO – 1
USB Cable Type A to
2. – 1
B

3. Motion Sensor HC-SR501 1

9V Power Adapter
4. – 1
for Arduino

5. Jumper Wires – 1

5
PIR Sensor Interfacing with Arduino UNO
HC-SR501 PIR Motion Sensor Pinout
The PIR motion sensor consists of 3 pins-VCC, GND, and the Data Output
Pin

6
PIR Sensor Interfacing with Arduino UNO
How does a PIR sensor work?

7
PIR Sensor Interfacing with Arduino UNO
How does a PIR sensor work?
• Every object with a temperature above absolute zero emits IR
radiation, which is invisible to the human eye but carries information
about an object's temperature and movement.

PIR Sensor Working.mp4

pir-sensor-with-arduino.mp4

8
PIR Sensor Interfacing with Arduino UNO
Motion Sensor with Arduino

1. Connect the HC-SR501 PIR motion sensor to the Arduino using the
VCC, GND, and OUT pins specified in the sensor’s documentation.
2. In the Arduino IDE, include the necessary libraries for the HC-SR501
sensor

3. In the setup() function, initialize the serial communication and the pin
mode for the sensor:

4. In the loop() function, read the sensor’s output value:

9
PIR Sensor Interfacing with Arduino UNO
Motion Sensor with Arduino

5. Use an if statement to check if motion is detected by checking if the


sensorValue is high and post the status on the serial monitor using the
Serial.println() function:

6. You can add any other action you want to take when motion is
detected.
7. Upload the code to the Arduino Uno and open the serial monitor to
see the sensor’s output values.

1
PIR Sensor Interfacing with Arduino UNO
Schematic Make connections according to the circuit diagram given below.

1
PIR Sensor Interfacing with Arduino UNO
Wiring / Connections

Arduino Sensor
5V VCC
GND GND
D2 OUT

Installing Arduino IDE

12
Arduino Sketch

const int PIN_TO_SENSOR = 2; // the pin that OUTPUT pin of sensor is connected to

int pinStateCurrent = LOW; // current state of pin

int pinStatePrevious = LOW; // previous state of pin

void setup() {
Serial.begin(9600); // initialize serial

pinMode(PIN_TO_SENSOR, INPUT); // set arduino pin to input mode to read


value from OUTPUT pin of sensor

13
Arduino Sketch
void loop() {
pinStatePrevious = pinStateCurrent; // store old state
pinStateCurrent = digitalRead(PIN_TO_SENSOR); // read new state

if (pinStatePrevious == LOW && pinStateCurrent == HIGH) { // pin


state change: LOW -> HIGH
Serial.println("Motion detected!");
// TODO: turn on alarm, light or activate a device ... here
}
else
if (pinStatePrevious == HIGH && pinStateCurrent == LOW) { //
pin state change: HIGH -> LOW
Serial.println("Motion stopped!");
// TODO: turn off alarm, light or deactivate a device
... here
}
}

14
Arduino Sketch
Working Explanation
• In the Arduino IDE, the necessary libraries for the HC-SR501 sensor
are included.

• In the setup() function, the serial communication and the pin mode for
the sensor is initialized. The baud rate is set to 9600 bps.

15
Arduino Sketch
Working Explanation

• In the loop() function, the sensor’s output value is read using the
digitalRead() function, which returns either a HIGH or LOW value.

• The value is then compared to check if motion is detected by using an


if statement.

• If the sensorValue is HIGH, it means motion is detected and the


message “Motion detected” is sent to the serial monitor.

• If the sensorValue is LOW, it means no motion is detected and the


message “No motion detected” is sent to the serial monitor.

16
PIR Sensor Interfacing with Arduino UNO
Applications

• Security Systems
• Robotics
• Industrial Automation
• Automotive
• Medical equipment
• Proximity Detection
• Gaming and interactive projects

17
Distance measurement using Ultrasonic sensor and Arduino
Ultrasonic Sensor
• An ultrasonic Sensor is a device used to measure the distance between the
sensor and an object without physical contact. This device works based on
time-to-distance conversion.
Working Principle of Ultrasonic Sensor
• Ultrasonic sensors measure distance by sending and receiving the ultrasonic
wave.
• The ultrasonic sensor has a sender to emit the ultrasonic waves and a
receiver to receive the ultrasonic waves.
• The transmitted ultrasonic wave travels through the air and is reflected by
hitting the Object.
• Arduino calculates the time taken by the ultrasonic pulse wave to reach the
receiver from the sender.
• We know that the speed of sound in air is nearly 344 m/s,
• So, the known parameters are time and speed (constant). Using these
parameters, we can calculate the distance traveled by the sound wave.
Formula: Distance = Speed * Time
18
How the HC-SR04 Ultrasonic Distance Sensor Works?

1
Distance measurement using Ultrasonic sensor and Arduino

• In the code, the “duration” variable stores the time taken by the sound
wave traveling from the emitter to the receiver.
• That is double the time to reach the object, whereas the sensor returns
the total time including sender to object and object to receiver.
• Then, the time taken to reach the object is half of the time taken to
reach the receiver.

• so we can write the expression as,

• Distance = Speed of Sound in Air * (Time Taken / 2)

• Note: Speed of sound in air = 344 m/s.

20
Distance measurement using Ultrasonic sensor and Arduino
Components Required

• Arduino Uno R3 board


• Ultrasonic sensor (HC-SR04)
• 16×2 LCD I2C Display
• Jumper Wires

21
Distance measurement using Ultrasonic sensor and Arduino
Circuit Diagram

22
Distance measurement using Ultrasonic sensor and Arduino
Setup

• Connect the Echo pin of the sensor to the D2 pin of the Arduino.

• Connect the Trig pin of the sensor to the D3 pin of the Arduino.

• Navigate to Tools and select board and port.

• Verify and compile the code, then upload the code to the Arduino Uno
R3 board.

• Monitor the output in the Serial monitor (Set the baud rate as 9600).

• To open Serial monitor Tools>Serial Monitor or (Ctrl+Shift+M).

2
Distance measurement using Ultrasonic sensor and Arduino
Arduino Code (Output in Serial monitor)
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10; // defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

2
Distance measurement using Ultrasonic sensor and Arduino
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro
seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel
time in
microseconds
duration = pulseIn(echoPin, HIGH); // Calculating the distance
distance = duration * 0.034 / 2; // Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

2
Distance measurement using Ultrasonic sensor and Arduino
Code Explanation
● First we have to define the Trig and Echo pins.
● In this case they are the pins number 9 and 10 on the Arduino Board and they are
named trigPin and echoPin.
● Then we need a Long variable, named “duration” for the travel time that we will get
from the sensor and an integer variable for the distance.
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;

// defines variables
long duration;
int distance;

2
Distance measurement using Ultrasonic sensor and Arduino
Code Explanation
● In the setup we have to define the trigPin as an output and the echoPin as an Input and
also start the serial communication for showing the results on the serial monitor.

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

2
Distance measurement using Ultrasonic sensor and Arduino
Code Explanation
● In the loop first we have to make sure that the trigPin is clear so you have to set that
pin on a LOW State for just 2 µs. Now for generating the Ultra sound wave we have to
set the trigPin on HIGH State for 10 µs.

// Clears the trigPin


digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds


digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

2
Distance measurement using Ultrasonic sensor and Arduino
Code Explanation
● Using the pulseIn() function we read the travel time and put that
value into the variable “duration”. This function has 2 parameters, the
first one is the name of the Echo pin and for the second is the state of
the pulse we are reading, either High or Low.

// Reads the echoPin, returns the sound wave travel time in


microseconds duration = pulseIn(echoPin, HIGH);

● In this case, we need this set to it HIGH, as the HC-SR04 sensors sets
the Echo pin to High after sending the 8 cycle ultrasonic burst from
the transmitter. This actually starts the timing and once we receive
the reflected sound wave the Echo pin will go to Low which stops the
timing. At the end the function will return the length of the pulse in
microseconds.

2
Distance measurement using Ultrasonic sensor and Arduino
Code Explanation
● For getting the distance we will multiply the duration by 0.034 and
divide it by 2 as we explained this equation previously.

// Calculating the distance


distance= duration*0.034/2;

// Prints the distance on the Serial Monitor


Serial.print("Distance: ");
Serial.println(distance);
● At the end we will print the value of the distance on the Serial Monitor.

3
Distance measurement using Ultrasonic sensor and Arduino

3
How to use IR Sensor Module with Arduino
Overview
● An IR (Infrared) Sensor Module is a device designed to
detect infrared light, commonly used in remote control
systems, proximity sensors, and line-following robots
● The IR Sensor Module typically includes an infrared
transmitter and receiver. The transmitter emits infrared light, and
the receiver detects the reflected infrared light from nearby objects.
When interfaced with an Arduino, this module allows the board
to detect the presence, distance, and even the movement of
objects based on infrared light reflections.

3
Connecting the IR Sensor Module to an Arduino UNO board
IR (Infrared) Sensor Module
● The IR Infrared Obstacle Avoidance Sensor Module based on the LM393 IC is a
specialized electronic component designed for detecting obstacles and implementing
basic proximity sensing.

IR Sensor Module

3
Connecting the IR Sensor Module to an Arduino UNO board
IR (Infrared) Sensor Module

IR Sensor Module

● An IR Obstacle Sensor operates based on the principle of infrared light reflection to


detect the presence of obstacles.
● Under normal conditions without any obstructing object, the infrared receiver does not
pick up any signals.
● However, when an object appears in front of the sensor, it blocks and reflects the
infrared light back towards the sensor.
● This reflection is then detected by the infrared receiver, indicating the presence of an
obstacle.

3
Connecting the IR Sensor Module to an Arduino UNO board
Working Principle of IR Sensor
● This sensor operates on the principle of infrared reflection. When
there are no obstacles in its path, the emitted infrared rays gradually
diminish in intensity with increasing distance and eventually dissipate
entirely.

3
Connecting the IR Sensor Module to an Arduino UNO board
Working Principle of IR Sensor
● However, the presence of an obstacle alters this behavior.
● As the infrared ray encounters an obstacle, it is reflected back towards
the sensor.
● The infrared receiver then picks up this reflected signal, identifying the
presence of an obstacle ahead.
● The range within which the sensor can detect obstacles is adjustable,
thanks to the inclusion of a built-in potentiometer.

3
Connecting the IR Sensor Module to an Arduino UNO board
Key Components and Functionality of IR Sensor Module

 An obstacle avoidance sensor is primarily composed of three components: an infrared


transmitter, an infrared receiver, and a potentiometer.

3
Connecting the IR Sensor Module to an Arduino UNO board

1. Infrared Emitting and Receiving Tubes: The module


includes a pair of tubes; one transmits infrared light, and
the other receives the reflected infrared waves. The
transmitting tube emits IR light, which, upon encountering
an obstacle, reflects back towards the module.

3
Connecting the IR Sensor Module to an Arduino UNO board

2. LM393 Comparator IC: The core of the module is the


LM393 IC, a low-power, low-offset voltage dual
comparator. This IC is responsible for comparing the
intensity of the received IR light with a reference value to
determine the presence of an obstacle.

3
Connecting the IR Sensor Module to an Arduino UNO board

3. Green Indicator LED: When the reflected IR waves are


detected by the receiving tube, the comparator IC
processes this information. If the received signal meets
the threshold set by the module, it triggers the green LED
to light up, indicating obstacle detection.

4
Connecting the IR Sensor Module to an Arduino UNO board

4. Adjustable Sensitivity: The module typically includes a


potentiometer to adjust the sensitivity of the sensor. By
tuning this potentiometer, users can set the threshold
distance for obstacle detection, making the module
versatile for different applications.

4
Connecting the IR Sensor Module to an Arduino UNO board

5. Output Signal: When an obstacle is detected within the


preset range, the module outputs a digital signal. This
signal can be fed to a microcontroller like an Arduino for
further processing and integration into larger systems.

4
Connecting the IR Sensor Module to an Arduino UNO board
IR Sensor Module Pinout
● The pinout of a typical IR (Infrared) Sensor Module is straightforward,
usually consisting of three main pins that are crucial for its operation
and interfacing with other devices like microcontrollers.

• VCC: Power Supply – Connects to the power source, typically 3.3V


to 5V.
• GND: Ground Pin.
• DO: Digital Output – Outputs a digital signal (high/low) indicating
obstacle detection.
4
Interfacing IR Sensor Module with Arduino

4
Interfacing IR Sensor Module with Arduino

• Begin by connecting the IR sensor’s VCC pin to the 5V output on the


Arduino for power.
• Then, connect the GND pin of the IR sensor to one of the GND pins on
the Arduino.
• Finally, connect the Digital Output Pin (DO) to a digital pin (such as D2)
on the Arduino.

4
Interfacing IR Sensor Module with Arduino
• This project is an obstacle detection system.
• The main idea is to use an IR sensor module to detect the presence of
an object.
• When an object is detected within a certain range of the sensor, the
LED is switched on.
• Conversely, when no object is detected, or it is beyond the sensing
range, the LED remains off.
• The range can be adjusted via the sensor’s built-in potentiometer, and
digital or analog signals can be used to determine the presence and
proximity of the obstacle.

46
Interfacing IR Sensor Module with Arduino
// Define pin numbers
const int ledPin = 3; // LED connected to digital pin 5
const int irSensorPin = 2; // IR Sensor connected to digital pin 2

void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(irSensorPin, INPUT); // Set the IR sensor pin as input
}

void loop() {
int sensorState = digitalRead(irSensorPin); // Read the state from the IR sensor

// The sensor outputs LOW when it detects an obstacle


if (sensorState == LOW) {
digitalWrite(ledPin, HIGH); // Turn the LED on
} else {
digitalWrite(ledPin, LOW); // Turn the LED off
}

delay(100); // Wait for 100 milliseconds


}
4
Interfacing IR Sensor Module with Arduino
// Define pin numbers
const int ledPin = 3; // LED connected to digital pin 5
const int irSensorPin = 2; // IR Sensor connected to digital pin 2

void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(irSensorPin, INPUT); // Set the IR sensor pin as input
}

void loop() {
int sensorState = digitalRead(irSensorPin); // Read the state from the IR sensor

// The sensor outputs LOW when it detects an obstacle


if (sensorState == LOW) {
digitalWrite(ledPin, HIGH); // Turn the LED on
} else {
digitalWrite(ledPin, LOW); // Turn the LED off
}

delay(100); // Wait for 100 milliseconds


}
4
Interfacing IR Sensor Module with Arduino
Testing the IR Sensor Arduino Project Working
After uploading the code to the Arduino UNO Board, the device is ready for testing.

To test it, place the IR Sensor in an environment with potential obstacles. If there is no
object within the sensor’s range, the LED remains off.

4
Interfacing IR Sensor Module with Arduino
Testing the IR Sensor Arduino Project Working
If an object is detected within the sensing range of the IR sensor, the LED is switched on.
You can test this by moving your hand or an object in front of the IR sensor.

5
51

You might also like