Buzzer and LED interfering with each other on an arduino?

Hi, I recently got an arduino for Christmas and am experiencing a problem.  I am practicing using it with a piezo buzzer, push button, and a RGB LED.  I programmed it so that whenever the button was pushed, the LED turned green, and when it wasn't pushed, the LED turned RED.  It worked fine until I added the buzzer.  I modified the program so that the buzzer was on when the button was not pushed.  Once I did this, the buzzer turned on and green and red pin of the LED came on dimly making the LED orange when the button was not pushed, and when the button was pushed, the buzzer turned off and the LED lit up green like it was supposed to.  I wan't to know why the LED is orange instead of solid red.  Any help would be appreciated.  If you need more information let me know.
Here is my sketch
//#include <NewPing.h>


const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BUTTON_PIN = 7;
const int BUZZER_PIN = 6;
//const int TRIGGER_PIN = 12, ECHO_PIN = 11;
//NewPing sonar(TRIGGER_PIN, ECHO_PIN);

void setup()
{
  pinMode (RED_PIN, OUTPUT);
  pinMode (GREEN_PIN, OUTPUT);
  pinMode (BUTTON_PIN, INPUT);
  pinMode (BUZZER_PIN, OUTPUT);
}

void loop()
{
  if (digitalRead(BUTTON_PIN) == HIGH)
  {
    digitalWrite(GREEN_PIN, LOW);
    digitalWrite(RED_PIN, HIGH);
    analogWrite(BUZZER_PIN, 100);
  }
  else if (digitalRead(BUTTON_PIN) == LOW)
  {
    digitalWrite(RED_PIN, LOW);
    digitalWrite(GREEN_PIN, HIGH);
    analogWrite(BUZZER_PIN,0);
  }
}

sort by: active | newest | oldest

You are upsetting the chip, probably, because the piezo buzzer may well take more current that the chip can provide.

roboticarrow (author)  steveastrouk3 years ago

Is there a way I can avoid this?

roboticarrow (author)  roboticarrow3 years ago

Ok, I found out that adding a resistor fixes the problem, but it makes the buzzer quieter. Is there a way around this other than an external power supply?

Try using a transistor to drive the buzzer, you can use the same supply on the arduino, but you can pull more than the pin can supply on its own that way. The circuit is the same as that for a relay driver.

roboticarrow (author)  steveastrouk3 years ago

Thanks. The transistor worked.

roboticarrow (author)  steveastrouk3 years ago

Thanks. The transistor worked.

manojhar903 years ago

Hi Sir, I am new to arduino am doing one simple program but it doesnt work properly. My concept is when LED is turned off Buzzer will ON and When LED is turned ON Buzzer is off....Kindly send the program for this....it will me to take further process.....

iceng3 years ago

How do you actually know, it is a piezo buzzer ? !

I have several times discovered a mini Sonalert like buzzer ... which was actually an electro-mechanical contact breaking buzzer with the attendant electrical noise spikes well known to upset the operation of the Arduino !!

If you cannot see the piezo disc THEN it is a real Buzzer !!!!

roboticarrow (author) 3 years ago

Sorry the code is slightly off. I was experimenting and trying to fix the problem and forgot to switch it back. I have when the buzzer is on switched around. It should be

if (digitalRead(BUTTON_PIN) == HIGH)
{
digitalWrite(GREEN_PIN, LOW);
digitalWrite(RED_PIN, HIGH);
analogWrite(BUZZER_PIN, 0);
}
else if (digitalRead(BUTTON_PIN) == LOW)
{
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
analogWrite(BUZZER_PIN,100);