HELP! Arduno buzzer

Hello! Im doing a school project- CO2 detector. It works good but i can't get the buzzer do what i want. I want it to beep for short time (10 seconds for example) if 150ppm is exceeded and if the value is under 150ppm then no beeping. Here is the code

/*
 * Displays text sent over the serial port (e.g. from the Serial Monitor) on
 * an attached LCD.
 * YWROBOT
 *Compatible with the Arduino IDE 1.0
 *Library version:1.1
 */
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
int buzzer=12;
void setup()
{
  pinMode(buzzer, OUTPUT);
  lcd.init();                      // initialize the lcd 
  lcd.backlight();
  Serial.begin(9600);
}

void loop()
{
int sensorValue = analogRead(A1);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CO2=");
Serial.print("CO2=");
lcd.print (sensorValue, DEC);
Serial.print (sensorValue, DEC);
lcd.println(" PPM      ");
Serial.println(" PPM      ");
lcd.setCursor(0, 1);
if (sensorValue < 100)
   {
   lcd.print("Good");
    Serial.print("Good ");
   }
if (sensorValue >100 && sensorValue <150)
{
  lcd.print("mediocre");
   Serial.print("mediocre ");
}

  if (sensorValue > 150)
{
  lcd.print("BAD");
  Serial.print("BAD");
}
      delay(1200);
if (sensorValue > 100)
{
tone(buzzer, 1000, 500); // tone() is the main function to use with a buzzer, it takes 2 or 3 parameteres (buzzer pin, sound frequency, duration)
 delay(1000);
 tone(buzzer, 2000, 500); // You can also use noTone() to stop the sound it takes 1 parametere which is the buzzer pin
 delay(1000);
 noTone(buzzer);
}
else
{
noTone(buzzer);
}
{

  }
}

Please include what the buzzer does or does not due. Sort of important information.

It might be a hardware issue.

Can you share a sketch of your circuit? A photo would do...


I hope this would do the job. There is 16x2 also I2C display. Its a 3 pin active buzzer. I can do real photos but the cable management is terrible and nothing would be vissible.

The buzzer turns on even when 150ppm are not exceeded and it won't stop anytime.

Never seen such a device. Can you supply any more information on the thing?

It is a buzzer with internal oscillation, the third pin set to zero makes it sound.
@vvventsi, why don't you have the third pin connected to pin 12 in your sketch?

This is the buzzer

The thirdnpin is connected to pin 12, i think the drawing app lagged and didn't save the drawing on the photo

Don't use the tone function, just put pin 12 LOW when you want it to sound and HIGH to stop it.

You can use a non-blocking buzzer library to make buzzer sound 10sec without blocking other task. See Arduino beep example for more detailed

It starts beeping again no matter what and doesnt stop.

Try this new code, if it is not to your liking change the delays and the limits of " sensorValue "

/*
   Displays text sent over the serial port (e.g. from the Serial Monitor) on
   an attached LCD.
   YWROBOT
  Compatible with the Arduino IDE 1.0
  Library version:1.1
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
int buzzer = 12;

void setup() {
  pinMode(buzzer, OUTPUT);
  lcd.init();                      // initialize the lcd
  lcd.backlight();
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A1);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("CO2=");
  Serial.print("CO2=");
  lcd.print (sensorValue, DEC);
  Serial.print (sensorValue, DEC);
  lcd.println(" PPM      ");
  Serial.println(" PPM      ");
  lcd.setCursor(0, 1);
  if (sensorValue < 100) {
    lcd.print("Good");
    Serial.print("Good ");
  }
  if (sensorValue > 100 && sensorValue < 150) {
    lcd.print("mediocre");
    Serial.print("mediocre ");
    digitalWrite(buzzer, LOW);
    delay (250);
    digitalWrite(buzzer, HIGH);
  }
  if (sensorValue > 150) {
    lcd.print("BAD");
    Serial.print("BAD");
    digitalWrite(buzzer, LOW);
    delay (1000);
    digitalWrite(buzzer, HIGH);
  }
  delay(750);
}

This one is working kinda okay. It doesn't beep when the value is under 100 and otherwise works good. I want to edit it so i that it beeps only above 150 so i deleted the 3 lines for the buzzer on if(sensorValue > 100 && sensorValue < 150) but then the buzzer beeps constantly even tho that it should't beep at all at that moment. Any ideas?`

 }
  if (sensorValue > 100 && sensorValue < 150) {
    lcd.print("mediocre");
    Serial.print("mediocre ");
    digitalWrite(buzzer, LOW);    */When i delete this and the 2 lines below this it beeps constantly instead of not beeping at all /*
    delay (250);
    digitalWrite(buzzer, HIGH);
  }
  if (sensorValue > 150) {
    lcd.print("BAD");
    Serial.print("BAD");
    digitalWrite(buzzer, LOW);
    delay (1000);
    digitalWrite(buzzer, HIGH);
  }
  delay(750);
}

You must have done something else, just by removing those three lines the program works correctly, check well what you have done.

I'm sure i haven't done anything else, i've made multiple attempts. It's really strange. I'm not the best at coding but I've learned how to find my mistakes and etc and as you said, it should work correctly by removing the 3 lines but it does not and there isn't any sence why it isn't. I'll bring the project to my IT teacher tomorrow and ask for advice but in theory it should work

How much is the sensor reading giving you? If you assign values directly to sensorValue the code works.

Depends but mostly around 130PPM. When i remove the lines for the 100-150ppm it beeps constantly and when i put them back it beeps with the set periods. It doesn't beep when it's under 100ppm

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