4 years, 12 months ago.

Why the buzzer with the following codes cannot generate any sounds?

/media/uploads/isaaczhao/image_of_circuit.jpg

NOTICE:When I use this buzzer and circuit to run other codes, it works well. So it may not be the fault of the buzzer and connection. Here are the codes below:

  1. include "mbed.h"

PwmOut buzzer(D5);

int main() { while(1){ buzzer.period(1/(659)); buzzer=0.5; wait(1); buzzer.period(1/(494)); buzzer=0.5; wait(1); buzzer.period(1/(554)); buzzer=0.5; wait(1); }

}

Question relating to:

Affordable and flexible platform to ease prototyping using a STM32L432KCU6 microcontroller.

1 Answer

4 years, 12 months ago.

You need to format your code in the <<code>> <</code>> so we can easily read it, see Editing tips.

It works if you use float numbers, 659.0. Try this below:

pwm test using Mbed(os2)

#include "mbed.h"

PwmOut buzzer(D5);
 
int main() {
 
 while(1){
   
    buzzer.period(1.0/659.0);
    buzzer=0.5;
    wait(1); 
    buzzer.period(1.0/494.0);
    buzzer=0.5;
    wait(1);
    buzzer.period(1.0/554.0);
    buzzer=0.5;
    wait(1);
    buzzer=0.0; // turn off audio 
    
    for (float i=0; i<26; i=i+5) {
            buzzer.period(1.0/969.0);
            buzzer = i/50.0f;
            wait(0.5);
            buzzer.period(1.0/800.0);
            wait(0.5);
    }
    buzzer=0.0; // turn off audio 
  
 }
}

Accepted Answer

Your method works well. Thanks for your answer and advice.

posted by Zhao Xiangyu 01 May 2019