LAB REPORT
Experiment No: 06
Experiment Name: Ultrasonic and PIR Sensors Interfacing with Arduino
Course Code: CSE234
Course Title: Embedded System and IOT lab
Submitted To:
Name: Mohammad Al Rasel
Designation: Lecturer
Department: CSE
Daffodil International University
Submitted By:
Name: Md Nayeem Islam
ID: 221-15-5621
Section: 61 (S2)
Semester: Summer, 25
Department: CSE
Daffodil International University
Submission Date: 2025-08-07
Objective:
1. To interface the Ultrasonic sensor (HC-SR04) with Arduino to measure distance.
2. To interface the PIR motion sensor with Arduino for motion detection.
3. To read distance values from the Ultrasonic sensor and motion status from the PIR sensor.
4. To display sensor readings and motion status on the Serial Monitor.
5. To understand how digital sensors communicate with microcontrollers and trigger outputs.
Theory:
The Ultrasonic Sensor (HC-SR04) measures distance by transmitting ultrasonic waves and measuring the time taken for the echo to return. Using the
speed of sound in air, the distance can be calculated using the formula:
Distance (cm)=Time (µs)×0.03432\text{Distance (cm)} = \frac{\text{Time (µs)} \times 0.0343}{2}Distance (cm)=2Time (µs)×0.0343
It works with a Trigger pin (to send pulse) and an Echo pin (to receive reflection).
The PIR (Passive Infrared) Sensor detects motion by sensing infrared radiation changes caused by moving objects (like humans or animals). When
motion is detected, it outputs a HIGH signal; otherwise, it outputs LOW.
This experiment shows how Arduino can read data from two different types of sensors and process them simultaneously.
Components required: Arduino Uno board, Ultrasonic Sensor (HC-SR04), PIR Motion Sensor, Breadboard, Jumper wires, USB cable, Laptop with
Arduino IDE
Experimental Setup:
The circuit is set up as follows:
Ultrasonic Sensor:
o VCC → 5V
o GND → GND
o Trigger → Arduino digital pin 9
o Echo → Arduino digital pin 10
PIR Sensor:
o VCC → 5V
o GND → GND
o OUT → Arduino digital pin 2
Diagram:
Code:
#define trigPin 9
#define echoPin 10
#define pirPin 2
long duration;
int distance;
int pirState = LOW;
int val = 0;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pirPin, INPUT);
Serial.begin(9600);
}
void loop() {
// Ultrasonic Sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.0343 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// PIR Sensor
val = digitalRead(pirPin);
if (val == HIGH) {
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
}
} else {
if (pirState == HIGH) {
Serial.println("Motion ended!");
pirState = LOW;
}
}
delay(500);
}
Results:
1. Both sensors successfully interfaced with Arduino.
2. Real-time distance measurements were displayed on the Serial Monitor.
3. PIR sensor accurately detected motion.
4. Both sensors worked simultaneously without interference.
5. Environmental changes were reflected in the readings.
Discussion:
The Ultrasonic sensor provided accurate distance readings, while the PIR sensor reliably detected movement. The experiment demonstrates multi-
sensor integration with Arduino. Adjustments in PIR sensitivity and delay between Ultrasonic readings can optimize performance in real applications.
Conclusion:
The experiment successfully interfaced Ultrasonic and PIR sensors with Arduino. Both distance measurement and motion detection were
implemented and displayed on the Serial Monitor. This knowledge can be applied to automation, robotics, and security systems.