last day (17 days later) » 

10:43
0
Q: Can't stop the 3 pin active buzzer

BukaidaI was simply trying to program a buzzer with the ultrasonic HC SR04 and 128x64 OLED display. The buzzer is an active buzzer (3 pin) on a small board. The problem is that, once on, it never stops buzzing. I have used the following code which is working fine except the buzzer. #include <SPI.h> #inc...

You wrote, that you connected the buzzer to pin 3, but your code assumes it is on pin 12. Also you wrote, that you have an active buzzer. But an active buzzer can only be turned on or off. You cannot use tone() with it to set a specific frequency (since active means, that the buzzer itself generates its frequency from the DC voltage, that is is getting as power supply). Do you really have an active buzzer?
It is a 3 pin buzzer wit +, - and out pins. It is programmed at pin 12 and connected as + to 5v, - to Gnd and out to pin 12 ( Digital pin 12 of Arduino)
use digitalWrite to turn an active buzzer on and off
substituted all noTone(buzzer) with digitalWrite(buzzer, LOW); and tone(buzzer,329) with digitalWrite(buzzer, HIGH); with no change in situation.
My problem was abruptly closed earlier referring to a completely different solution thread on different hardware by someone. Please let the OP clarify whether the provided solution is adequate for the present case or not before closing the thread. I am using stack exchange for more than 10 years but never faced such situation in any other sites.
add the new information to the question
10:43
The solution thread mentioned was for 2 pin buzzer and the buzzer was not working. Mine is a 3 pin buzzer working all the time. How these two solutions may be similar?
There is some confusion here about the term "active buzzer". An active buzzer is one that makes its own noise. What we have is not an active buzzer. It's an active transducer. It doesn't make its own noise, but it does have the correct piezo drive circuitry to make it work properly. It does require tone() and noTone() to drive it properly.
Does your display tell you the sound should be stopped?
try to disconnect the 5V pin
I was trying to buzz only if the distance is between 2.9 and 3.5 . However it is buzzing for all the cases.
disconnected the 5v and connected the + to pin 12 and - to GND. Now the buzzer stopped completely for all conditions.
write a test sketch only for buzzer to learn how it works
int buzzer = 12; void setup() { pinMode(buzzer,OUTPUT); } void loop() { unsigned char i; while(1) { //output an frequency for(i=0;i<80;i++) { digitalWrite(buzzer,HIGH); delay(1);//wait for 1ms digitalWrite(buzzer,LOW); delay(1);//wait for 1ms } } }
Working perfectly
10:43
so now what we do with this question about buzzer?
It is not stopping
but it is not a problem of the hardware but the logic of the sketch as I understand it
Yes, that is why I have uploaded my entire sketch in OP. I am a beginner in Arduino and not quite familiar with the available /required methods and their parameters for this case.
You could take all the "noTone()" (or equivalent digitalWrite()) commands out of the "if () { ... } " blocks as all of them turn off the buzzer. At least you now know that the buzzer is turned off every loop(). It looks like you are turning the buzzer on for a very short time (20ms which is 1/50 of a second). Is that what you want? It would also help if you properly indented the code to make your decision structure clearer. You are familiar with Python so that should be second nature to you ;-)
You haven't really answered, if you get the expected (and changing!) output on the display and on Serial. Please confirm this, since it is of crucial importance. With I2C the program execution might stop when there is an I2C problem, thus not further controlling the buzzer. Do you get the correct distance measurement every half second on Serial?
10:43
Yes the serial out as well as OLED is measuring the distance ( Although I need to calibrate for accuracy). and they are showing the messages and distances as appropriate.

  last day (17 days later) »