Ultrasonic Sensor Code

Hello,
I am trying to make a code for an ultrasonic sensor that gives us distance and if the distance is less than 50 the buzzer beeps but if the distance is continuously less than 50 then after 10sec the buzzer gets stop for further.
Please help me to make this code.

Can you please give more details about what hardware you have.

Also, please realize the expectation is for us to help guide you, not do it for you...

Please let us know if you have made any attempts, or where you may be lost.

-JB

Cross posted in Project Guidance.

Hello,
I am trying to make a code for an ultrasonic sensor that gives us distance and if the distance is less than 50 the buzzer bips but if the distance is continuously less than 50 then after 10sec the buzzer gets stop for further.
Please help me to make this code.

Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

The if structure reference.

Give it a go and if you get stuck, post the code, tell us what the code is doing and how that differs from what you want.

Read the forum guidelines.

I've merged your cross-posts @JBornstein90 .

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide you will find at the top of every forum category. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

@pert These aren't my cross posts :wink:

Always appreciate upkeep!

1 Like

Yes, I am using esp32.

my code

// defines pins numbers
const int trigPin = 18;
const int echoPin = 19;
int LED_BUILTIN = 2;
// defines variables
long duration;
int distance;

void setup() {

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED_BUILTIN, OUTPUT);

Serial.begin(9600);
}

void loop() {

// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

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

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

Serial.print("Distance: ");
Serial.println(distance);

if (distance < 50)
{
digitalWrite(LED_BUILTIN, HIGH);
delay(10);
}
else
{
digitalWrite(LED_BUILTIN, LOW);
delay(10);

}

}

Thank you

And . . ?

(I'd slow the ping rate down to around 20 to 30Hz, maximum)

Please remember to use code tags when posting code

I apologize for the incorrect mention. I meant to mention @thakur_85 in that reply.

2 Likes

Devide and conquer. Lets see a sketch that just does the distance and shows the result on the Serial Monitor. Then we can work on the timing thing.

-jim lee

  static unsigned long LastTime;
  if (distance > 0 && distance < 50)
  {
    if (millis() - LastTime < 10000)
    {
      // Turn on the buzzer if close and less than 10 seconds
      digitalWrite(LED_BUILTIN, HIGH);
    }
    else
    {
      // Turn off the buzzer if close but more than 10 seconds
      digitalWrite(LED_BUILTIN, LOW);
    }
  }
  else
  {
    //Turn off the buzzer if not close
    digitalWrite(LED_BUILTIN, LOW);
    LastTime = millis();
  }
1 Like

First step is to get the ultrasonic sensor working. There are many libraries with examples that will get you started.

Its probably the best to find one that will print the distance to the Arduino IDE.

If that is too big a step for you then get the Arduino IDE installed and download the example "Blink without delay". It will simply blink the LED on your Arduino board. What makes this meaningful is you know you have the IDE working and can download a program and have it work.

more than 50, less than 50

50 Whats? Microns, MM, CM, Inches, FEET, Meters, Kilometers?

thank you so much sir it's working

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