About The Project
In this project, we will learn how to measure distance using an ultrasonic sensor.
Ultrasonic Sensor
The working principle of an ultrasonic distance sensor is based on the echo of high-frequency sound (Ultrasonic Sound)waves.
- Sound Wave Emission: The sensor emits short pulses of ultrasonic sound waves (typically above the range of human hearing, around 40 kHz).
- Travel to Object: These sound waves travel through the air and bounce off any obstacle or object in their path.
- Echo Reception: The sensor then listens for the echoes of the sound waves reflected back from the object.
Time Calculation : By measuring the time interval between sending the sound pulse and receiving its echo, the Arduino Uno calculates the distance to the object using the formula: Distance = Speed of Sound ×Time interval between sending the sound pulse and receiving its echo/2.
Speed of Sound: The speed of sound wave in air is approximately 343 meters per second .
Circuit Wiring
This Arduino sketch demonstrates a basic implementation of interfacing with an HC-SR04 ultrasonic sensor to measure distances. It uses the time-of-flight method where the Arduino sends a trigger pulse and measures the time it takes for the pulse to return after bouncing off an object. The calculated distance is then displayed on the Serial Monitor.
Programme Code
//www.matthewtechub.com
//Ultrasonic Sensor Basic
const int trigPin = 10;
const int echoPin = 7;
float time_us;
float distance_cm;
void setup() {
// begin serial port
Serial.begin (9600);
// configure the trigger pin to output mode
pinMode(trigPin, OUTPUT);
// configure the echo pin to input mode
pinMode(echoPin, INPUT);
}
void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// measure time duration of pulse from ECHO pin
time_us = pulseIn(echoPin, HIGH);
// To calculate the distance use following formula
distance_cm = 0.017 * time_us;
// print the value to Serial Monitor
Serial.print("Time duration: ");
Serial.print(time_us);
Serial.println(" microseconds");
Serial.print("distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
delay(1000);
}
Code Explanation
const int trigPin = 10;
const int echoPin = 7
float time_us
float distance_cm
- trigPin and echoPin: These constants define the Arduino pins connected to the trigger (TRIG) and echo (ECHO) pins of the HC-SR04 ultrasonic sensor, respectively.
- time_us: This float variable holds the duration of the pulse received from the ultrasonic sensor in microseconds.
- distance_cm: This float variable stores the calculated distance in centimeter.
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
- Serial.begin(9600): Initializes serial communication between the Arduino and your computer at a baud rate of 9600 bits per second. This allows you to send data from the Arduino to the Serial Monitor for debugging and monitoring.
- pinMode(trigPin, OUTPUT): Configures trigPin (pin 10) as an output pin. The Arduino will use this pin to send a short 10-microsecond pulse to the TRIG pin of the HC-SR04 sensor.
- pinMode(echoPin, INPUT): Configures echoPin (pin 7) as an input pin. The Arduino will read the duration of the pulse sent back by the HC-SR04 sensor, which is connected to the ECHO pin.
void loop() {
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
time_us = pulseIn(echoPin, HIGH);
distance_cm = 0.017 * time_us;
Serial.print(“Time duration: “);
Serial.print(time_us);
Serial.println(” microseconds”);
Serial.print(“distance: “);
Serial.print(distance_cm);
Serial.println(” cm”);
delay(1000);
}
- Triggering the Sensor:
- digitalWrite(trigPin, HIGH) sends a 10-microsecond pulse to the trigPin, which triggers the HC-SR04 sensor to send out an ultrasonic pulse.
- delayMicroseconds(10) holds the pulse high for 10 microseconds.
- digitalWrite(trigPin, LOW) turns off the pulse.
- Measuring Distance:
- pulseIn(echoPin, HIGH) measures the duration (in microseconds) of the pulse received on echoPin. This duration corresponds to the time it takes for the ultrasonic pulse to travel to an object and back.
- distance_cm = 0.017 * time_us calculates the distance in centimeters using the speed of sound in air (approximately 343 meters per second).
- Serial Output:
- Serial.print(“distance: “), Serial.print(distance_cm), and Serial.println(” cm”) print the measured distance in centimeters to the Serial Monitor.
- Delay:
- delay(1000) adds a 1000-millisecond delay before taking the next distance measurement. This slows down the loop to ensure that distance measurements are not taken too frequently10