Ultrasonic HY-SRF05 doesn't work?

Hi! I'm trying to work with HY-SRF05 and NodeMcu esp8266. Here is my code:

#include <Arduino.h>

const unsigned int TRIG_PIN=D6;
const unsigned int ECHO_PIN=D5;
const unsigned int BAUD_RATE=115200;

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  Serial.begin(BAUD_RATE);
}

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

 const unsigned long duration= pulseIn(ECHO_PIN, HIGH);
 int distance= duration/29/2;
 if(duration==0){
   Serial.println("Warning: no pulse from sensor");
   } 
  else{
      Serial.print("distance to nearest object:");
      Serial.println(distance);
      Serial.println(" cm");
  }
 delay(1000);
 }

ask1

My problem is that on serial monitor, the only value i receive is 0. Can anyone please tell me what did i do wrong? Thank you !

is this a 5V device ?

Thank you ! i've checked and found out that it only works with 5v. Can you suggest what can i do to fix this?

if you power through USB, the Vin pin of your NodeMcu esp8266 should be close to 5V and should be suitable for powering the sensor (it does not draw too much current - possibly 40 mA). Double check that voltage is around 5V before plugging anything

As you power with 5V, the echo pin will send 5V back and your NodeMcu esp8266 is not 5V compatible so you need to adapt the voltage down (a couple resistor or a voltage adapter) so that you bring the 5V back to 3.3V

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