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);
}
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 !