'Tone' in If Statements

Hi All. I am currently following Mr. Paul McWhorter's Arduino Tutorials: Arduino Tutorial 22: Understanding and Using Active Buzzers to Add Sound to Your Project - YouTube.

In Lesson #22 he has us create a small Arduino project where we have to make the buzzer create sound if the user inputs a number greater than 10. For the project he's using an active buzzer from the Elegoo kit but I'm using the Piezo buzzer from the Arduino starter kit (I'm not sure if that makes a difference or not).

This is the code that I made to fit this project goal:

int myNumb;
String msg="Please Input A Number: "; 
void setup() {
  pinMode(3,OUTPUT);
  Serial.begin(9600);

}
void loop() {
Serial.println(msg);
while (Serial.available()==0) {

}
myNumb=Serial.parseInt();
if (myNumb>10) {
  tone(3, 261);
  delay(1000);
}
else {
  tone(3,0);
  delay(1000);
}
}

However, when I enter any number greater than 10 into the Serial monitor, no sound comes out. I tried to use different pins like the Analog pin and Digital pin but to no avail. Is this something to do with the fact that I have a different buzzer from Mr. McWhorter? If so, whats the difference between an active buzzer and a piezo buzzer?

Thank you!

Edit: I had it backwards, I think. See

Anyway, one uses tone, the other uses simple digital output on/off. Is the code you posted Mr. Ice Tea guy's code, or your fixes to it to account for the different buzzer type?

original not awake:

Probably. Try just momentarily connecting your device between 5 volt and GND.

No Arduino. If it makes a tone, you have a passive active buzzer. If it only clicks when you connect and disconnect, you have an active passive buzzer.

A passive active buzzer doesn't use tone(), it only needs

 digitalWrite(buzzerPin, HIGH);

to turn on, Imma let you guess how to turn it off…

I used buzzerPin, that I set to 3. If you wire the device between pin 3 and ground, use 3.

 digitalWrite(3, HIGH);

On the other hand, active buzzers usually make some kind of awful noise when used with tone(), so I would advise checking your wiring very carefully.

HTH

a7

Hello! Thank you so much for taking the time to reply.

To check if my buzzer uses tone, I created some new coding statements and added it into Arduino. When I just used tone, my Arduino produced some sound so I don't think the problem is related to wiring.

The code I posted follows Mr Ice Tea guy's code very closely, except for a couple of small things altered. These are the two things I changed:

  1. He uses digitalWrite to make his buzzer make sound, whereas I changed my code to use tone.

  2. He puts in "digitalWrite(buzzerPin, LOW);" to turn off the sound in the if statement after the delay, whereas I did not.

So just sitting here waiting for the next thing. I'm out the door so that's why I may disappear.

Yes, but I see you have tone(3, 0). I usually use noTone(), which turns off the sound launched by tone(). Like

else {
  noTone();
  delay(1000);
}

So you should be hearing something. Did yuo get the polarity of the device correct? The '+' pin to theArduino output, and the other pin to GND?

Welcome to the forum BTW. How are you finding your time with Mr. ITG? He is popular.

a7

1 Like

Holy frick. I changed my tone(3,0) to use noTone() and my buzzer FINALLY MADE SOUND. THANK YOU SO MUCH.

Apologies for the stupid mistake lmao. Why would using tone(3,0) not work in comparison to noTone()?

Thank you for the welcome! Honestly, he's a really great teacher and I'm loving his lessons so far and the way that he teaches. I noticed he has a Raspberry Pi series so I'm really excited to follow along with those lessons too :).

Yay!

Happy to help. All before I leave for the beach.

I'm not sure, I never used tone(3, 0) since I knew about noTone().

I'm glad you've found a learning source you like. We always here to plug up the gaps and check your understanding and find your typos and harass you about your wiring, fun!

L8R

a7

Haha, thanks! :slight_smile:

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