Piezo Buzzer

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