DEV Community

張旭豐
張旭豐

Posted on

Why Your HC-SR04 Proximity Sensor Keeps Lying to You

Why Your Proximity Sensor Keeps Lying to You

You set up an ultrasonic sensor. You write the code. You test it.

The light turns on when you walk by. Good.

But sometimes it triggers when nothing is there. And sometimes you have to stand completely still for it to notice you. And sometimes it keeps firing after you walk away, like it cannot let go.

This is not a code problem. The sensor is doing exactly what the data says. The data is lying.

What the Datasheet Does Not Tell You

HC-SR04 sends a sound pulse and waits for the echo. The time difference gives you distance. Clean in theory.

In practice, HC-SR04 has a typical accuracy of 3mm, but the readings jump around by ±10-30mm even on a solid target. Hard surfaces return clean echoes. Soft surfaces, curtains, clothing, hands held at angles — these absorb or scatter the sound. The sensor gives you a number. That number does not match reality.

Makers run into this immediately and think: my threshold is wrong. So they adjust the threshold. Still inconsistent. They add smoothing. Still random firing. They add delay between readings. Still occasional false triggers.

The problem is not the threshold. The problem is treating the sensor as precise when it is noisy.

The Perception Problem

In interactive art, you do not need accurate distance. You need believable behavior.

Believable means: the light responds when a person intends to be noticed, and stays quiet when no one is there.

HC-SR04 on a default threshold fires on noise floor. Noise floor exists in any environment — HVAC systems, street traffic, nearby speakers. When the ambient noise is high enough, the threshold crossing looks like a real signal. False positive. The installation has a ghost.

Believable also means: the response happens at a consistent interval after the trigger. HC-SR04 speed-of-sound calculation compounds small timing errors into large distance errors. A 10 microsecond clock error becomes a 3.4mm distance error. In a 2-meter room, that is not much. On a 20cm interactive sculpture, it is the entire operating range.

What You Are Actually Detecting

Most HC-SR04 implementations assume: signal arrival = someone present. This is wrong.

Signal arrival = something returned an echo. That something could be a person. It could also be a hard floor, a nearby wall, a standing wave from your own installation structure.

When you see an LED fire with no one in the room, the code is correct. The sensor is detecting an echo. The echo is real. The interpretation of "someone is there" is wrong.

You need a second signal or a different sensor.

The Fix: Two Directions

Direction one: add a noise floor check. Measure ambient echo levels when the space is empty. Set a minimum echo strength threshold. If the echo is weaker than ambient, ignore it.

Direction two: change the sensor. VL53L0X Time-of-Flight measures distance using infrared laser instead of sound. It is not affected by surface absorption, standing waves, or ambient noise. It costs more and requires I2C, which means more wiring. But the readings are stable in ways HC-SR04 cannot match.

Many makers report switching to VL53L0X and having their installation "just work" after months of fighting HC-SR04.

The Enough Condition

Before you pick a sensor, answer: what is the minimum stability this piece needs?

If it is a hallway light that just needs to turn on when someone walks through, HC-SR04 with noise floor filtering is fine.

If it is an interactive sculpture where false triggers make the piece feel haunted for the wrong reasons, you need the VL53L0X or a camera-based solution.

The sensor you need is determined by the perception requirement, not the datasheet.

Components Used

These parts work for the behaviors described in this article:

FAQ

Q: Can I just use code to fix HC-SR04 noise?
A: You can reduce it. Smoothing (moving average) reduces variance. Median filtering removes spikes. But these techniques cannot distinguish a weak real echo from noise floor. At some point the hardware limits what software can fix.

Q: Is HC-SR04 completely wrong for interactive art?
A: No. It is wrong for pieces where false positives break the perception of aliveness. It works fine for occupancy detection in hallways, basic proximity triggers at fixed ranges, and situations where occasional extra triggers are not noticed.

Q: How do I know if my installation has a false positive problem?
A: Leave the room empty for 30 minutes. Come back. Check if the LED fired at any point while no one was there. If it did, you have false positives. If that bothers you, VL53L0X is the fastest fix.

The sensor is not the problem. The assumption that the datasheet number is the truth is the problem.

Top comments (0)