Buzzer works once on XIAO extension, but than silence ? Why?

Trying out the buzzer on the XIAO extension card with a ESP32C6. Works the first time, but then doesn’t? Is there something obvious I am missing here?

/*
 * Close Encounters
 */
 const int buttonPin = 1;
 int pinBuzzer = D3; // Define the buzzer on pin 3, if you're using XIAO RP2040/XIAO ESP32, change 3 to A3
 
 void setup() {
  Serial.begin(115200);
  while (!Serial);
    pinMode(pinBuzzer, OUTPUT); // Set the buzzer pin to output state
    pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
    int buttonState = digitalRead(buttonPin);

    // Check if the button is pressed, if the button is pressed:
    if (buttonState == LOW) {
        // The buzzer sounds with a frequency of 200, for a duration of 200 milliseconds
        Serial.println("Play");
        float sound[] = {587.3295,659.2551,523.2511,261.6256,391.9954};
        int length[] = {512,512,512,768,1024};
        for (int i = 0; i <= sizeof(sound); i++) {
          tone(pinBuzzer, sound[i],length[i]);
          delay(10);
        }
    }
    delay(256);
}

Suspect it is related to the buttonState that doesn’t reset itself it seems? Tried to do so manually, but that doesn’t work either?

But I also tried without the button, and it still only places once?? what is going on??

Anyone come across something similar?

Try this.

    if (buttonState == LOW) {
        // The buzzer sounds with a frequency of 200, for a duration of 200 milliseconds
        Serial.println("Play");
//        float sound[] = {587.3295,659.2551,523.2511,261.6256,391.9954};
        int sound[] = {587,659,523,261,392};        
        int length[] = {512,512,512,768,1024};
//        for (int i = 0; i <= sizeof(sound); i++) {
        for (int i = 0; i < 5; i++) {          
          tone(pinBuzzer, sound[i],length[i]);
          delay(10);
        }
    }
1 Like

I would love to try this, but right now I have had to downgrade my arduino library to use the round display and it pre-dates the ESP32C6 release- the chip at the center of this code.