Ultrasonic sensor disconnected but still reading


please help me with this - I have ultrasonic sensor module HSR-04, connected to Arduino Mega 2560 like this:

+5V in digital section GND in digital section Trigger pin 34 in digital section Echo pin 35 in digital section

The sensor is connected via 50cm ethernet cable

Everything is working OK untill I disconnect the sensor, but leave the 50cm cable connected to pins on Arduino. There is still reading - it varies from a few centimeters to 1,8 meter, up and down irregular.

Problem is, that I need it to read -999 or something like that, when it is disconnected (or broken). It reads -999 only if I disconnect the echo line from ETH cable from pin 35 on Arduino completely.

I have tried to put 1 or 2 or 3 4k7 resistors in series between pin 35 and the cable, but it still gets some reading :-(

I’m sorry I can’t personally comment on the module, BUT decoupling capacitors can be used for filtering noise - would a more experienced member be able to tell us whether this would be helpful?

Have a search for "Floating Input", there's plenty of info on the forum.

Please explain how you can read 999 from a digital input pin?

Please explain how you can read 999 from a digital input pin?

It is impossible. you can read only logical "0" or logical "1".

[u]Read a "floating input" is a nonsense.[/u] Results are entirely depending of ambient electrical noise.

If library receives invalid data, it can only give invalid outputs.

DrDiettrich: Please explain how you can read 999 from a digital input pin?

It is not 999 but -999 what library returns when senso is not connected and no other interference is ruining signal.

But my problem is solved - I put 4K7 resistor between Echo pin 35 on Arduino and GND in digital section on Arduino. All the readings are now OK - if sensor is connected, it reads accurate measurements and if it is disconnected, it returns -999. This is good solution, because in the sketch it is traceable as sensor failure :-)

r-man: But my problem is solved

You've found the simplest solution - a pull-down resistor. Good luck with your project.