Buzzer affects LED and serial monitor output

Hello,

My Arduino nano doesn't work as it should everytime I connect the buzzer to it.

My Arduino nano is connected to a LM35, a buzzer and 3 LEDs. My program blinks a specific LED and makes a noise for each temperature condition it meets. The problem is when I run my program, it blinks all the LEDs and make all the noise while my serial monitor outputs random temperature readings. When using another program to read the temperature only, the temperature is precise and is not random unlike the program that I'm using.

When I removed the buzzer, the LEDs work as they should depending on the temperature. I tried changing the pins for the LM35, buzzer and each LEDs but the same problem remains.

The code that I have:

/* Sensors */
int tempSensor = A6; //LM35 temp sensor
/* LEDs */
int ledNormalPin = 10;
int ledHypoPin = 11;
int ledFeverPin = 12;
/* Buzzer */
int buzzerPin = 9;
/*State*/
int ledNormalState = LOW;
int ledHypoState = LOW;
int ledFeverState = LOW;
/* Notes */
const int nHypo = 5500;
const int fHypo = 4000;
const int nFever = 700;
const int fFever = 900;
/* Duration */
const int dnHypo = 140;
const int dfHypo = 80;
const int dnFever = 140;
const int dfFever = 80;

/* Constants */
const float celcius = 0.48828125; //500/1024
const float hypoTemp = 36.5;
const float feverTemp = 38.0;

/* Two "independant" timed events */
const long eventTime_0_temp = 1000; //in ms
const long eventTime_normal_on = 300; //in ms
const long eventTime_normal_off = 1000; //in ms
const long eventTime_hypo_on = 300; //in ms
const long eventTime_hypo_off = 1000; //in ms
const long eventTime_fever_on = 300; //in ms
const long eventTime_fever_off = 1000; //in ms

/* past millis() */
unsigned long previousTime_0 = 0;
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
unsigned long previousTime_3 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(ledNormalPin, OUTPUT); 
  pinMode(ledHypoPin, OUTPUT);      
  pinMode(ledFeverPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(tempSensor, INPUT);
}

void loop() {

  /* Updates time frequently */
  unsigned long currentTime = millis();
  float temp = analogRead(tempSensor)*celcius;

  /* Temperature reading */
  if ( currentTime - previousTime_0 >= eventTime_0_temp) {
    Serial.print("Temp: ");
    Serial.println(float(analogRead(tempSensor)*celcius));
    
    /* Update the timing for the next event*/
    previousTime_0 = currentTime;
  }
     /* Normal */
  if ((ledNormalState == LOW)&&(currentTime - previousTime_1 >= eventTime_normal_on)&&(temp > hypoTemp)&&(temp < feverTemp)) {
    
    //noTone(buzzerPin);
    ledNormalState = HIGH;
    //tone(buzzerPin, nHypo, dnHypo);

    Serial.print("Normal state: High\n");
    //Serial.println(float(analogRead(tempSensor)*celcius));
    digitalWrite(ledNormalPin, ledNormalState);
    
/**/
    
/**/
    previousTime_1 = currentTime;
  }

  else if ((ledNormalState == HIGH)&&(currentTime - previousTime_1 >= eventTime_normal_off)) {
    //noTone(buzzerPin);
    ledNormalState = LOW;
    //tone(buzzerPin, fHypo, dfHypo);
    Serial.print("Normal state: Low\n");
    //Serial.println(float(analogRead(tempSensor)*celcius));
    digitalWrite(ledNormalPin, ledNormalState);
/**/
    
/**/
    previousTime_1 = currentTime;
  }

    /* Cold */
/**/
  if ((ledHypoState == LOW)&&(currentTime - previousTime_2 >= eventTime_hypo_on)&&(temp <= hypoTemp)) {
    //for(int i = 0; i <=1; i++ ){
    noTone(buzzerPin);
    ledHypoState = HIGH;
    tone(buzzerPin, nHypo, dnHypo);

    Serial.print("Hypo state: High\n");
    //Serial.println(float(analogRead(tempSensor)*celcius));
    digitalWrite(ledHypoPin, ledHypoState);
    //}
/**/
    
/**/
    previousTime_2 = currentTime;
  }

  else if ((ledHypoState == HIGH)&&(currentTime - previousTime_2 >= eventTime_hypo_off)) {
    noTone(buzzerPin);
    ledHypoState = LOW;
    tone(buzzerPin, fHypo, dfHypo);
    //Serial.print("Hypo state: Low\n");
    //Serial.println(float(analogRead(tempSensor)*celcius));
    digitalWrite(ledHypoPin, ledHypoState);
/**/
    
/**/
    previousTime_2 = currentTime;
  }
/**/
    /* Hot */
/**/
  if ((ledFeverState == LOW)&&(currentTime - previousTime_3 >= eventTime_fever_on)&&(temp >= feverTemp)) {
    //for(int i = 0; i <=1; i++ ){
    noTone(buzzerPin);
    ledFeverState = HIGH;
    tone(buzzerPin, nFever, dnFever);

    //Serial.print("Fever state:High\n");
    Serial.println(float(analogRead(tempSensor)*celcius));
    digitalWrite(ledFeverPin, ledFeverState);
    //}
/**/
    
/**/
    previousTime_3 = currentTime;
  }
/**/
/**/
  else if ((ledFeverState == HIGH)&&(currentTime - previousTime_3 >= eventTime_fever_off)) {

    ledFeverState = LOW;
    noTone(buzzerPin);
    tone(buzzerPin, fFever, dfFever);
    //Serial.print("Fever state:Low\n");
    //Serial.println(float(analogRead(tempSensor)*celcius));
    digitalWrite(ledFeverPin, ledFeverState);
/**/
    
/**/
    previousTime_3 = currentTime;
  }
/**/
}

Apologies for the comments, it helps with commenting out codes to test the program.

What can I do to solve this problem? Are there any issues with my code? do I have to change something in my output? add something to my circuit? :confused:

Hello,

My Arduino nano doesn't work as it should everytime I connect the buzzer to it.

My Arduino nano is connected to a LM35, a buzzer and 3 LEDs. My program blinks a specific LED and makes a noise for each temperature condition it meets. The problem is when I run my program, it blinks all the LEDs and make all the noise while my serial monitor outputs random temperature readings. When using another program to read the temperature only, the temperature is precise and is not random unlike the program that I'm using.

When I removed the buzzer, the LEDs work as they should depending on the temperature. I tried changing the pins for the LM35, buzzer and each LEDs but the same problem remains.

The code that I have:

/* Sensors */
int tempSensor = A6; //LM35 temp sensor
/* LEDs */
int ledNormalPin = 10;
int ledHypoPin = 11;
int ledFeverPin = 12;
/* Buzzer */
int buzzerPin = 9;
/*State*/
int ledNormalState = LOW;
int ledHypoState = LOW;
int ledFeverState = LOW;
/* Notes */
const int nHypo = 5500;
const int fHypo = 4000;
const int nFever = 700;
const int fFever = 900;
/* Duration */
const int dnHypo = 140;
const int dfHypo = 80;
const int dnFever = 140;
const int dfFever = 80;

/* Constants */
const float celcius = 0.48828125; //500/1024
const float hypoTemp = 36.5;
const float feverTemp = 38.0;

/* Two "independant" timed events */
const long eventTime_0_temp = 1000; //in ms
const long eventTime_normal_on = 300; //in ms
const long eventTime_normal_off = 1000; //in ms
const long eventTime_hypo_on = 300; //in ms
const long eventTime_hypo_off = 1000; //in ms
const long eventTime_fever_on = 300; //in ms
const long eventTime_fever_off = 1000; //in ms

/* past millis() */
unsigned long previousTime_0 = 0;
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
unsigned long previousTime_3 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(ledNormalPin, OUTPUT); 
  pinMode(ledHypoPin, OUTPUT);      
  pinMode(ledFeverPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(tempSensor, INPUT);
}

void loop() {

  /* Updates time frequently */
  unsigned long currentTime = millis();
  float temp = analogRead(tempSensor)*celcius;

  /* Temperature reading */
  if ( currentTime - previousTime_0 >= eventTime_0_temp) {
    Serial.print("Temp: ");
    Serial.println(float(analogRead(tempSensor)*celcius));
    
    /* Update the timing for the next event*/
    previousTime_0 = currentTime;
  }
     /* Normal */
  if ((ledNormalState == LOW)&&(currentTime - previousTime_1 >= eventTime_normal_on)&&(temp > hypoTemp)&&(temp < feverTemp)) {
    
    //noTone(buzzerPin);
    ledNormalState = HIGH;
    //tone(buzzerPin, nHypo, dnHypo);

    Serial.print("Normal state: High\n");
    //Serial.println(float(analogRead(tempSensor)*celcius));
    digitalWrite(ledNormalPin, ledNormalState);
    
/**/
    
/**/
    previousTime_1 = currentTime;
  }

  else if ((ledNormalState == HIGH)&&(currentTime - previousTime_1 >= eventTime_normal_off)) {
    //noTone(buzzerPin);
    ledNormalState = LOW;
    //tone(buzzerPin, fHypo, dfHypo);
    Serial.print("Normal state: Low\n");
    //Serial.println(float(analogRead(tempSensor)*celcius));
    digitalWrite(ledNormalPin, ledNormalState);
/**/
    
/**/
    previousTime_1 = currentTime;
  }

    /* Cold */
/**/
  if ((ledHypoState == LOW)&&(currentTime - previousTime_2 >= eventTime_hypo_on)&&(temp <= hypoTemp)) {
    //for(int i = 0; i <=1; i++ ){
    noTone(buzzerPin);
    ledHypoState = HIGH;
    tone(buzzerPin, nHypo, dnHypo);

    Serial.print("Hypo state: High\n");
    //Serial.println(float(analogRead(tempSensor)*celcius));
    digitalWrite(ledHypoPin, ledHypoState);
    //}
/**/
    
/**/
    previousTime_2 = currentTime;
  }

  else if ((ledHypoState == HIGH)&&(currentTime - previousTime_2 >= eventTime_hypo_off)) {
    noTone(buzzerPin);
    ledHypoState = LOW;
    tone(buzzerPin, fHypo, dfHypo);
    //Serial.print("Hypo state: Low\n");
    //Serial.println(float(analogRead(tempSensor)*celcius));
    digitalWrite(ledHypoPin, ledHypoState);
/**/
    
/**/
    previousTime_2 = currentTime;
  }
/**/
    /* Hot */
/**/
  if ((ledFeverState == LOW)&&(currentTime - previousTime_3 >= eventTime_fever_on)&&(temp >= feverTemp)) {
    //for(int i = 0; i <=1; i++ ){
    noTone(buzzerPin);
    ledFeverState = HIGH;
    tone(buzzerPin, nFever, dnFever);

    //Serial.print("Fever state:High\n");
    Serial.println(float(analogRead(tempSensor)*celcius));
    digitalWrite(ledFeverPin, ledFeverState);
    //}
/**/
    
/**/
    previousTime_3 = currentTime;
  }
/**/
/**/
  else if ((ledFeverState == HIGH)&&(currentTime - previousTime_3 >= eventTime_fever_off)) {

    ledFeverState = LOW;
    noTone(buzzerPin);
    tone(buzzerPin, fFever, dfFever);
    //Serial.print("Fever state:Low\n");
    //Serial.println(float(analogRead(tempSensor)*celcius));
    digitalWrite(ledFeverPin, ledFeverState);
/**/
    
/**/
    previousTime_3 = currentTime;
  }
/**/
}

Apologies for the comments, it helps with commenting out codes to test the program.

What can I do to solve this problem? Are there any issues with my code? do I have to change something in my output? add something to my circuit? :confused:

Which buzzer do you use? How is it connected?

The buzzer I'm using is the piezo buzzer. It's that round black cylinder one. I attached a photo for reference.

The positive pin is connected to D9 pin while the negative is connected to arduino nano ground pin.

piezo-buzzer-01-500x500-500x435.jpg

It might be easier if you create a copy of your code with all the messy stuff removed to make it possible to read.

Looking at it I would be thinking about the individual ‘things’ you are doing like reading temperature and giving them a separate function which you can call when required. It may be that your code would be amenable to a ‘case’ structure but I am not sure exactly what you are trying to do.

Are you saying the program work normally if you just reMove the buzzer and keep the code the same?
If so you have a power supply or interference issue.

I agree with you, pmagowan so I rewrote my code from scratch. I removed some unnecessary comments and added additional variables to compensate for overlapping time events. Apparently what my initial code was doing was the LED events were happening at the same time and ended up overlapping each other. I created 3 more time events that happened independent from the 3 LED on-off blink events. For example, at normal temperature, the first event occurred that happens every 5 seconds. In that event, it tells the LED to blink on and off at another set of timed events. The result allows all the temperature evaluations to happen in their given timeline with each having another timeline for LED blinking.

/* Sensors */
int tempSensor = A0; //LM35 temp sensor
/* LEDs */
int ledNormalPin = 10;
int ledHypoPin = 11;
int ledFeverPin = 12;
/* Buzzer */
int buzzerPin = 9;
/*State*/
int ledNormalState = LOW;
int ledHypoState = LOW;
int ledFeverState = LOW;
/* Notes */
const int nHypo = 5500;
const int fHypo = 4000;
const int nFever = 6000;
const int fFever = 7300;
/* Duration */
const int dnHypo = 200;
const int dfHypo = 100;
const int dnFever = 200;
const int dfFever = 100;

/* Constants */
const float celcius = 0.48828125; //500/1024
const float hypoTemp = 36.5;
const float feverTemp = 38.0;

/* timed events */
const long eventTime_0_temp = 1000; //in ms
const long eventTime_1_temp = 4000; //in ms for normal
const long eventTime_2_temp = 1500; //in ms for hypo
const long eventTime_3_temp = 1000; //in ms for fever
const long eventTime_normal_on = 300; //in ms
const long eventTime_normal_off = 300; //in ms
const long eventTime_hypo_on = 300; //in ms
const long eventTime_hypo_off = 300; //in ms
const long eventTime_fever_on = 300; //in ms
const long eventTime_fever_off = 300; //in ms

/* past millis() */
unsigned long previousTime_0 = 0;
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
unsigned long previousTime_3 = 0;
unsigned long previousTime_normal = 0;
unsigned long previousTime_hypo = 0;
unsigned long previousTime_fever = 0;

void setup() {
  Serial.begin(9600);
  pinMode(ledNormalPin, OUTPUT); 
  pinMode(ledHypoPin, OUTPUT);      
  pinMode(ledFeverPin, OUTPUT); 
  pinMode(tempSensor, INPUT);
}

void loop(){
    /* Updates time frequently */
  unsigned long currentTime = millis();
  float temp = analogRead(tempSensor)*celcius;

  /* Temperature reading */
  if (currentTime - previousTime_0 >= eventTime_0_temp) {
    Serial.print("Temp: ");
    Serial.println(temp,4);
    
    previousTime_0 = currentTime;
  }
/*Normal*/
  if (currentTime - previousTime_1 >= eventTime_1_temp) {
    Serial.print("Hey\n");
      if ((ledNormalState == LOW)&&(currentTime - previousTime_normal >= eventTime_normal_on)&&(temp > hypoTemp)&&(temp < feverTemp)) {
        //noTone(buzzerPin);
        ledNormalState = HIGH;
        //tone(buzzerPin, nHypo, dnHypo);
    
        Serial.print("Normal state: High\n");
        //Serial.println(float(analogRead(tempSensor)*celcius));
        digitalWrite(ledNormalPin, ledNormalState);
        previousTime_normal = currentTime;
        }
    previousTime_1 = currentTime;
  }
  if ((ledNormalState == HIGH)&&(currentTime - previousTime_normal >= eventTime_normal_off)) {
        //noTone(buzzerPin);
        ledNormalState = LOW;
        //tone(buzzerPin, fHypo, dfHypo);
        Serial.print("Normal state: Low\n");
        //Serial.println(float(analogRead(tempSensor)*celcius));
        digitalWrite(ledNormalPin, ledNormalState);
        previousTime_normal = currentTime;
        }
/*Hypotermia*/
  if (currentTime - previousTime_2 >= eventTime_2_temp) {
    Serial.print("Hello\n");
      if ((ledHypoState == LOW)&&(currentTime - previousTime_hypo >= eventTime_hypo_on)&&(temp <= hypoTemp)) {
        //noTone(buzzerPin);
        ledHypoState = HIGH;
        tone(buzzerPin, nHypo, dnHypo);
    
        Serial.print("Hypo state: High\n");
        //Serial.println(float(analogRead(tempSensor)*celcius));
        digitalWrite(ledHypoPin, ledHypoState);
        previousTime_hypo = currentTime;
        }
    previousTime_2 = currentTime;
  }
  if ((ledHypoState == HIGH)&&(currentTime - previousTime_hypo >= eventTime_hypo_off)) {
        //noTone(buzzerPin);
        ledHypoState = LOW;
        tone(buzzerPin, fHypo, dfHypo);
    
        Serial.print("Hypo state: High\n");
        //Serial.println(float(analogRead(tempSensor)*celcius));
        digitalWrite(ledHypoPin, ledHypoState);
        previousTime_hypo = currentTime;
        }
/*Fever*/
  if (currentTime - previousTime_3 >= eventTime_3_temp) {
    Serial.print("Howdy\n");
      if ((ledFeverState == LOW)&&(currentTime - previousTime_fever >= eventTime_fever_on)&&(temp >= feverTemp)) {//Remove temp condition and run, set external temp to hypo and you get a catchy tune 
        //noTone(buzzerPin);
        ledFeverState = HIGH;
        tone(buzzerPin, nFever, dnFever);
    
        Serial.print("Fever state: High\n");
        //Serial.println(float(analogRead(tempSensor)*celcius));
        digitalWrite(ledFeverPin, ledFeverState);
        previousTime_fever = currentTime;
        }
    previousTime_3 = currentTime;
  }
  if ((ledFeverState == HIGH)&&(currentTime - previousTime_fever >= eventTime_fever_off)) {
        //noTone(buzzerPin);
        ledFeverState = LOW;
        tone(buzzerPin, fFever, dfFever);
    
        Serial.print("Fever state: Low\n");
        //Serial.println(float(analogRead(tempSensor)*celcius));
        digitalWrite(ledFeverPin, ledFeverState);
        previousTime_fever = currentTime;
        }        
}

It is still a bit long but I'm still trying to shorten this using functions when I can.

piezo-buzzer-01-500x500-500x435.jpg

If it's an active buzzer then a direct connection to Arduino pins is calling for trouble. A passive buzzer can not normally affect the hardware from working properly.

So what do you mean by "removing the buzzer"? Disconnect it physically or remove the buzzer code from your sketch?

Thanks Paul__B, that's what I was referring to. By any chance you can share how to post images here?

DrDiettrich, it's the one posted by Paul__B. I think that's active since mine has resin at the bottom.

refer_pin:
Thanks Paul__B, that's what I was referring to. By any chance you can share how to post images here?

DrDiettrich, it's the one posted by Paul__B. I think that's active since mine has resin at the bottom.

If the buzzer is an active one then it contains its own internal circuitry to generate the output frequency.

You don't use the tone( ) function, you just apply a voltage to turn it on.

There is a guide on how to insert images at Simple guide for inserting images in a post - Website and Forum - Arduino Forum

refer_pin:
Thanks Paul__B, that's what I was referring to. By any chance you can share how to post images here?

If you click on "quote", you can see what code I used. :grinning:

Note that you have to actually post the attachment first in order to get its link location, then go back and edit the post in order to use that as the URL for the image. Obviously in this case I put your attachment link in my reply. :sunglasses:

I would suspect that to be an "active" buzzer containing the driver circuitry since it has a "+" marked on it which would be meaningless for a passive piezo disc.

JohnLincoln:
You don't use the tone( ) function, you just apply a voltage to turn it on.

tone() function works in my code. What function can you suggest to make the buzzer make a noise in a specific frequency?

Paul__B:
If you click on "quote", you can see what code I used. :grinning:

Thanks for this. Learned now how to use quotes properly :smiley:

Paul__B:
I would suspect that to be an "active" buzzer containing the driver circuitry since it has a "+" marked on it which would be meaningless for a passive piezo disc.

what do you mean by the "active" buzzer containing the driver circuitry?

refer_pin:
Thanks for this. Learned now how to use quotes properly :smiley:

what do you mean by the "active" buzzer containing the driver circuitry?

The active buzzers I have seen all have a "+" sign stamped near one pin. They have to be installed with the correct polarity. If yours is just a plain piezo, there is no polarity.

Paul

Paul_KD7HB:
The active buzzers I have seen all have a "+" sign stamped near one pin. They have to be installed with the correct polarity. If yours is just a plain piezo, there is no polarity.

Paul

The buzzer I have has the "+" sign above it stamped near a pin so I believe that it has a polarity.

refer_pin:
The buzzer I have has the "+" sign above it stamped near a pin so I believe that it has a polarity.

Then apply +5 to the plus pin and - to the other pin. If your hearing is better than mine, you will hear the tone.

Paul

Connect the buzzer to 5V and Gnd and hear its frequency. If you want other frequencies you have to use a passive buzzer with tone().

Paul_KD7HB:
Then apply +5 to the plus pin and - to the other pin. If your hearing is better than mine, you will hear the tone.

Paul

I think what you said was funny regarding the hearing and all:D but I realized that I might offend you of sort so no offense but I still think it's funny.

But would that only make a single tone?

DrDiettrich:
Connect the buzzer to 5V and Gnd and hear its frequency. If you want other frequencies you have to use a passive buzzer with tone().

I did the 5v to Gnd but it's just creating a single frequency. Using the tone() I can generate other set of frequency. Are there any alternatives to tone()?