0

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);
}
asked Sep 21 at 8:18
9
  • 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. Commented 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. Commented Sep 21 at 9:40
  • @Matt - perfectly audible to a 20 year old... (-: Commented 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? Commented Sep 21 at 13:40
  • @JimMack Nah, my twenty are in the last millenium. Commented Sep 21 at 17:39

1 Answer 1

0

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

answered Sep 22 at 16:09
1
  • Yeah, I found out the problem was the module and I replaced it. Now it is working fine. Thank you for your additional information. Commented 2 days ago

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.