Piezo Buzzer

I just bought this... http://www.sparkfun.com/datasheets/Components/CEM-1203.pdf... I am feeding it 12v's with a 470ohm resistor. It makes a tiny beeping noise when it switches to either high or low, but when it's on high it doesn't make a noise. What am I doing wrong? (when it does sound when switching between high and low its so soft I can barley hear it.)

Can you give us the code?

Sure..

int tresPin = 3;
int dosPin = 2;
int unoPin = 5;
int buzzPin = 7;
//int msPin = 5;

void setup()               
{
  // begin the serial communication
  Serial.begin(9600);
  
  pinMode(tresPin, OUTPUT);     
  pinMode(dosPin, OUTPUT); 
  pinMode(unoPin, OUTPUT); 
  pinMode(buzzPin, OUTPUT); 
  //pinMode(msPin, INPUT);
}

void loop() // continuous loop
{
  Serial.println("3");
  digitalWrite(tresPin, HIGH);  
  delay(1500); 
  digitalWrite(tresPin, LOW);
  
  Serial.println("2");
  digitalWrite(dosPin, HIGH);  
  delay(1500); 
  digitalWrite(dosPin, LOW);
  
  Serial.println("1");
  digitalWrite(unoPin, HIGH);  
  delay(1500); 
  digitalWrite(unoPin, LOW);
  
  Serial.println("Go!"); 
  digitalWrite(tresPin, HIGH);  
  digitalWrite(dosPin, HIGH);  
  digitalWrite(unoPin, HIGH);  
  digitalWrite(buzzPin, HIGH);
  delay(3000); 
  
  digitalWrite(tresPin, LOW);
  digitalWrite(dosPin, LOW);
  digitalWrite(unoPin, LOW);
  digitalWrite(buzzPin, LOW);

}

The power to run the buzzer comes from 12v wall plug, but I am using a transistor as a switch so that's what triggers the buzzer with buzzPin.

The code you've written there will (as far as I can tell) make a few clicks every few seconds. To make a plain piezo sounder beep, you must feed it with a square wave of a few hundred cycles per second. The data sheet for your buzzer recommends 2048Hz, in fact. And it states that the buzzer only needs 3 to 5 Volts to work properly. That data sheet also mentions that the device is not a high-impedance piezo sounder but a low-impedance magnetic buzzer. It's possible that 12V is too much for it!

Bouzy,

Looks like you are trying to make a racing light... I assume the other pins you are sending HIGH are turning on some LEDs.

There actually is no problem with your code if you had the right buzzer. Some buzzers "self-oscillate" at a particular frequency, and some need to be stimulated at a frequency to produce a sound. It should be obvious by now that you have the kind you need to stimulate :slight_smile:

Based on your buzzer's specs, I would change your resistor from 470ohms to 100ohms. Make sure it's at least a 1W resistor. 2W would be better, but you are only turning it on for 3 seconds so it shouldn't get too hot in that period of time. This will drop the 12V down to 3.5V for your buzzer, since it has a coil resistance of about 42ohms. (12V * 42 / (100+42)) is about 3.5V.

Given that you are driving it correctly now, you need to "stimulate" it.

If you replace this code:

digitalWrite(buzzPin, HIGH);
delay(3000);

With this code:

for (long i = 0; i < 2048 * 3; i++ ) 
{
    // 1 / 2048Hz = 488uS, or 244uS high and 244uS low to create 50% duty cycle
    digitalWrite(buzzPin, HIGH);
    delayMicroseconds(244);
    digitalWrite(buzzPin, LOW);
    delayMicroseconds(244);
}

You should hear a 2048Hz tone on your buzzer for 3 seconds.

You can also put this code at the end of your loop() routine to keep it from looping over and over:

while(true); //kill loop, while true do nothing

Here's a couple links to some code that may help:

Hope that helps! I had fun testing the code so it's all good on my end :wink:

-Brett

is there a way to make this buzzer quieter? would you use a large resistor?

would you use a large resistor?

No:- a potential divider. This will allow you to cut down the voltage to what ever you want.

is there a way to make this buzzer quieter? would you use a large resistor?

Yes, I would put a potentiometer (maybe a 5k - 20k ohm or larger) in series with the piezo and increase the resistance until the desired sound output is reached. Then remove the pot from the circuit and measure it's resistance, and replace it with an approximately close value discrete resistor.

The mounting scheme of the piezo will also have an effect on the sound amplitude. If the piezo is screwed down to a plate... it will vibrate the entire surface and will gain amplification. Stick it to some foam tape and it will be quieter. Just something to be aware of.

The piezo can be thought of as a capacitor. If you are driving your output high AND low, then a series resistor and a 50% duty cycle output charges and discharges the "capacitor" evenly. Increase the single resistor and you effectively decrease the current to the piezo in the ON and OFF cycles.

If you use a resistor divider, you are putting one resistor in series with the piezo from the driver pin, and one in parallel with the piezo to ground. This gives you a reduced voltage to the piezo in the ON state, and an reduced impedance during the OFF state. The "drive" is a bit disproportional and may "sound" less amplified on the ON state... but is going to produce a sharper OFF state, albeit one with less voltage. The impedance of the piezo matters here as well... and will react with your resistor divider values, to the point where your Vin*R/(R+R) may not produce exactly half of Vin, but something less.

A pot works well in my opinion, but play with both ways and see what sounds best to you.

A pot and resistive divider will work in exactly the same way. Any worry about the speed of the rising or falling edge is only going to show up in the harmonic content of the buzzer and as the fundamental frequency is quite high I would be surprised if you will here any difference.

A pot and resistive divider will work in exactly the same way. Any worry about the speed of the rising or falling edge is only going to show up in the harmonic content of the buzzer and as the fundamental frequency is quite high I would be surprised if you will here any difference.

What I mean is use the pot as an adjustable resistor (rheostat), use the wiper and one leg and let the other leg dangle. Sorry, I figured that was obvious given that he wanted to just "use a large resistor". I should have clarified.

I would imagine that to reduce the perceived volume of the piezo, you are going to have to increase the series resistance large enough to actually change the square wave to more of a reduced amplitude sawtooth waveform... so harmonics are not a factor. This is the least complicated way of attenuating the piezo output. The resistor divider way works as well, but is unbalanced.

I was too lazy to draw this, so I searched around and found this reference:

The author also suggests replacing the series resistor with a series POT for volume control.
http://hades.mech.northwestern.edu/index.php/Driving_a_piezo_speaker_with_a_PIC