Trying to use a buzzer, tone and noTone freeze my program

Hello all I am trying to use a buzzer at the moment. I have followed this tutorial exactly.

I have tried using digitalWrite(buzzer, HIGH); and tone(buzzer, 1000); and noTone(buzzer); to turn it off. I found that using digitalWrite(buzzer, HIGH); doesn't make the buzzer turn on but tone(buzzer,1000) does. The problem is that my loop will freeze and nothing else is executed after it. Does anyone know why this is? Thanks.

Please post your code and details of your buzzer.

No-one wants to look at an Instructable.

Especially an Instructable that does not work.

tone() uses pulse width modulation (PWM) on the pin to generate the frequency you (should) hear. `DigitalWrite(buzzer,HIGH)' simply sets the pin to +5V - no sound.

Not really - PWM implies a single frequency and variable duty-cycle.
tone() uses the timers in CTC (clear timer on compare) mode to give variable frequency.

heres my code

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-piezo-buzzer
 */

const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int buzzer= 3; // Arduino pin connected to Buzzer's pin

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(buzzer, OUTPUT);       // set arduino pin to output mode
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN); // read new state

  if (buttonState == HIGH) {
    Serial.println("The button is being pressed");
      tone(buzzer, 1000);

  }
  else
  if (buttonState == LOW) {
    Serial.println("The button is unpressed");
noTone(buzzer);
  }

    Serial.println("after the sound");

}



Serial.println("after the sound"); This code is never executed. How do I clear the timer on compare? What compare thanks/

review how to do a If else thingy.

if (condition) {
   // body of if statement
}
else
{
// body of else statement
}

Compare that if else thingy to your if else thingy. One of the if else thingies is wrong. and its not the one in this post.

For a given frequency passed into the tone() function, you get a constant duty cycle PWM signal.

...is what we call a square (50% duty cycle) or rectangular (!= 50% duty cycle) wave.
It's not PWM, because there is no M.

You wouldn't call a HIGH or LOW condition "a fixed amplitude AM signal"

Could you point what is wrong with OPs if else code, I’m not seeing it

it's not wrong but unnecessary.

    if (buttonState == HIGH)
        ...
    else if (buttonState == LOW)
        ...

is the same as

    if (buttonState == HIGH)
        ...
    else
        ...

Where is the else { thingy and } thingy?

i don't see you're code freezing. i see prints regardless of the button state

When you call analogWrite(), you get one, fixed duty cycle, yet that is called PWM.

it's not an else, it's an else if

Boink! I missed that.

Well that code was quickly taken put of something bigger so I made mistakes when I copy pastes it but they are still not going to make a difference when I use tone one way or another in my loop it freezes my loop and nothing after it is executed.

I found a new library for a newer version of tone hope this one doesn't do that.

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