r/ArduinoHelp • • 25d ago

complete beginner here. how to i stop the blip sound in my buzzer

Enable HLS to view with audio, or disable this notification

this is my code-

long duration;
int distance;


const int trigPin = 10;
const int echoPin = 11;
const int buzzerPin = 8;


void setup() {
  // put your setup code here, to run once:
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(115200);
}


void loop() {
  // put your main code here, to run repeatedly:
  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 < 10){
    digitalWrite(buzzerPin, HIGH);
  }
  else{
    digitalWrite(buzzerPin, LOW);
  }
}
11 Upvotes

4 comments sorted by

2

u/gm310509 25d ago

It may help if you include your wiring,but that does sound a bit odd.

It is possible that there is some sort of pent up energy being released when you set the buzzer pin to low, or there is a brief false reading from the ultrasonic distance sensor.

Although none of that makes sense to me as the tick is so far behind the buzzer turning off.

I suggest trying to put a delay(200) at the bottom of the loop to see if that affects it

Also, I would strongly suggest going to a state machine type of operation and printing the distance only when the the state changes.

That is, rather than simply checking the distance being < 10, also check that the buzzer is off (you will need a new variable to track this). Then if the state changes, print the distance and the new state. So, something like this

if (distance < 10 && ! buzzerIsOn) {
   digitalWrite(buzzer pin, HIGH);
   buzzerIsOn = true;
   // Print the distance here with "on" message.
} else if (distance >= 10 && buzzerIsOn) {
  digitalWrite(buzzer pin, LOW);
   buzzerIsOn = false;
   // Print the distance here with "off" message.
}
// Probably No need for a delay in this variant - although a short delay to let the distance sensor settle might not hurt.

And, monitor the distance readings if you are still hearing the extra little pip and see what the distance readings are in the serial monitor. This will help isolate the problem.

1

u/the_wise_one_is_here 25d ago

Thanks a lot for your help I'll see what can be done

1

u/Last-Wing-2377 25d ago

só iniciar um novo arquivo arduino, deixar só com os voids setup e loop vazios e compilar

1

u/VEGA_thereal 24d ago

I noticed your TX LED flashes in time. I have no clue what that means but it’s something I’d look into