Using ultrasonic sensor to detect an object with a buzzer

Updated
Hi I was having with my coding for some reason duration that print out keep decreasing as I move the object away from the ultrasonic sensor

// 8/8/2021 aka ytee
//Please cite the source if you plan to duplicate it
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,7,6,5,4);//initalize the library with the number of the interface pins
int buzzer=11; //assign pin for buzzer
int trigPin= 8; //assign Trig Pin
int echoPin=10; //assign Echo Pin
long duration;
long distance;
void setup()
{
  lcd.begin(16,2);//set up the lCD number of columns and rows
  lcd.print("Dai Nguyen");//print my name to first column and row of lcd display
  pinMode(buzzer,OUTPUT);//assign output for buzzer
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
}
void loop()
{
  int maxDis= 100;
  long pulseTimeout= (maxDis*1000000)/(0.5*343*100);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration=pulseIn(echoPin,HIGH,pulseTimeout);
  distance = 0.5*(pulseTimeout*343*100)*(1/1000000); //distance in cm
  if (distance>0 && distance <=20)
  {
  lcd.setCursor(6,1); //set cursor the the 6th column of second row
  lcd.print("Dis cm:");//print out the distance
  lcd.print(distance);
  lcd.setCursor(0,1);
  lcd.print(" s:");
  lcd.print(duration);
  tone(buzzer,261);//buzz at the frequency relative to C note in Hz
  delay(500);
  }
  else if (distance>20 && distance <=60)
  {
    lcd.setCursor(6,1);
    lcd.print("Dis cm:");//print out the distance
    lcd.print(distance);
    lcd.setCursor(0,1);
    lcd.print(" s:");
    lcd.print(duration);
    tone(buzzer,293);//buzz at the frequency relative to D note in Hz
    delay(500);
  }
  else if (distance>60 && distance <=80)
  {
    lcd.setCursor(6,1);
    lcd.print("Dis cm:");//print out the distance
    lcd.print(distance);
    lcd.setCursor(0,1);
    lcd.print(" s:");
    lcd.print(duration);
    tone(buzzer,329);//buzz at the frequency relative to E note in Hz
    delay(500);
  }
  else if (distance > 80 && distance <=100)
  {
    lcd.setCursor(6,1);
    lcd.print("Dis cm:");//print out the distance
    lcd.print(distance);
    lcd.setCursor(0,1);
    lcd.print(" s:");
    lcd.print(duration);
    tone(buzzer,349);//buzz at the frequency relative to F note in Hz
    delay(500);
  }
  else
  {
    lcd.setCursor(6,1);
    lcd.print("Dis cm:");//print out the distance
     lcd.print(distance);
    lcd.setCursor(0,1);
    lcd.print(" s:");
    lcd.print(duration);
    tone(buzzer,392);//buzz at the frequency relative to G note in Hz
    delay(500);
  }
}
void loop()
int maxDis = 100; //maximum measuring distance is 100cm;

You have no opening brace on the code in the loop() function

maxDis1000000
What is this supposed to do ?

i just use maxdis x 1000000 but for some reason it won't show the *

@UKHeliBob for some reason it won't show the entire number on the lcd screen

No version of the LiquidCrystal library supports the println function. That is the cause of some of the weird characters. Since newline does not work you must use setCursor to conrol the cursor line.

Read the forum guidelines to see how to post code.

Post your test code in code tags. Use the IDE autoformat tool (ctrl-t or Tools, Auto Format) to indent the code for readability before posting code.

1 Like

Please edit your post to add code tags.

thank you so much

Shouldn't 'distance' be based on 'duration' and not on 'pulseTimeout'? Try:
distance = duration / 58; //distance in cm

For slightly more accuracy, try:

// 343 meters per second == 0.002915452 seconds per meter
const float MicrosecondsPerCM = 29.15452;
const float MicrosecondsPerRoundTripCM = MicrosecondsPerCM * 2;  // ~58.3

  distance = duration / MicrosecondsPerRoundTripCM; //distance in cm

(1/1000000)
Done in 'long int' this is zero.

1 Like

I want to ask what is the pulseTimout and how we can calculate it?

You SET pulse timeout when you call pulseln() as one of the parameters. RTFM!

The maximum range of the HC-SR04 is about 4 or 5 meters (400-500 cm). Multiply by 58.3 to get microseconds, so 23320 to 29150 microseconds. I just round it up to 30000.

Your version comes out to 5830 microseconds. That sets the maximum range at 100 cm (1 meter).

1 Like

Thank you so much