Piezo buzzer works in separate code, but when implemented in too a system it never shuts off

Currently I'm doing a project for a ATV belt temp sensor and warning system.
Everything except the buzzer works great, I have a LCD 16x2 display with a infrared temp sensor.

What I'm trying to do is when the belt temp reaches a certain point the heat level bar on the display flashes and the buzzer will sound off so I can give the belt a break.

Currently the buzzer just stays on in a low buzzing state, when the temp is reached for the warning, it buzzes a little louder and then continues the low buzz. its plugged into digital 9 and 5V, I've tried different pins. on top of that ive tried digital write and many other things
It seems like something in the code is keeping the 9 pin on?

Here is the code:

#include <Adafruit_MLX90614.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

//LCD port and init
LiquidCrystal_I2C lcd(0x27, 16, 2);
Adafruit_MLX90614 mlx = Adafruit_MLX90614();

//initi belttemp
int beltTemp;
int buzzer = 9;

//initi custom char
byte customChar[8] = {
  0b10100,
  0b11100,
  0b10101,
  0b00000,
  0b10000,
  0b10001,
  0b10000,
  0b11100
};

void setup()
{
  //temp sens init
  mlx.begin();
  
  //lcd init
  lcd.init();  
  lcd.backlight();  
  lcd.createChar(0, customChar);
  lcd.begin(16, 2);

  //buzzer init
  pinMode(buzzer,OUTPUT);
}

//**** MAIN ****
void loop() 
{  

  //init belttemp var
  beltTemp = mlx.readObjectTempF();

  //printing current belt temp
  lcd.setCursor(0,0);
  lcd.print("Belt temp:"); lcd.print(beltTemp); lcd.print("F"); lcd.print("     ");

  //printing belt heat level
  lcd.setCursor(0,1);
  lcd.write((uint8_t)0); //HL custom char
  HeatLevel(beltTemp); 
}

//**** FUNCTIONS ****

//Function for the Heat Level Bar
int HeatLevel(int beltTemp)
{
  //Heat Level
  int HL = 0;
  //Loop Control (Copy of HL)
  int LC = 0;
  //wont show if less than 100F
   HL = ((beltTemp-90)/10); //min 100, max 250
   LC = HL;

//when HL is greater than 0(100F) it shows heat level
if(HL > 0)
{
  while(LC > 0)
{
  lcd.write(255); 
  LC--;
}
}
lcd.print("              ");

//when HL is 10 (200F) or greater it flashes
  if(HL > 2)
  {
    //Clearing bar and buzzing alert (Flashing effect)
  delay(250);
    tone(buzzer, 1000);
    lcd.setCursor(1,1);
    lcd.print("               ");
  delay(250);
  noTone(buzzer);
  }
}

Please let me know if you see anything that's causing the buzzer to stay on at all times.

Thanks for posting code properly!

Please post a clearly labeled, hand drawn (not Fritzing) wiring diagram, and a link to the buzzer.

Here is the drawing, I tried to make it as straight forward as possible

Fine drawing! Please post a link to the buzzer product page or data sheet.

1 Like

Did you try just the buzzer alone?

This is a bit different, try it:

https://create.arduino.cc/projecthub/munir03125344286/add-buzzer-to-arduino-bf010b

a7

1 Like

Im just gonna cover all the bases so here's everything that's in the project.

Buzzer

Screen

Sensor

Ardunio

That buzzer is a "low level trigger" so to turn it off, you need to set pin 9 HIGH.

Oh.

That's an active buzzer, you just use digitalWrite() to turn it on and off at its build in frequency.

a7

1 Like

Yes I tried it with the buzzer alone, I didn't disconnect anything I just ran a different bit of code.

Simply:

void setup() {
  // put your setup code here, to run once:
int buzzer = 4;
pinMode(buzzer,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  int buzzer = 4;
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer, LOW);
delay(1000);

}

And it worked flawlessly

Good. So ditch you calls to tone() &c. and control it the way the example does.

a7

1 Like

Don't use tone(), just digitalWrite(9, LOW) to turn on, HIGH for off.

1 Like

(post deleted by author)

This worked! Thank you! my question now is why is it off when on HIGH? I read that when set to HIGH it would be on and on LOW it would be off? Is it simply because its a low level alarm?

it is a different part than the one you read about

1 Like

Thank you everyone for your help! Should I post the final project? This was my first post so I'm just wondering

That's what I read about digitalWrite, which is why im confused

please do. it will benefit future readers

1 Like

The key phrase is "low level triggered". Unfortunately when you buy things from Amazon, you rarely get proper operating manuals or instructions. The buyer comments are sometimes helpful.

As for digitalWrite, you specify a logic level, not what that logic level does. Logic HIGH could mean turn something ON or turn it OFF. Put comments in the code to make the intended operation clear to others.

1 Like

Yes. And you can use constants to make one place where "upside down" errors can be fixed, viz:

# define BUZZ_ON    HIGH
# define BUZZ_OFF   LOW

and use

digitalWrite(buzzerPin, BUZZ_ON);

anywhere you turn on the buzzer. And obvsly, I hope, BUZZ_OFF to turn it off.

Got it wrong everywhere, just swap the # defined values of BUZZ_ON and BUZZ_OFF.

BUZZ_OFF, haha.

Works good for switches and LEDs which may be wired in different ways than you expect.

a7

1 Like