Course Code: EEE 444
Course Title: Integrated Design Project II
Experiment No.: 02
Experiment Title: Ultrasonic Distance Measurement using Arduino
Student Name : Md Shakil Ahmed
Student ID : 213001007
Section : 213_D2
Date of Assignment : 14-02-2025
Date of Submission : 21-02-2025
Submitted to: Md. Istiac Ahmed, Lecturer, Green University of Bangladesh
To be Filled by the Teacher. Don’t Write Anything Here!
Received Date:
Late Submission:
Obtained Mark:
Date of Return:
Remarks:
Signature:
Department of Electrical and Electronic Engineering
Green University of Bangladesh
Task 01
Circuit Diagram:
Following is the circuit setup for an Ultrasonic Distance Measurement using Arduino. The Equipment
of LEDs,Resistors, HC-SR04 Sonar Sensor Module, Breadboard and Jumper wires.LED is connected to the
digital pin 8,9,10,11,12 of the Arduino Uno via a resistor of 1 kΩ. This resistor acts as a current-limiting
resistor here.And Sonar Sensor connected to the digital pin 6 and 7.
Figure 1 Distance Measurement circuit
Arduino Code :
const int Trig = 7;
const int Echo = 6;
const int RedLED1 = 8;
const int RedLED2 = 9;
const int RedLED3 = 10;
const int GreenLED = 11;
const int BlueLED = 12;
void setup() {
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
pinMode(RedLED1, OUTPUT);
Page 2 of 6
pinMode(RedLED2, OUTPUT);
pinMode(RedLED3, OUTPUT);
pinMode(GreenLED, OUTPUT);
pinMode(BlueLED, OUTPUT);
Serial.begin(9600); // Start serial communication
}
void loop() {
// Send a 10-microsecond pulse to the TRIG pin
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
long duration, distance; // defining variables
duration=pulseIn(Echo,HIGH);
distance=0.034*duration*0.5; // measuring the distance in cm unit
Serial.print("Distance is: "); // accessing the serial monitor
Serial.println(distance);
delay(100);
// Turn off all LEDs initially
digitalWrite(RedLED1, LOW);
digitalWrite(RedLED2, LOW);
digitalWrite(RedLED3, LOW);
digitalWrite(GreenLED, LOW);
digitalWrite(BlueLED, LOW);
// Determine which LEDs to turn on based on distance
if (distance < 5) {
digitalWrite(RedLED1, HIGH); }
else if (distance >= 5 && distance < 10) {
digitalWrite(RedLED1, HIGH);
digitalWrite(RedLED2, HIGH);}
else if (distance >= 10 && distance < 20) {
digitalWrite(RedLED1, HIGH);
digitalWrite(RedLED2, HIGH);
digitalWrite(RedLED3, HIGH); }
else if (distance >= 20 && distance < 400) { // 400 cm is HC-SR04 max range
digitalWrite(GreenLED, HIGH);
}
else {
digitalWrite(BlueLED, HIGH);
}
delay(200); // Short delay to stabilize readings
}
3|Page
Task 02
Circuit Diagram:
Following is the circuit setup for an Ultrasonic Distance Measurement using Arduino. The Equipment
of LEDs,Resistors, HC-SR04 Sonar Sensor Module, Breadboard and Jumper wires.LED is connected to the
digital pin 13 of the Arduino Uno via a resistor of 1 kΩ. This resistor acts as a current-limiting resistor
here.And Sonar Sensor connected to the digital pin 6 and 7.
Figure 2 Distance Measurement circuit
Arduino Code :
const int Trig = 7;
const int Echo = 6;
const int Alarm = 13; // Alarm (LED )
const int numReadings = 20; // Total readings to average
const int interval = 5000; // 5 seconds interval (5000 ms)
const int threshold = 50; // Threshold distance (cm)
void setup() {
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
pinMode(Alarm, OUTPUT);
Serial.begin(9600);
}
Page 4 of 6
void loop() {
long totalDistance = 0;
int validReadings = 0;
Serial.println("Starting distance measurement...");
for (int i = 0; i < numReadings; i++) {
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
long duration, distance; // defining variables
duration=pulseIn(Echo,HIGH);
distance=0.034*duration*0.5; // measuring the distance in cm unit
Serial.print("Distance is: "); // accessing the serial monitor
Serial.println(distance);
delay(interval); // Wait 5 seconds before next reading
}
if (validReadings > 0) {
int avgDistance = totalDistance / validReadings;
Serial.print("Average Distance: ");
Serial.print(avgDistance);
Serial.println(" cm");
if (avgDistance > threshold) {
Serial.println("Threshold exceeded! Turning on alarm...");
digitalWrite(Alarm, HIGH); }
else {
Serial.println("Distance is within safe limits.");
digitalWrite(Alarm, LOW);
}}
else {
Serial.println("No valid readings obtained.");
}
delay(10000); // Wait 10 seconds before the next cycle
} }
else if (distance >= 20 && distance < 400) { // 400 cm is HC-SR04 max range
digitalWrite(GreenLED, HIGH);
}
else {
digitalWrite(BlueLED, HIGH);
}
delay(200); // Short delay to stabilize readings
}
5|Page
Report Questions and Discussion
Q1. What is the range of values returned by the analogRead function, and how is it related to the resolution
of the analog-to-digital converter (ADC) on the Arduino board?
Answer:
The Arduino analogRead() function splits the reference voltage (often 0V to 5V) into 1024 levels and
returns values between 0 and 1023, which corresponds to a 10-bit ADC resolution. For increased precision,
certain boards, such as the Arduino Due, feature a 12-bit ADC that provides values ranging from 0 to 4095.
Q2. How can you improve the accuracy of readings obtained with analogRead?
Answer:
You can use a capacitor to filter noise, take numerous readings and average them, use a steady reference
voltage, and keep wires short to prevent interference in order to increase the accuracy of analogRead().
Q3. Discuss situations where you might choose to use pulseIn instead of digitalRead or analogRead for
signal measurement.
Answer:
Unlike digitalRead(), which only verifies the current status of a pin, pulseIn() can be used to measure the
length of a HIGH or LOW pulse. When timing information is crucial, it is utilized in IR communication,
PWM signal monitoring, and ultrasonic sensors.
Q4. How can you use the analogRead function to measure the voltage of a sensor that provides a voltage
range beyond the Arduino's reference voltage?
Answer:
You can use an external ADC to measure a wider range or a voltage divider circuit to scale down a
sensor's output if it exceeds the Arduino's reference voltage. Additionally, a Zener diode can shield the
Arduino from high voltage.
Q5. Describe real-time constraints and considerations when using pulseIn or digitalRead in applications
that require precise timing, such as robotics or motor control.
Answer:
PulseIn() can cause delays in real-time applications such as robotics or motor control by stopping function
execution while awaiting a pulse. For accurate timing without compromising system responsiveness, timers
(millis(), micros()) or interrupts (attachInterrupt()) should be utilized instead.
Discussion :
This Experiment 2 is Ultrasonic Distance Measurement using Arduino. I have a lack of understanding
of this experiment. That's why I'm having a lot of difficulty in coding.
6|Page