node_hcsr04 icon indicating copy to clipboard operation
node_hcsr04 copied to clipboard

Incorrect reading?

Open eriksjodin opened this issue 10 years ago • 0 comments

I used your code but I only get incorrect readings from the HC-SR04 sensor.

I did some debug modifications in the function self.measure() see below:

function self.measure() gpio.trig(self.echo, "up", self.echo_cb) gpio.write(self.trig, gpio.HIGH) tmr.delay(100) gpio.write(self.trig, gpio.LOW) tmr.delay(100000) if (self.time_end - self.time_start) < 0 then return -1 end -- Modified for debug purpose print ("-------------------") print (self.time_start) print (self.time_end) print (self.time_end - self.time_start) print ("-------------------") return (self.time_end - self.time_start) / 58

This is the debug printout of the self.time_start and _end

1079788923 1079789402

479


1079897355 1079897818

463


1080035710 1080036173

463


1080174067 1080174530

463

7

1080788919 1080789402

483


1080897355 1080897822

467


1081035711 1081036177

466


1081174065 1081174532

467

8

Do you have an explination to why the time between trigger and echo is constant at around 460-470 us even if there is nothing infront of the sensor.

Could you also explain why you devide the delta time by 5800, I am guessing it has something to do with the speed of sound in air. 343 m/s = 34300 cm/s

What am i doing wrong? ;) Thanx for your support

eriksjodin avatar Jan 07 '16 22:01 eriksjodin