How Does an Ultrasonic Sensor Really Work?
A beginner-friendly tutorial explaining how the HC-SR04 ultrasonic sensor uses sound waves, echo time, and Arduino code to measure distance.
Devices & Components
1
Breadboard - 400 contacts
1
10 jumper wires 150mm male
1
Arduino Nano
1
Ultrasonic Sensor - HC-SR04
Hardware & Tools
oscilloscope
Software & Tools
Arduino IDE
Project description
Code
CODE
c
1/*Triggering the Sensor 2The microcontroller sets the TRIG pin HIGH for 10 μs to start measurement. 3The sensor emits 40 kHz ultrasonic waves. 4The ECHO pin goes HIGH, marking the start of wave travel. 5 6The ultrasonic waves move outward through the air. 7If they hit an object, they bounce back toward the sensor. 8 9The sensor detects the reflected waves. 10Once the waves return, the ECHO pin goes LOW. 11 12The microcontroller measures the time the ECHO pin stayed HIGH. 13The distance is calculated using the formula: 14 15If no object is detected, the ECHO pin automatically goes LOW after 38 ms. 16pulseIn(ECHO_PIN, HIGH, 38000) ensures the function doesn’t wait forever.*/ 17 18#define ECHO_PIN 3 19#define TRIGGER_PIN 4 20 21/* 22 Explanation of Echo and Trigger signals: 23 - The ultrasonic sensor has two main pins for communication: TRIGGER and ECHO. 24 - The TRIGGER pin is used to send an ultrasonic pulse (a short burst of high-frequency sound waves). 25 - The ECHO pin listens for the reflected signal (echo) that bounces back after hitting an object. 26 - By measuring the time difference between sending and receiving the pulse, we can calculate the distance. 27*/ 28 29// Variables to keep track of timing 30unsigned long last_time_ultrasonic_trigger = millis(); 31unsigned long ultrasonic_trigger_delay = 100; // Minimum delay between triggers (in milliseconds) 32 33 34// Function to send an ultrasonic pulse from the sensor 35void trigger_ultrasonic_sensor(){ 36 // Ensuring a clean signal before triggering the pulse 37 digitalWrite(TRIGGER_PIN, LOW); // This ensures the trigger pin is LOW before we send the pulse 38 delayMicroseconds(2); // A small delay to stabilize the signal 39 40 // Sending a 10-microsecond pulse to the ultrasonic sensor 41 digitalWrite(TRIGGER_PIN, HIGH); // This starts the ultrasonic pulse 42 delayMicroseconds(10); // The pulse must last at least 10 microseconds for proper operation 43 digitalWrite(TRIGGER_PIN, LOW); // Stop the pulse 44 45 /* 46 How the sensor works: 47 - When the TRIGGER pin is set HIGH for at least 10 microseconds, the ultrasonic sensor sends out an 8-cycle burst of 40 kHz sound waves. 48 - These sound waves travel through the air and bounce back when they hit an object. 49 - The ECHO pin goes HIGH when the sound waves are sent and stays HIGH until the reflected waves are detected. 50 - The duration that the ECHO pin remains HIGH is proportional to the time taken by the sound waves to travel to the object and return. 51 */ 52} 53 54// Function to calculate the distance measured by the ultrasonic sensor 55double get_distance() 56{ 57 /* 58 pulseIn() measures the time (in microseconds) that the ECHO_PIN stays HIGH. 59 - When the ultrasonic pulse is sent, the ECHO pin waits for the sound to bounce back. 60 - Once the reflected sound is detected, the ECHO pin goes LOW. 61 - The function pulseIn() returns the total time (in microseconds) that the ECHO pin remained HIGH, 62 which represents the round-trip travel time of the sound wave. 63 */ 64 65 // we are checking how long it takes to run this line of code. you can see that it can take 66 // a long time if something is far away(23 ish millis), and if you have a lont of sensors that 67 //can block the code for a lot of time as wee are running every 100 millis the sensor. 68 //that is why we in the next proj will use interrupts 69 unsigned long timebegin = millis(); 70 double duration_micros = pulseIn(ECHO_PIN, HIGH); 71 unsigned long timeend = millis(); 72 unsigned long duration = timeend - timebegin; 73 Serial.print("The duration is: "); 74 Serial.println(duration); 75 /* 76 Explanation of the distance calculation: 77 - The speed of sound in air is approximately 343 meters per second or 0.0343 cm per microsecond. 78 - The sound wave travels to the object and back, so the measured time is for the round trip. 79 - To get the one-way distance, we divide the time by 2. 80 - The formula used is: 81 Distance (cm) = (Time in microseconds × Speed of sound in cm/µs) / 2 82 Distance (cm) = (duration_micros × 0.0343) / 2 83 Distance (cm) = duration_micros / 58.0 (since 1/ (0.0343 × 2) ≈ 1/58) 84 */ 85 double distance = duration_micros / 58.0; // Convert time to distance in centimeters 86 return distance; 87} 88 89void setup() { 90 Serial.begin(115200); 91 92 pinMode(ECHO_PIN, INPUT); // Set the echo pin as input to receive reflected pulses 93 pinMode(TRIGGER_PIN, OUTPUT); // Set the trigger pin as output to send ultrasonic pulses 94} 95 96void loop() { 97 unsigned long time_now = millis(); 98 99 // Ensure we only trigger the ultrasonic sensor after the specified delay 100 if (time_now - last_time_ultrasonic_trigger > ultrasonic_trigger_delay) 101 { 102 last_time_ultrasonic_trigger += ultrasonic_trigger_delay; 103 trigger_ultrasonic_sensor(); // Send a new ultrasonic pulse 104 Serial.println(get_distance()); // Measure and print the distance to the serial monitor 105 } 106}
Comments
Only logged in users can leave comments