Ultrasonic Sensor (HC-SR04) functions differently when disconnected from Arduino IDE

Good Day,

I'm baffled at this issue. When my Mega 2560 is connected to my Mac and Arduino IDE 2.0.4, I get a different response from the Ultrasonic sensor.

While connected, it triggers an if statement when the distance is less than 4 cm. The trigger moves the servo to move the sensor 90 degrees left to 90 degrees right. It then determines the furthest distance without an obstacle and displays the result to the serial printer.

When it is not connected and powered by the 2 x 3.2v batteries, it triggers the if statement (registered by a distance less than 4 cm) and begins to move the servo.

Thinking that the surface I am placing the project on may be causing the issue, I tried it in various hard surfaces with no background noise. No matter what I do, it responds the same way when running from battery power. The batteries are fully charged too.

Here's the code and diagram of this project:

#include <Servo.h>
#include <HCSR04.h>

#define TRIGPIN 34
#define ECHOPIN 36

UltraSonicDistanceSensor ultraSonic(34, 36);

Servo head;

int distanceIN = ultraSonic.measureDistanceCm();

// Variables for Ultrasonic distances based on head direction
int max;
String maxName;
int farRight;
String farRightName = "Far Right";
int quarterRight;
String quarterRightName = "Quarter Right";
int front;
String frontName = "Front";
int quarterLeft;
String quarterLeftName = "Quarter Left";
int farLeft;
String farLeftName = "Far Left";

void setup() {

  // SET UP SERVO
  head.attach(52);
  head.write(90);

  Serial.begin(9600);
}

void loop() {

  distanceIN = ultraSonic.measureDistanceCm();
  // Serial.println(distanceIN);

  if (distanceIN < 4) {
    Serial.println("Obstacle in front of me");

    front = distanceIN;  // take initial reading
    Serial.print("Front: ");
    Serial.println(front);
    delay(1000);

    head.write(45);  // Look near right
    quarterRight = ultraSonic.measureDistanceCm();
    Serial.print("Quarter right: ");
    Serial.println(quarterRight);
    delay(0750);

    head.write(0);  // Look far right
    farRight = ultraSonic.measureDistanceCm();
    Serial.print("Far right: ");
    Serial.println(farRight);
    delay(0750);

    head.write(135);  // Look near left
    quarterLeft = ultraSonic.measureDistanceCm();
    Serial.print("Quarter left: ");
    Serial.println(quarterLeft);
    delay(0750);

    head.write(180);  // Look far left
    farLeft = ultraSonic.measureDistanceCm();
    Serial.print("Far left: ");
    Serial.println(farLeft);
    delay(0750);

    head.write(90);  // Look forward

    max = front;
    maxName = frontName;

    if (farRight > max) {
      max = farRight;
      maxName = farRightName;
    }

    if (quarterRight > max) {
      max = quarterRight;
      maxName = quarterRightName;
    }

    if (quarterLeft > max) {
      max = quarterLeft;
      maxName = quarterLeftName;
    }

    if (farLeft > max) {
      max = farLeft;
      maxName = farLeftName;
    }

    Serial.println(farRight);
    Serial.println(quarterRight);
    Serial.println(quarterLeft);
    Serial.println(farLeft);

    if (maxName == "Front") {

      Serial.print("Direction to move is ");
      Serial.print(maxName);
      Serial.print(" with a distance of ");
      Serial.print(max);
      Serial.println(" cm");
    }

    if (maxName == "Far Right") {

      Serial.print("Direction to move is ");
      Serial.print(maxName);
      Serial.print(" with a distance of ");
      Serial.print(max);
      Serial.println(" cm");
    }

    if (maxName == "Quarter Right") {
      Serial.print("Direction to move is ");
      Serial.print(maxName);
      Serial.print(" with a distance of ");
      Serial.print(max);
      Serial.println(" cm");
    }

    if (maxName == "Quarter Left") {
      Serial.print("Direction to move is ");
      Serial.print(maxName);
      Serial.print(" with a distance of ");
      Serial.print(max);
      Serial.println(" cm");
    }

    if (maxName == "Far Left") {
      Serial.print("Direction to move is ");
      Serial.print(maxName);
      Serial.print(" with a distance of ");
      Serial.print(max);
      Serial.println(" cm");
    }
  }
}

I'm grateful for any assistance provided.

Sincerely,

Kevin

6.4V is too low to put into Vin. It should be 7V - 7.5V, minimum.

Powering the servo from to 5V regulator is not a good idea. The servo can (probably is) effecting the 5V regulator output voltage.

You should power the servo directly ftom the battery.

1 Like

Hello,

Thank you for the suggestion but that did not seem to work. It is a small 9g servo and I was using a tutorial example when connecting it. Should sensors be powered separately. I'm new to this.

Do you have any other recommendations?

Thank you.

What did you do to rectify the above?

Hello again!

That seems to work! I connected the servo to a separate 5v supply and increased the VIN to a 12v supply.

Thank you so much for your help. Learned some good things today.

Sincerely,

Kevin

Please consider:

Be careful when using the power plug or Vin. Powering through Vin or the power jack means that the Arduino and all peripherals that are on the 5V rail are powered by the onboard 5V regulator. The on board 5V regulator is not heat sinked so will supply limited current before it overheats and shuts down. The amount of current depends on the voltage input to Vin or the power jack. The higher the voltage the less current can by supplied. I would use a buck converter to drop the 12V to 5V and connect that to the 5V on the Arduino, bypassing the, weak, 5V regulator. Then the rated current of the DC DC converter is available on the 5V line.

If you are powering just a couple of sensors or small LEDs from the 5V, you are fine with Vin or the jack, just be aware the for heavier currents the on board regulator may not hold up.

With 12V into Vin you have less than around 100mA available to supply peripherals connected the 5V regulator output.

Thank you for the additional advise. It will be very useful as I expand my project's power consumption.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.