Buzzer Erratic Beeping

Hello,
I’m making an IoT button with Particle. When pushed, it’s basically supposed to do a little light effect and then beep once. It does the proper lighting effect, but the buzzer beeps in erratic patterns. I unplugged the neopixel and tried again without the neopixel, but it did the same thing. I know the buzzer is not faulty because I tested it with an Arduino Uno. The buzzer is connected to pin D0. Below is the part of the code for the buzzer. Does anyone know why this is happening? Thanks.

what it’s doing

void setup() { 
   pinMode(D0, OUTPUT);
}
void loop() {
   tone(D0, 1000, 500);
}

Can you specify what is “erratic”? I watched the video but it sounded ok for me.

Or rather, I couldn’t “see” what’s the issue. :slight_smile:

1 Like

Thanks for responding! I want the buzzer to beep once but it beeps in weird patterns.

But your code shows tone() in loop() which runs continuously?

1 Like

My bad! I didn’t put my whole program here because its quite long, but here is the code that controls the buzzer and lights. Sorry!

buttonState = digitalRead(D6); // is the button pressed?
if (buttonState == LOW) { // yes  -  button state is low (inverted)
  Spark.publish("Pushed", buttonState); // publish button state
  tone(D0, 1000, 500); //beep
  colorWipe(strip.Color(255, 0 , 0), 50); //wipe all pixels green

} else { // no  -  button state is high (inverted)
  colorWipe(strip.Color(0, 0, 0), 10); // wipe all pixels off
}
}

One way is to set a flag like:

boolean beep = true;

buttonState = digitalRead(D6); // is the button pressed?
if (buttonState == LOW) { // yes  -  button state is low (inverted)
  Spark.publish("Pushed", buttonState); // publish button state
  if(beep) tone(D0, 1000, 500); //beep
  beep = false;
  colorWipe(strip.Color(255, 0 , 0), 50); //wipe all pixels green
  }
 else { // no  -  button state is high (inverted)
  colorWipe(strip.Color(0, 0, 0), 10); // wipe all pixels off
  beep = true;
  }
}

3 Likes

Also are you using INPUT_PULLUP with your button on D6?
Without that (or an external pull-up resistor), your pin will be floating when the button is not pressed, resulting in random activations.

3 Likes

Unfortunately even with your code it’s still doing it :(. Do you think it could be a hardware issue?

Yes. The button works when it’s pressed and doesn’t go off randomly, but when the button is pressed, the buzzer beeps in weird patterns. Thanks for responding :slight_smile:

@jcommisso, which buzzer unit are you using?

You need to put boolean beep = true; globally outside setup() or loop()

You probably placed it in loop so it’s always true.

I’m using a breadboard mounted piezo buzzer. I tried both an active and passive piezo buzzer but it’s still doing the same thing.

I did place it globally but it’s still not working.

@jcommisso, can you please be more specific on the models you are using. I ask because the voltage and current available on a Photon pin is not the same as on an Arduino pin.

1 Like

Sorry, I’m not sure… it doesn’t say anything on it. But I know I’ve gotten it to work with the Particle before. I also know that it doesn’t work with 9V. So it may be the voltage, but its only acting up now because I have more devices connected to the Particle.

@jcommisso, can you post a picture of your setup. The buzzer may not be happy at 3.3v or it is drawing too much current for a GPIO pin.

1 Like

Here you go!

@jcommisso, that ring of neopixels will draw too much current from the 3V3 pin. If this project is USB powered, you should power the neopixels from the Vin pin. This may play a large part in your issues.

The diagram does not show me what the real buzzer model is. Can you take a picture of your passive and active buzzers and post them? I suspect these buzzers may be drawing to much current.

Here’s a picture of the buzzers that I tried. Thanks so much for going out of your way to help me!

@jcommisso, did you make the power changes for the neopixel ring and test again? The active buzzer is ideal. Are you driving it with a PWM signal (analogWrite()) or digitalWrite()?