user82050: I am currently working on a project which integrates esp32 and HC-SR04 and send the data from HC-SR04 to my own domain. The connection diagram is this

And the code which I use is this,
const int trigPin = 5;
const int echoPin = 18;
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701

long duration;
float distanceCm;
float distanceInch;

void setup() {
 Serial.begin(115200); // Starts the serial communication
 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
 pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}

void loop() {
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);

 duration = pulseIn(echoPin, HIGH);

 distanceCm = duration * SOUND_SPEED/2;

 distanceInch = distanceCm * CM_TO_INCH;

 Serial.print("Distance (cm): ");
 Serial.println(distanceCm);
 Serial.print("Distance (inch): ");
 Serial.println(distanceInch);

 delay(1000);
}

But the module doesn't give out the distance. Can anyone suggest a better method for connection. Thanks in advance!

user57952: Suriya,
You aren't crazy - it appears the ESP32 GPIO pins (at least the ones I tested) won't drive the HC-04 sonar sensor trigger pin sufficiently high to trigger a sonar pulse.  I tested the NewPingExample.pde code example from the NewPing library's 'example' folder on both a MEGA2560 and a ESP32_DevKitC_V4. The MEGA implementation worked right off the bat, but I never could get the ESP module to work.  So, I took a look at the Trigger/Echo pin (by default the NewPing.h file uses the 'Single Pin' mode, so you have to make sure the TRIGGER_PIN and ECHO_PIN defines use the same pin).  The two scope traces below are from the MEGA2560 and the ESP32.  As you can see, the ESP32 output only goes to 1.865V - not high enough to actually trigger the device.  The MEGA implementation output goes to over 3V and consistently triggers the echo (the big rectangle to the right of the narrow trigger signal).
You might want to try using the STMicro VL53L0X time-of-flight LIDAR chip instead - it is much smaller, uses less current, and doesn't suffer from self-interference if you plan to use more than one sensor. See this post on my blog site for some ideas



82050: <p>I am currently working on a project which integrates esp32 and HC-SR04 and send the data from HC-SR04 to my own domain. The connection diagram is this
<a href="https://i.stack.imgur.com/mZdo1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mZdo1.png" alt="enter image description here" /></a>
And the code which I use is this,</p>
<pre><code>const int trigPin = 5;
const int echoPin = 18;
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701

long duration;
float distanceCm;
float distanceInch;

void setup() {
 Serial.begin(115200); // Starts the serial communication
 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
 pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}

void loop() {
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);

 duration = pulseIn(echoPin, HIGH);

 distanceCm = duration * SOUND_SPEED/2;

 distanceInch = distanceCm * CM_TO_INCH;

 Serial.print(&quot;Distance (cm): &quot;);
 Serial.println(distanceCm);
 Serial.print(&quot;Distance (inch): &quot;);
 Serial.println(distanceInch);

 delay(1000);
}
</code></pre>
<p>But the module doesn't give out the distance. Can anyone suggest a better method for connection. Thanks in advance!</p>
 57952: <p>Suriya,</p>
<p>You aren't crazy - it appears the ESP32 GPIO pins (at least the ones I tested) won't drive the HC-04 sonar sensor trigger pin sufficiently high to trigger a sonar pulse.  I tested the NewPingExample.pde code example from the NewPing library's 'example' folder on both a MEGA2560 and a ESP32_DevKitC_V4. The MEGA implementation worked right off the bat, but I never could get the ESP module to work.  So, I took a look at the Trigger/Echo pin (by default the NewPing.h file uses the 'Single Pin' mode, so you have to make sure the TRIGGER_PIN and ECHO_PIN defines use the same pin).  The two scope traces below are from the MEGA2560 and the ESP32.  As you can see, the ESP32 output only goes to 1.865V - not high enough to actually trigger the device.  The MEGA implementation output goes to over 3V and consistently triggers the echo (the big rectangle to the right of the narrow trigger signal).</p>
<p>You might want to try using the STMicro VL53L0X time-of-flight LIDAR chip instead - it is much smaller, uses less current, and doesn't suffer from self-interference if you plan to use more than one sensor. See <a href="https://www.fpaynter.com/2020/06/replacing-hc-sro4-ultrasonic-sensors-with-vl53l0x-arrays/" rel="nofollow noreferrer">this post</a> on my blog site for some ideas</p>
<p><a href="https://i.stack.imgur.com/aiHaP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aiHaP.png" alt="MEGA2560 Implementation" /></a>
<a href="https://i.stack.imgur.com/P33Rg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/P33Rg.png" alt="ESP32 Implmentation" /></a></p>
