ESP8266 and HC-04

I have a Nodemcu ESP8266 module and a HC-04 Ultrasonic sensor. Previously, when I connected them and ran my code, it worked well with the VCC on the ultrasonic connected to the 3V of the ESP.
However lately it is not working anymore. Below is attached my code and the output. I suspect that it is the power itself that is an issue, because when connected to an Arduino Uno, VCC to 5V, the sensor works well.
Can somebody help me please?

const int trigPin = 16;
const int echoPin = 5;

//define sound velocity in cm/uS
#define SOUND_VELOCITY 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() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate the distance
  distanceCm = duration * SOUND_VELOCITY/2;
  
  // Convert to inches
  distanceInch = distanceCm * CM_TO_INCH;
  
  // Prints the distance on the Serial Monitor
  Serial.print("Distance (cm): ");
  Serial.println(distanceCm);
  Serial.print("Distance (inch): ");
  Serial.println(distanceInch);
  
  delay(1000);
}

Never heard of one of those units running well on anything less than a solid 5V.

What makes you think this is an Arduino installation issue?

Because on the ESP8266 3v it did not work, but on the uno 5v it worked. However previously it did work on the ESP8266 3v, and there is lots of projects online where it was powered using the 3v itself, or sometimes the Vin pin.
Any solution on how i can supply the sensor 5v then while keeping the I/O on the ESP itself?

You may be able to trigger the device with a 3.3V output, but you'll need a simple voltage divider for the echo pin, to bring the 5V down to 3.3V.

It still doesn't make it an Arduino installation issue.

Could you explain me how to proceed with this solution please?

I already have:

@yanishrao

Your topic was moved to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

It will help you get the best out of the forum in the future.

Thank you

Maybe the devboard's 3V regulator p00ped.

@anon73444976 Actually there is two types of ultrasonic sensors.
HC-SR04 uses 5v and there is another one HC-SR04P which can use 3.3v, so it is possible that i was using a HC-SR04P without knowing it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.