My HC-SR04 had been working completely fine but since yesterday I've been getting gibberish output from my module. I had been using the following code to run it and obtain distance in cms.
int trig=2;
int echo=4;
int distance;
int duration;
void setup() {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
duration=pulseIn(echo,HIGH);
distance=(duration*.034)/2;
Serial.print("distance : " +distance);
delay (2000);
}
Which no longer works. First I thought the pins I had been using for input/output on my arduino had failed hence I connected different sensors to the same pins and they worked fine. So is there something wrong with my board or has the sensor gone bad.