I have a very strange error. My ultrasonic sensor, when both speakers are sticking out of my case, doesn't work at all. It's a black case. It gives very "near" readings. But it does work when outside. Why could this be an issue?
It is likely that the transducer and receiver are being coupled together by the enclosure, so that the signal being received first is the vibration through the enclosure rather than an external object. I would recommend enlarging the holes so they are not in physical contact with the enclosure.
You may also be getting similar problems if the board is secured very rigidly to the base of the enclosure, in that case maybe consider using some o-rings between the mounting hardware and the enclosure. But try step 1 first if the holes are tight fitting.
The sensor consists of a transmitter and a receiver, and it measures the time delay between a transmitted signal and the same signal being received. The further an obstacle which reflects the signal the higher the delay. If you put the sensor in a closed housing then the US signal will reflect back immediately when it bounces on the housing. It's normal that you get a "near" reading.
Always mount transmitter and receiver so that they have a free view towards the environment, So you need holes in the housing.