Detectting Motion With PIR Sensor
Detectting Motion With PIR Sensor
Things) 1
PIR Sensor Interfacing with Arduino UNO
Introduction
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
• 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
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-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:
9
PIR Sensor Interfacing with Arduino UNO
Motion Sensor with Arduino
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
12
Arduino Sketch
const int PIN_TO_SENSOR = 2; // the pin that OUTPUT pin of sensor is connected to
void setup() {
Serial.begin(9600); // initialize serial
13
Arduino Sketch
void loop() {
pinStatePrevious = pinStateCurrent; // store old state
pinStateCurrent = digitalRead(PIN_TO_SENSOR); // read new state
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.
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.
20
Distance measurement using Ultrasonic sensor and Arduino
Components Required
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.
• 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).
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.
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.
● 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.
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
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
3
Connecting the IR Sensor Module to an Arduino UNO board
3
Connecting the IR Sensor Module to an Arduino UNO board
3
Connecting the IR Sensor Module to an Arduino UNO board
4
Connecting the IR Sensor Module to an Arduino UNO board
4
Connecting the IR Sensor Module to an Arduino UNO board
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.
4
Interfacing IR Sensor Module with 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
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
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