I am using a HC-SR04 ultrasonic sensor to measure the distance using an Arduino Uno. When I run this code which is worked perfectly in the previous, it only shows just random readings in the serial monitor. the sensor is little old and there is good chance for it to be damaged. So I want to know is it a problem in my code or could it be a problem in the sensor?
#define trig 2
#define echo 4
void setup() {
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
long t = pulseIn(echo,HIGH);
long inches = t/74/2;
long cm = t/29/2;
Serial.print(inches);
Serial.print("inch \t");
Serial.print(cm);
Serial.println("cm");
delay(500);
}
-
You might want to use an oscilloscope to watch the trigger pulse and the received echo at the pins. This would separate hardware issues from software problems. Anyway, since you use a simple sketch that worked for you in the past, it is presumably the hardware. Try another sensor.the busybee– the busybee2025年09月21日 08:49:22 +00:00Commented Sep 21 at 8:49
-
Despite being called ultrasonic, those sensors are perfectly audible. So you don't need anything but ears to check if, at least, the transmitter is working or not.Matt– Matt2025年09月21日 09:40:06 +00:00Commented Sep 21 at 9:40
-
@Matt - perfectly audible to a 20 year old... (-:Jim Mack– Jim Mack2025年09月21日 13:01:07 +00:00Commented Sep 21 at 13:01
-
Are you using batteries? If so, what kind? If a power supply, what is the voltage and maximum current capability?st2000– st20002025年09月21日 13:40:27 +00:00Commented Sep 21 at 13:40
-
@JimMack Nah, my twenty are in the last millenium.Matt– Matt2025年09月21日 17:39:19 +00:00Commented Sep 21 at 17:39
1 Answer 1
Your code is fine. The problem is very likely a bad HC-SR04 module, loose wiring, or unstable power supply. These sensors can degrade, especially if exposed to moisture or dust. Internal transmitter/receiver or timing IC can fail, causing noise or stuck readings.HC-SR04 requires 5V (not 3.3V). If powered from unstable source (like weak USB port), the sensor may give noisy results.Try powering from Arduino 5V pin directly.
Replace with another HC-SR04.
Or use a simple LED test. connect the echo pin to an LED+resistor. Trigger the sensor and see if the LED flickers — if not, the sensor isn’t producing an echo pulse.
Try a known-good library like NewPing (https://docs.arduino.cc/libraries/newping/). If still random hardware problem.
These projects may give you some more insights.
https://projecthub.arduino.cc/Isaac100/getting-started-with-the-hc-sr04-ultrasonic-sensor-7cabe1
https://www.pcbway.com/project/shareproject/DIY_Advanced_Theremino_Sonar_f4408c86.html
-
Yeah, I found out the problem was the module and I replaced it. Now it is working fine. Thank you for your additional information.Y_Jay– Y_Jay2025年09月27日 10:29:37 +00:00Commented 2 days ago
Explore related questions
See similar questions with these tags.