Hardwarenahe Programmierung
You are viewing an older revision! See the latest version
PWM Speaker
Der folgende Code gibt ein Beispiel für die Verwendung des Lautsprechers (Speaker) des mbed-Application-Board:
mbedESP32S_Speaker-PWM.ino
// mbed board speaker const int speaker = 12; // 12 corresponds to GPIO16 // setting PWM properties const int freq = 5000; const int ledChannel = 0; const int resolution = 8; void setup(){ // configure LED PWM functionalitites ledcSetup(ledChannel, freq, resolution); // attach the channel to the GPIO to be controlled ledcAttachPin(speaker, ledChannel); } void loop(){ // increase the LED brightness for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){ // changing the LED brightness with PWM ledcWrite(ledChannel, dutyCycle); delay(15); } // decrease the LED brightness for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){ // changing the LED brightness with PWM ledcWrite(ledChannel, dutyCycle); delay(15); } }