Buzzer Trouble...

So… I’m obviously doing something wrong here. :stuck_out_tongue: I’m trying to produce sound from this buzzer that I bought from Radio Shack (part number 273-0053; it’s a mini buzzer, 1.5-3.0VDC, 15mA. It’s resonant frequency is 300 - 500Hz) using my LilyPad Arduino as a driver. The LEDs light up just fine; it’s the buzzer that isn’t working. I’m getting nada sound out of it. :frowning:

I’m powering the circuit with 3.0V (two AA batteries). The buzzer’s positive lead is attached to the #3 pin of the Lilypad, and the negative lead is attached to a ground bus. As for the code, I’ve written the following set of functions, but the ones in particular that seem to be at fault are the beep() and scale() functions, as near as I can tell. Can anyone tell me what I’m doing wrong?

/*
  Blink 7
  Turns on 7 LEDs on for one second, then off for one second, repeatedly.
 
  Modified to include a Buzzer sequence at the beginning of the LED blinks.
 */
int LED1[] = {0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0};
int speakerPin = 3;

void setup() {                
  // initialize the digital pins as output/input.
  int i;
  for (i = 0; i < 20; i++){
    if (LED1[i] == 1){
   pinMode(LED1[i], OUTPUT);
 } 
else{
  pinMode(LED1[i], INPUT);
} 
  }
    
}

void loop() {
  int i;
  for (i = 0; i < 20; i++){
    if (LED1[i] == 1){
    digitalWrite(i + 1, HIGH);
    }
  }
  delay(1000);
    for (i = 0; i < 20; i++){
    if (LED1[i] == 1){
    digitalWrite(i + 1, LOW);
    }
  }
  scale();
  delay(1000);
}

void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)     // the sound producing function
{ 	 
          int x; 	 
          long delayAmount = (long)(1000000/frequencyInHertz);
          long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
          for (x=0;x<loopTime;x++) 	 
          { 	 
              digitalWrite(speakerPin,HIGH);
              delayMicroseconds(delayAmount);
              digitalWrite(speakerPin,LOW);
              delayMicroseconds(delayAmount);
          } 	 
} 	 
  	 
void scale ()
{ 	  // My buzzer has a resonant frequency of 300 - 500 Hz, so I am limiting my sequence to these notes.
          beep(speakerPin,330,500); 	//E3
          beep(speakerPin,349,500); 	//F3
          beep(speakerPin,392,500); 	//G3
          beep(speakerPin,440,500); 	//A3
          beep(speakerPin,494,500); 	//B3
}

Is it a problem with my code, or am I simply using the wrong type of component for this? Since I didn’t buy the special Lilypad buzzer from SparkFun, am I facing divine retribution from the Arduino gods? xD

It's resonant frequency is 300 - 500Hz)

I think you are misunderstanding this. It has only one resonant frequency and that can be anywhere between 300 and 500Hz, varying from device to device. You might get some sound outside of it's frequency but I have never used that device so I don't know. I would use the tone library instead of trying to roll your own.

Grumpy_Mike:

It's resonant frequency is 300 - 500Hz)

I think you are misunderstanding this. It has only one resonant frequency and that can be anywhere between 300 and 500Hz, varying from device to device. You might get some sound outside of it's frequency but I have never used that device so I don't know. I would use the tone library instead of trying to roll your own.

I think I see what you're saying... This tone library... I should use its functions to get my buzzer to make sounds?

I was trying to use the one that Leah Buechley wrote, because I didn't know there was already a header file out there with the capability I wanted. :) So let me make sure I understand this correctly, though. The resonant frequency of MY buzzer is somewhere between 300 and 500Hz, and it will only make sound at that frequency? Is it because mine isn't a piezo buzzer? I think I read something somewhere that piezo buzzers are different, but I don't know if that factors into this or not.

Thank you so much for your help! I am learning, slowly but surely! :)

I bought from Radio Shack (part number 273-0053;

Have you got a link to that part a search of the Radio Shack web site pulls up nothing for this code.

Grumpy_Mike:

I bought from Radio Shack (part number 273-0053;

Have you got a link to that part a search of the Radio Shack web site pulls up nothing for this code.

It seems I was incorrect; apparently it is a piezo. Here is the buzzer, though the part number is missing a 0 in it. :x link

There are two types of Piezo buzzers, one you just apply a DC voltage to and it makes a sound, the other you have to feed it an AC signal to make a sound. Sadly but unsurprisingly the web site gives very little information and I can't tell what type it is. However it does say the voltage is 1.5-3VDC, so if you are connected to an arduino you are giving it too much voltage. Try connecting it directly to a battery and see if it makes a sound. The fact that is says DC suggests that doesn't need feeding with a signal just a voltage.

Grumpy_Mike: There are two types of Piezo buzzers, one you just apply a DC voltage to and it makes a sound, the other you have to feed it an AC signal to make a sound. Sadly but unsurprisingly the web site gives very little information and I can't tell what type it is. However it does say the voltage is 1.5-3VDC, so if you are connected to an arduino you are giving it too much voltage. Try connecting it directly to a battery and see if it makes a sound. The fact that is says DC suggests that doesn't need feeding with a signal just a voltage.

You may well be right about the voltage, although I'm powering the Arduino (and the rest of my circuit) with two AA batteries, so it should be (nominally) 3.0 volts already. Is it something that could be fixed with a resistor? Without knowing the voltage drop of my buzzer (it doesn't give any more information on the package either, unfortunately), can I even know which resistor I could use?

I'll try it out with a battery (or two) when I get home and see what happens. Just in case, though, I went ahead and ordered the LilyPad buzzer anyway. It's smaller and probably won't give me much sound, but at least I'll know it works with the code I've written (presumably). I thought maybe I could get off cheaper this way, but it looks like I thought wrong, lol.

Urgh…

Okay, so I noticed a problem in my code that was throwing everything off. -_- Apparently, I wasn’t setting the pins to output/input properly. Check this part out:

void setup() {                
  // initialize the digital pins as output/input.
  int i;
  for (i = 0; i < 20; i++){
    if (LED1[i] == 1){
   pinMode(LED1[i], OUTPUT);
 } 
else{
  pinMode(LED1[i], INPUT);
} 
  }
    
}

Instead of using the index value itself, I used the value in my array instead (0 if input, 1 if output). That meant that I was setting and resetting pin 1 to Output over and over again, and pin 0 (which doesn’t exist) to input over and over again. Argh.

I’ve changed my code, and now I’m setting the pins properly. My buzzer is buzzing. :slight_smile: Unfortunately, when I do anything more than feed it voltage, it’s extremely faint. I suppose I’ll need the lilypad buzzer after all if I want to use discrete tones, lol. Anyway, thanks for all your help! I appreciate it!

Oh, and as a bonus, when I fixed this part of my code, all of the sudden my LEDs were lighting up really brightly. xD I just figured I had super weak LEDs up until now, lol.