Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 6 months ago.
Why the buzzer with the following codes cannot generate any sounds?

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:
- 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:
1 Answer
6 years, 6 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
}
}