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 enter image description here 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);
 digitalWrite(trigPin, HIGH);
 digitalWrite(trigPin, LOW);

 duration = pulseIn(echoPin, HIGH);

 distanceCm = duration * SOUND_SPEED/2;

 distanceInch = distanceCm * CM_TO_INCH;

 Serial.print("Distance (cm): ");
 Serial.print("Distance (inch): ");


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

  • 1
    Can you be more specific about the problem? If it "doesn't give out the distance", what happens instead? Does it crash? Does it always return the same distance? Is the distance wildly inaccurate? Or something else? You know what you mean, but we don't.
    – romkey
    Feb 26, 2022 at 4:21
  • 1
    So normally after connection, the output according to the code comes out something like, Distance(cm): 0.00 Distance(inch):0.00 Feb 26, 2022 at 4:38
  • 1
    But when I connect to an Arduino the sensor works perfectly Feb 26, 2022 at 4:39
  • 1
    please add a connection diagram and test code to your post ... your question should stand on its own, without an outside link
    – jsotola
    Feb 26, 2022 at 4:55
  • 1
    I checked it using a multimeter and it gives me only about 3 V. I did the same with the arduino UNO and it gave me about 3.3 V at the 5 V pin. Feb 26, 2022 at 12:52

1 Answer 1



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

MEGA2560 Implementation ESP32 Implmentation

  • Thank you for the reply @starship15. I really appreciate your time for clarifying me with results! Feb 27, 2022 at 11:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.