2

I am extremely new to Arduino and electrical engineering, and the project I am attempting involves a ultrasonic sensor. I am using the HC-SR04 with the Elegoo Mega2560 Arduino board. A few days ago I was able to print out the distance correctly with an Arduino library, but after adding a h-bridge, 2 motors, and 2 9-volt batteries, the sensor didn't seem to work properly. I went back to a simple circuit with only the HC-SR04, and the distance readouts were all over the place. No matter what, the distance stayed between 90 and 120, mainly staying in the 110s. These numbers seemed to have no correlation with the actual distance. I even tried to calculate the distance myself in case there was something wrong with the library.

I have the VCC pin on the HC-SR04 connected to the 5v on the Arduino, the GND pin connected to the GND pin on the Arduino, the Trigger pin connected to pin 50 on the Arduino, and the Echo pin on the Arduino connected to pin 52 on the Arduino. The Arduino is connected to my computer through USB.

Here is my sketch for printing out the distance without a library:

const int triggerPIN = 50;  //these are digital pins on the Arduino board
const int echoPIN = 52;     //I'm sure these pins aren't broken because I never used them before now

long duration;
int distance;

void setup() {
    Serial.begin(9600);

    pinMode (triggerPIN, OUTPUT);
    pinMode (echoPIN, INPUT);

    delay(1000);
}

int getDistance () {                  
    digitalWrite (triggerPIN, LOW);
    delayMicroseconds (2);

    digitalWrite (triggerPIN, HIGH);
    delayMicroseconds (10);
    digitalWrite (triggerPIN, LOW);

    duration = pulseIn (echoPIN, HIGH);
    distance = duration * 0.034 / 2;

    Serial.println (distance);
}

void loop() {
    getDistance ();  //I tried putting the code directly in the loop function with the same result
}

I am afraid that I broke either a part of the Arduino board or the sensor when I was fiddling around with the two batteries. Is it possible to break a HCSR04 sensor by connecting two much voltage to the VCC pin on the sensor? If so, how should I prevent this in the future? I know that the working voltage for a HCSR04 is 5V, does this mean it needs at least 5V or no more than 5V? I wanted to know for sure that it was broken and not an error on my part before I bought a new one. Thanks in advance!

1
  • Unfortunately, it seems the HC-SR04 has no proper datasheet with maximum values. The only documents you can find define the operating voltage of 5V. I personally tested it with less than 5V (IIRC I could make it work at 4V and a bit less).
    – jfpoilpret
    Commented Dec 28, 2017 at 22:02

3 Answers 3

0

The code that you have provided for the distance calculation is fine according to me. There is a high chance that you might have damaged your HC-SR04, Ultrasonic sensor.

As you have asked about how to prevent this from happening in the future, I would suggest that you should use a voltage regulator in the circuit, LM7805, it restricts the output voltage to 5V - just matching your ultrasonic sensor's operating voltage as it works perfectly at 5 Volts.

3
  • Thank you so much Manav. I bought a few more ultrasonic sensors and the sketch works perfectly. I will definitely look into a voltage regulator. 😊
    – cr5519
    Commented Jan 4, 2018 at 15:53
  • Amazing! Glad to be of help.
    – Manav
    Commented Jan 4, 2018 at 15:56
  • No. Don't use a discrete 7805. The Arduino already has a better regulator which is wired up properly, while adding another one is easy to get wrong. And should the ground connection not be there, even for an instant, the regulator will pass the full input voltage. Commented Jun 13, 2018 at 18:56
0

I guess the main problem is a serial printing. It always bother the system mainly the analog reading. It's quiet good for check some sub result while you programing (what the return value from a method etc) but not good to test your project result. In this case the distance measurement. Try a different way, put some led into the circuit and if the distance over 10cm switch on first, if over 20 cm switch on the second and so on. Or write the result on an lcd panel, or 8x8 led.

2
  • I will keep that in mind for future projects, an lcd panel seems like the way to go.
    – cr5519
    Commented Jan 4, 2018 at 15:56
  • No, the problem is not serial printing. But putting in a delay (or non-blocking timer check before taking a new measurement) to allow any old echoes to die out would not be a bad idea. Commented Jun 13, 2018 at 18:57
0

A major functional difference between the code in your question and the usual ping sensor example is that your code repetitively measures as fast as it or the output stream will allow.

In contrast, the normal example has a delay(100) after each measurement cycle. This allows 1/10th of a second time for any longer room echoes to die out, and thus avoid confusing new measurements.

While that might not be your immediate or only problem, it is generally a good idea not to try to take sonar measurements so closely spaced in time that they could overlap.

Should your sketch also need to do other things, instead of a blocking delay() you can check the elapsed time and only take a new sonar measurement once a certain amount of time has passed; in the meantime your program can continue with any other critical operations.

Not the answer you're looking for? Browse other questions tagged or ask your own question.