You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HC-SR04传感器静止时距离值异常跳变,求助排查故障或代码问题

Troubleshooting HC-SR04 Wild Distance Jumps

Hey there! Let's break down why your HC-SR04 is spitting out those huge, erratic distance values when it's stationary. We'll cover both code adjustments and hardware checks to get your sensor behaving reliably.

First, Let's Understand the Issue

Looking at your output, you've got valid, reasonable readings (like 25.93 cm) mixed with massive outliers (like 3540.65 cm). Those big numbers happen because the pulseIn() function isn't getting a valid echo signal from the sensor, so it hits its default 1-second timeout and returns a huge duration value. When you calculate distance with that, you end up with those unrealistic results.

Code Fixes to Filter Outliers

Your base code is mostly correct, but we need two key improvements to eliminate those jumps:

  1. Set a reasonable timeout for pulseIn() (since HC-SR04's max range is ~4 meters, we don't need to wait 1 second for a response)
  2. Add logic to ignore invalid/extreme readings

Here's the adjusted code:

// HC-SR04 adjusted sketch
const int trigPin = 9;
const int echoPin = 10;
float duration, distance;
// Max timeout based on HC-SR04's 4m max range (rounded up to 30000 microseconds)
const long PULSE_TIMEOUT = 30000;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Get pulse duration with a strict timeout matching the sensor's range
  duration = pulseIn(echoPin, HIGH, PULSE_TIMEOUT);

  // Only calculate and print if we got a valid echo signal
  if (duration > 0) {
    distance = (duration * 0.0343) / 2;
    // Filter out readings beyond the sensor's physical max range
    if (distance <= 400) { // 400cm = 4m, adjust if your sensor has a different rated range
      Serial.print("Distance: ");
      Serial.println(distance);
    } else {
      Serial.println("Distance: Out of range");
    }
  } else {
    Serial.println("Distance: No echo received");
  }

  delay(100);
}

Key Changes Explained:

  • PULSE_TIMEOUT: Sets a 30ms limit for waiting for an echo. This aligns with the sensor's physical capabilities, so we don't waste time waiting for a signal that'll never come.
  • Validity Checks: We only process readings when duration > 0 (meaning we got a valid echo) and filter out values over 400cm (the sensor's typical max range). This immediately eliminates those massive outliers.

Hardware Checks to Rule Out Faults

If the code fixes don't resolve the jumps, let's verify the hardware side:

  • Power Supply: HC-SR04 needs a stable 5V supply. If you're powering it from the Arduino's 5V pin, try using a USB wall adapter instead of a computer USB port—weak or inconsistent power often causes unstable sensor behavior.
  • Wiring: Double-check that trigPin (9) and echoPin (10) are wired firmly and correctly. Loose or frayed wires can cause intermittent signal drops.
  • Interference: Keep the sensor away from other ultrasonic devices, motors, or Wi-Fi routers. Electromagnetic noise can disrupt the echo signal and lead to false readings.
  • Sensor Placement: Make sure the sensor's probes are unobstructed, and the target surface is flat and reflective. Soft surfaces like curtains absorb sound waves, leading to missed echoes and erratic data.
  • Sensor Quality: If all else fails, test the sensor with a known-good setup. Cheap HC-SR04 modules sometimes have manufacturing defects that cause persistent instability.

Final Notes

Start with the code adjustments first—those are quick to test and will likely fix most of your outlier issues. If you still see jumps after that, work through the hardware checks one by one to narrow down the problem.

内容的提问来源于stack exchange,提问作者Artūras Maziliauskas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:11:48