Ultrasonic Sensor Output Distance to 0 alternatingly

Hey there. I've been trying to set up the ultrasonic sensor and get it to buzz whenever the distance is less than 23. However, the output given alternatingly 0 and the actual distance. Therefore, I tried creating a conditional statement to fix that. It is working, but it is still a flaw.

I would like to find a way to fix the inaccurate output problem for the 0.

Here is my code and part of the output.

CODE

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04

// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
  Serial.println("with Arduino UNO R3");
  pinMode(8, OUTPUT);
}

void loop() {
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(100);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
    if (distance > 0){
      if (distance < 23){
        tone (8, 294, 250);
        delay (325);
        tone (8, 294, 250);
        delay (325);
      }
    }
}

OUTPUT
image

Thank you.

what does the comment say? what does the code say?

you can't ping the ultrasound sensor at the loop speed. Add a pause.

const byte echoPin = 2;     // attach pin D2 Arduino to pin Echo of HC-SR04
const byte trigPin = 3;     // attach pin D3 Arduino to pin Trig of HC-SR04
const byte buzzerPin = 8;   // attach pin D8 Arduino to signal pin of the buzzer

// defines variables
unsigned long duration;     // variable for the duration of sound wave travel
double distance;            // variable for the distance measurement

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT and ensure it's LOW
  digitalWrite(trigPin, LOW);

  pinMode(echoPin, INPUT);  // Sets the echoPin as an INPUT

  pinMode(buzzerPin, OUTPUT);

  Serial.begin(115200);     // Serial Communication is starting with 115200 of baudrate

  Serial.println(F("Ultrasonic Sensor HC-SR04 Test"));
  Serial.println(F("with Arduino UNO R3"));
}

void loop() {
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculating the distance
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)

  // Displays the distance on the Serial Monitor
  Serial.print(F("Distance: "));
  Serial.print(distance);
  Serial.println(F(" cm"));

  if ((distance > 0) && (distance < 23)) {
    tone (buzzerPin, 294, 250);
    delay (325);
    tone (buzzerPin, 294, 250);
    delay (325);
  }
  
  delay(500); // poll @ 2Hz
}

I see. Thank you.

Does it work now?

Yes. Thank you very much!!

Welcome to the forum, mark the solution and share some love :heart:

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