Problem with Buzzer Part Mega 2560

Could you please review the code? It appears to have a malfunctioning function. According to the code, the buzzer is designed to sound only when the Threshold value is high, and it will stop when the value is low. However, after the Threshold value has been high once, the buzzer does not stop sounding. Initially, everything works normally, but the problem arises when the Threshold value returns to normal, and the buzzer continues to sound. Can you suggest a solution to this issue?

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address and LCD dimensions
const int buzzerPin = 7;            // Pin for buzzer
const int sensorPin = A0;           // Pin for MQ-3 sensor output
const int threshold = 550;          // Threshold for alcohol detection

void setup() {
  Serial.begin(9600);
  //ss.begin(9600);
  lcd.init();
  //lcd.begin();
  lcd.backlight();
  lcd.clear();
  lcd.print("Welcome!");
  delay(1000);
  //pinMode(7, OUTPUT);
  lcd.clear();
  lcd.print("Onboard");
  delay(2000);
  lcd.clear();
  lcd.print("Reading...");
  delay(3000);
  
}

void loop() {
  int sensorValue = analogRead(sensorPin);
  lcd.setCursor(0, 1);
  lcd.print(sensorValue);
  {
  if (sensorValue >= threshold) {
    lcd.clear();
    lcd.print("Overdrunk");
    lcd.setCursor(0, 1);
    lcd.print(sensorValue);
    tone(7,1,100);
  }
  else
  {   
  lcd.clear();
  lcd.print("NORMAL HAI BHIDU");
  lcd.setCursor(0, 1);
  lcd.print(sensorValue);
  noTone(7);
  }
  }
  delay(1000);
}

it looks like you have an active buzzer. don't use tone. use digitalWrite

Same problem.

The tone frequency must be greater than 31, you have it set to 1.

Try tone(7,400)

@jim-p it is obviously an active buzzer

No working sir

it's a pizo buzzer

Do you have a data sheet for the buzzer or a link to where you bought it
Can you post a simple schematic of how you have the buzzer connected to the Mega

Buzzer is connected like:- Red wire to the 5V power Source of Mega 2560 and the Black wire to the PIN 7 in Mega 2560.

buzzer link :- https://www.amazon.in/Buzzer-Small-Enclosed-Piezo-Electronic/dp/B07P87BWVY/ref=sr_1_1?keywords=piezo+buzzer&sr=8-1

i am using this buzzer.

Connect the red wire to pin 7 and the black wire to GND.
Use digitalWrite(7, HIGH) to turn on and digitalWrite(7, LOW) to turn off
Don't forget to uncomment the pinMode (7, OUTPUT)

One of the replies on that page is "have built in circuit".
That raises a red flag.

Connect the buzzer to 5volt and ground, and see if it makes a sound on it's own.
If it does, then you can't use tone() with it.
Leo..

That is your (other) problem. You have it wired to be on when the pin is LOW. So when you call noTone(7), you are actually turning it on, not off.

You have some code problems. After auto formatting it becomes obvious that you have extra brackets, also you can factor some code since it's in both the 'if' and 'else' clause.

void loop() {
  int sensorValue = analogRead(sensorPin);
  lcd.setCursor(0, 1);
  lcd.print(sensorValue);
  {
    if (sensorValue >= threshold) {
      lcd.clear();
      lcd.print("Overdrunk");
      lcd.setCursor(0, 1);
      lcd.print(sensorValue);
      tone(7, 1, 100);
    }
    else
    {
      lcd.clear();
      lcd.print("NORMAL HAI BHIDU");
      lcd.setCursor(0, 1);
      lcd.print(sensorValue);
      noTone(7);
    }
  }
  delay(1000);

I formatted it so you can see it.

Here is how you could factor:

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address and LCD dimensions
const int buzzerPin = 7;            // Pin for buzzer
const int sensorPin = A0;           // Pin for MQ-3 sensor output
const int threshold = 550;          // Threshold for alcohol detection

void setup() {
  Serial.begin(9600);
  //ss.begin(9600);
  lcd.init();
  //lcd.begin();
  lcd.backlight();
  lcd.clear();
  lcd.print("Welcome!");
  delay(1000);
  //pinMode(7, OUTPUT);
  lcd.clear();
  lcd.print("Onboard");
  delay(2000);
  lcd.clear();
  lcd.print("Reading...");
  delay(3000);

}

void loop() {
  int sensorValue = analogRead(sensorPin);
  lcd.setCursor(0, 1);
  lcd.print(sensorValue);

  lcd.clear();
  lcd.setCursor(0, 1);
  if (sensorValue >= threshold) {
    lcd.print("Overdrunk");
    tone(7, 1, 100);
  }
  else
  {
    lcd.print("NORMAL HAI BHIDU");
    noTone(7);
  }
  lcd.print(sensorValue);

  delay(1000);
}

With an active buzzer, yes.
A passive piezo doesn't care if a DC voltage is left on it.
But yes, it's better to wire the "whatever it is" between pin and ground.
Leo..

Okay, but that is an active buzzer.

Let's see after OP has tested it.
If it's an active buzzer, then OP should only use digitalWrite(), not tone().

Seller call everything "Piezo", even if it's not.
An acive buzzer is rarely a piezo.
It's usually a mechanical speaker, like a car claxon.
Leo..

Not so, as far as I remember... at least not all piezo. Some don't like extended periods of DC.

Notice (Handling)

  1. ...
  2. If DC voltage is applied to the component, silver migration
    may occur. Please pay full attention to avoid subjecting
    the component to DC voltage for long periods

Yes, knew that, but migration is a process that takes a long time.
DC on the piezo doesn't stop it from working right now.
Leo..

Yes, DC is what is needed right now. It's a DC device.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.