Comments on: Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/ Learn ESP8266, ESP32, Arduino, and Raspberry Pi Thu, 26 Mar 2026 09:25:47 +0000 hourly 1 https://wordpress.org/?v=6.8.5 By: Morse code https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/#comment-1172903 Thu, 26 Mar 2026 09:25:47 +0000 http://randomnerdtutorials.com/?p=1492#comment-1172903 The HC-SR04 ultrasonic sensor measures distance by timing the rhythmic, five-part echo of sound pulses, a methodical synchronization of signals that is perfectly mirrored by the structured format of Morse code numbers.

]]>
By: patrick https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/#comment-1165608 Mon, 02 Mar 2026 04:20:56 +0000 http://randomnerdtutorials.com/?p=1492#comment-1165608 thanks this helped me in school

]]>
By: aim dynamics https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/#comment-855741 Fri, 18 Aug 2023 11:50:42 +0000 http://randomnerdtutorials.com/?p=1492#comment-855741 In reply to Rajesh.

The TRIG_PIN is used to send the trigger signal, and the ECHO_PIN is used to receive the echo signal.
The pulseIn() function measures the duration of the pulse from the echo pin, which corresponds to the time taken for the ultrasonic wave to travel to the object and back.
The speed of sound is approximately 343 meters per second (or 34300 cm/s). The formula used in the code calculates the distance based on this speed and the time of flight of the ultrasonic wave.
The calculated distance is then printed to the Serial Monitor.

]]>
By: Rajesh https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/#comment-854727 Sat, 12 Aug 2023 10:10:55 +0000 http://randomnerdtutorials.com/?p=1492#comment-854727 In reply to Sid.

Once you program and test small arduino Nano model and then you dont need PC. You have to power arduino and HC-SR04 with 5v which you can do with small batteries. Arduino will need PC only when you are uploading and testing the program.

]]>
By: Rajesh https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/#comment-854721 Sat, 12 Aug 2023 09:40:29 +0000 http://randomnerdtutorials.com/?p=1492#comment-854721 Hi Rui
Thanks for this tutorial, I have tested my sensors with Newping library also having found about it from your tutorial.

I want to know distance as well as angle of the object.
I saw Arduino Radar based on HC-SR04 also. It requires a small servo motor to sweep the sensor to find out distance and angle.
I wanted to get that functionality without servo so I guessed I will have to use multiple HC-SR04 placed at different angles to cover the area under observation. I guess 5 sensors will be sufficient. Naturally, when object is there, mostly two sensors will be able to ‘see’ it and report the distance. The sensor which reports closest distance is the one I intend to use for measurement of distance and angle.
When two sensors pickup the object presence, I guess I should use mean(average) of the angle of both sensors. I hope I am clear enough of the requirement. Once this logic is tested, I can use distance data also to fine tune the angle. For example, If sensor A is reporting the distance of 24 inches and sensor B is reporting distance of 30 inches, the angle of the object is not exactly where sensor A is facing but moved slightly towards sensor B. When the object moves closer to sensor B, both distances will get almost equal measurement and then the angle is exactly between angle of both sensors.
Logically these calculations seem ok to me. Please give suggestions and guidance if I am on the right track.
I hope to see your tutorial on Doppler radar sensor RCWL-0516. It has very broad vision of 270 degrees so I chose ultrasonic sensors as I wanted angle of object calculations.
Have a great time ahead !

]]>
By: boris https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/#comment-826632 Thu, 23 Mar 2023 16:07:47 +0000 http://randomnerdtutorials.com/?p=1492#comment-826632 In reply to Robert L. Pendergast.

its a great project!! i connected there a GSM module, and it sends sms when its less than 15cm
AND IT WORKED !!

]]>
By: Sara Santos https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/#comment-810130 Fri, 13 Jan 2023 11:51:18 +0000 http://randomnerdtutorials.com/?p=1492#comment-810130 In reply to Nahuel.

Hi.
Yes, that’s possible. But we don’t have any projects about that specific subject.
Regards,
Sara

]]>
By: Nahuel https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/#comment-809975 Thu, 12 Jan 2023 19:47:46 +0000 http://randomnerdtutorials.com/?p=1492#comment-809975 Hi! thanks in advance. i would like to know if it possible to control volume of a mp3 file just by distance. so when i get close to de HC-SR04 the volume turns up, and when i get far the volume goes down.

thanks!

]]>
By: Xaalee https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/#comment-769552 Sun, 17 Jul 2022 06:35:58 +0000 http://randomnerdtutorials.com/?p=1492#comment-769552 In reply to Joost.

Where you see number that I add number ‘2’, this is for the second sensor.

int trigPin = 11; // Trigger
int echoPin = 12; // Echo
int trigPin2 = 9; // Trigger for second sensor
int echoPin2 = 10; // Echo for second sensor
long duration, cm, inches;
long duration2, cm2, inches2;

void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
}

void loop() {
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
pinMode(echoPin2, INPUT);
duration = pulseIn(echoPin, HIGH);
duration = pulseIn(echoPin2, HIGH);
// Convert the time into a distance
cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
cm2 = (duration/2) / 29.1;
inches = (duration/2) / 74; // Divide by 74 or multiply by 0.0135 // Divide by 29.1 or multiply by 0.0343
inches2 = (duration/2) / 74;
Serial.print(inches);
Serial.print(“in, “);
Serial.print(cm);
Serial.print(“cm”);
Serial.println();
Serial.print(inches2);
Serial.print(“in2, “);
Serial.print(cm2);
Serial.print(“cm2”);
Serial.println();

delay(250);
}

]]>
By: Sara Santos https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/#comment-768229 Mon, 11 Jul 2022 08:46:45 +0000 http://randomnerdtutorials.com/?p=1492#comment-768229 In reply to Shiva.

Hi.
You can check the following tutorials with the SIM900:
https://randomnerdtutorials.com/?s=SIM900
At the moment, we don’t have any GPS examples with the SIM900. But there are several libraries with examples.
Regards,
Sara

]]>