HC-SR04 bad reading at close range

I am using the HC-SR04 in a automatic door. I want to close the lock powered by a dc motor when the HC-SR04 reads a small distance value (ie door is closed), something along the lines of 2-4 cm. But for whatever reason the HC-SR04 never reads close values properly. The closer I move the box (box used for testing purposes) to the sensor the worse the readings get. At 5cm distance it reads 7 cm. At 2 cm the readings wary from 17 cm to 380 cm. What am i doing wrong here?

Code bellow:

const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

You may not be waiting enough time between range readings. If the echos from a previous ranging have not died down, they can cause an erroneous reading on the next ranging. I usually do not range more than 10 to 20 times per second.

2 cm is the minimum distance that this sensor can even measure, so no wonder you're getting errors there. Place the sensor a bit further away from the door.

Also stray echoes may cause problems indeed, but those usually give a too short distance as result rather than a too long distance (the sensor responds to the first echo to come in). Doors usually do not move super fast, measuring once or twice a second is likely fast enough to keep up with the door movement quite well.

The way these devices operate is that a high intensity pulse train is emitted from the TX transducer, then
after a short delay the RX sensor is activated - this delay prevents erroneous triggering from the TX ring-down travelling through the pcb or by diffraction between transducers. Both transducers are resonant so amplitude
builds up, and then fades away, adding to uncertainty.

The time for detection of the pulse train is somewhat variable depending on the acoustic path and amplitude too,
so accuracy isn't brilliant - the pulse train is nominally 8 cycles, so about 6cm in length, and detection is effectively a tuned circuit which may take several cycles to build up to a trigger - thus the uncertainty is several cm anyway. Also the receiver side circuitry for a cheap sensor is going to be less than ideal in performance.

I'd use a microswitch to detect a closed door.

groundFungus:
You may not be waiting enough time between range readings. If the echos from a previous ranging have not died down, they can cause an erroneous reading on the next ranging. I usually do not range more than 10 to 20 times per second.

I add a 1 second delay at the end of the loop but the results stay the same :frowning:

I agree, you are at the absolute minimum range at 2.0 cm. What you are seeing doesn't surprise me at all. These, while the work OK, are not quite precise measuring systems. For a door I would use a lever actuated micro-switch or a common magnetic reed switch available at any home improvement store. I doubt your code is the problem.

Ron