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

NodeMCU代码编译正常但COM5无distance串口输出求助

Troubleshooting Serial Output Issues with NodeMCU & Ultrasonic Sensor

Let’s work through this step by step to figure out why your serial monitor isn’t showing the distance readings. Here are the key areas to check and fix:

1. First, Verify Serial Monitor Basics

  • Double-check that your serial monitor is set to 9600 baud rate (matches Serial.begin(9600) in your code). It’s easy to accidentally leave it on a different rate like 115200.
  • Confirm you’re still connected to COM5—sometimes after uploading, the port can shift slightly, so re-select it in the serial monitor dropdown if needed.
  • Press the RESET button on your NodeMCU right after uploading. The hard reset via RTS might not always trigger the program to start properly, so a manual reset ensures the code begins executing.

2. Fix Potential Blocking in the getDistance() Function

The most likely culprit here is the pulseIn() call. By default, pulseIn() will wait 1 full second for a signal from the echo pin. If your sensor isn’t sending a valid signal (due to wiring issues or power problems), this will freeze your program entirely—meaning the Serial.println(distance) line never gets executed.

Fix this by adding a timeout to pulseIn() (I recommend 200ms, which covers a maximum distance of ~34 meters, more than enough for most use cases):

// Replace the existing pulseIn line with this:
duration = pulseIn(echoPin, HIGH, 200000); // 200000 microseconds = 200ms timeout

Also, add some debug prints inside getDistance() to confirm the function is even running:

void getDistance() {
  Serial.println("Running getDistance()..."); // Add this line
  // Clear trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // trigPin HIGH for 10us (note: your comment said "10ms" which is a typo—should be microseconds)
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads echoPin with timeout
  duration = pulseIn(echoPin, HIGH, 200000);
  Serial.print("Measured duration: ");
  Serial.println(duration); // Add this line to check if we're getting a signal
  // Calculating the distance
  distance = duration * 0.034 / 2;
  // Temporarily comment out Firebase to rule out network blocking
  // Firebase.setFloat("distance", distance);
}

3. Check Hardware Wiring & Power

  • Pin Mapping Confirmation: Your code uses trigPin = 4 (NodeMCU’s D2 pin) and echoPin = 3 (NodeMCU’s D3 pin). Make sure you haven’t swapped these two pins on the sensor.
  • Sensor Power: Most HC-SR04 ultrasonic sensors require 5V power. If you’re powering it from NodeMCU’s 3.3V pin, it might not have enough juice to send a strong signal. Switch to the 5V pin, but add a voltage divider to the echo pin (1kΩ + 2kΩ resistors) to bring the 5V signal down to 3.3V—NodeMCU’s GPIO pins can’t handle 5V input directly, which could damage the pin or cause incorrect readings.
  • Common Ground: Ensure the sensor’s GND pin is connected directly to NodeMCU’s GND. Without a common ground, signal readings will be unstable or non-existent.

4. Rule Out Firebase Network Blocking

If your NodeMCU is having trouble connecting to Firebase, the Firebase.setFloat() call could hang the program. By commenting out that line (as suggested in the modified code above), you can test if the serial output starts working. If it does, you’ll need to debug your Firebase connection:

  • Double-check your FIREBASE_HOST (make sure it doesn’t have a trailing slash at the end—your code has firebaseio.com/, remove the /)
  • Verify your FIREBASE_AUTH token is correct
  • Add debug prints for Firebase connection status:
// After Firebase.begin() in setup()
if (Firebase.failed()) {
  Serial.print("Firebase connection failed: ");
  Serial.println(Firebase.error());
}

Next Steps

Start with the serial monitor basics and the pulseIn() timeout fix—those are the quickest wins. If you start seeing the "Running getDistance()" and "Measured duration" prints, you’ll know the program is executing, and you can narrow down the issue to either the sensor wiring or Firebase.

内容的提问来源于stack exchange,提问作者user11436977

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 23:07:43