noTone won't stop sound, just change it's volume

Hello guys, I am working on a small project with a contactless water sensor and a buzzer. It is very simple, if signal from a water sensor is true(1) play sound for a few seconds, and if it’s not - nothing.

But I have a problem with a noTone function, instead of turn sound of it just “shift” it to other kind of tone… I really can’t locate a problem, maybe it is in my buzzer, it is a version with a 3 pins (VCC|I/O|GND) and I connected it to a 3.3V pin on Arduino Uno.

Here is the code:

int liquid_level = 0;
int piezo_pin = 8;
boolean timer_started = false;
unsigned long current_time;

void setup() {
  pinMode(5, INPUT);
  Serial.begin(9600);
}

void loop() {
  liquid_level=digitalRead(5);
  if(liquid_level == 1) {
    Serial.println(liquid_level);
    if(timer_started == false ) {
      timer_started = true;
      current_time = millis();
      tone(piezo_pin, 1000);
    }
    else {
      if((millis() - current_time) >= 5000) {
        noTone(piezo_pin);
      }
    }
    Serial.println(timer_started);
  }
  else {
    Serial.println(liquid_level);
    noTone(8);
  }
  delay(1000);
}

So, my buzzer is connected to 3.3V, GND and a digital pin 8. My water sensor is connected to 5V, GND, and digital pin 5.

Is there something wrong with a code, or with a hardware (my wiring)?

Thanks in advance!

Try a simple sketch that does nothing but start a tone and stop it.

void loop() {
  tone(piezo_pin, 100, 800);
  noTone(piezo_pin);
  delay(5000);
}

With this a just got a constant sound...

P.S. piezo_pin is a digital pin on Arduino, connected on I/O pin on a buzzer.

Well I thought you might have an active buzzer but the FC-07 is passive - is that the exact module you are using in the picture or just one that sorta looks like it?

a7

Here is the exact one:

On the link: 20200423-234842 — ImgBB

Your timing goes like, on-off-delay-on-off-delay-on-off...

in that case the on time is only microseconds, you need to on-delay-off-delay-on-delay-off...

See?

Post link to the listing you bought the buzzer module from. I can't tell if that's the kind that's meant to be controlled with tone() or if it's the kind you're supposed to just turn on or off...

Here is the link to exactly the same model (not the store that I got it from, but that is the model.)

Yeah, that has a constant buzzing frequency and is turned on with a low level ("active low"). It's not the kind you drive with tone()

So, what should I use and how?

peky27:
So, what should I use and how?

I guess, use the FC-07 since it was mentioned as passive. For how, see reply #5.

peky27:
So, what should I use and how?

As mentioned above, this buzzer board is "active low". It will produce sound when IO pin is low and it will be quiet when IO pin is high.

If you are happy with the sound this buzzer produces you can turn it on with:

digitalWrite(piezo_pin, LOW);

and turn it off with:

digitalWrite(piezo_pin, HIGH);

:slight_smile:

Good point. You can use the self oscillating buzzer as described above. You have to turn it on for more than a few microseconds to hear it though. :slight_smile:

Ok, gotta point. Just one more question, is it safe to turn it off with "digitalwrite HIGH", because as far as I know HIGH set output to 3.3/5V..?

Why would it not be "safe"?

Others have told you it's an active low (inverse logic) signal:
LOW = ON
HIGH = OFF

So yes, it will have 3/3/5V (depending on your board) on the pin, which the device interprets as "OFF".