American International University- Bangladesh
(AIUB)
Lab Report Title: Interfacing the Arduino with an external sensor using serial
communication protocol for implementing an obstacle detection system.
Lab Report No: 07 Date of 05-12-2023
Submission:
Course Title: Microprocessor and Section: L
Embedded Systems
Semester: Fall 2023-24 Course Teacher: Protik Parvez Sheikh
Group No:
No Name ID Program
1 Mahmud, Eshtab Rak 21-45387-3 Bsc. Cse
2 Islam, Tanjib Ul 21-45393-3 Bsc. Cse
3 Shahrier, Md. Akib 21-45428-3 Bsc. Cse
4 Anjum, Hasin 21-45430-3 Bsc. Cse
5 Ekhon, Meher Ab 21-45438-3 Bsc. Cse
Submitted By: Islam, Tanjib Ul
Student ID: 21-45393-3
Title:
Interfacing the Arduino with an external sensor using serial communication protocol for
implementing an obstacle detection system.
Introduction:
The objectives of this experiment are to implement a simple obstacle detection system in Arduino
IDE and to implement a simple obstacle detection system using an Arduino microcontroller.
Apparatus:
1) Arduino IDE (2.0.1 or any recent version)
2) Arduino UNO (R3) board
3) Sonar Sensor (HCSR04)
4) Breadboard
5) LEDs (red, green and yellow) 6) Resistor of 100 Ω 7) Jumper Wires.
Experimental Procedure:
Connect the circuit diagram as follows, upload the program into the board, and then run the program.
Connect three LEDs of red, green, and yellow to pins 2, 3, and 4, respectively through three 100 W
resistors to limit the current flow through LEDs (though it is not shown in the circuit diagram).
Observe the object detection distances in the serial monitor and based on that LEDs turning ON.
Connection diagram
Code:
// define the pin numbers
const int trigPin = 11;
const int echoPin = 12;
// define variables
long duration;
float distance, distanceInches, distanceThreshold;
void setup() {
[Link](9600); // Starts the serial communication
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(2, OUTPUT); // Sets pins 2, 3, and 4 as the Output pin
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 microseconds
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/2) * 1e-6 * 340 * 100;
distanceinches = (distance / 2.54);
// Prints the distance on the Serial Monitor
[Link]("Distance = ");
[Link](distance);
[Link](“cm; ”);
[Link]("Distance = ");
[Link](distanceinches);
[Link](“inches”);
// set threshold distance to activate LEDs
distanceThreshold = 80;
if (distance > distanceThreshold) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if (distance < distanceThreshold && distance > distanceThreshold-30) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if (distance < distanceThreshold-30 && distance > distanceThreshold-50) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}
if (distance < distanceThreshold-50 && distance > distanceThreshold-70 ) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(200); // Wait for 200 millisecond(s)
}
Hardware Result:
Figure: 1 LED turned ON when a distance as at a far.
Figure: 2 LED turned ON when a distance as at a medium distance
Figure: 3 LED turned ON when a distance as at a close distance
Simulation:
Figure: First LED ON when the object was far away.
Figure Second LED ON when the object was in Medium Distance.
Figure: Third LED ON when the object was in Close Distance