// Testing: https://docs.espressif.com/projects/arduino-esp32/en/latest/api/ledc.html
//
// The tone() function on a Arduino board is only for one buzzer at a time.
// The ESP32 simulates that. There can be only one buzzer active.
//
// The "ledc" has a few channels, and they work independantly.
// That means that multiple buzzers can be active at the same time.
const int buzzerPin1 = 4;
const int buzzerPin2 = 5;
const int buzzerPin3 = 6;
unsigned long frequency = 400UL;
int increment = +2;
void setup()
{
Serial.begin(115200);
Serial.println("-------------------------------");
Serial.println("Project ESP32-C3 with a buzzer.");
delay(1000);
Serial.print("Testing tone() and noTone() ... ");
tone(buzzerPin1, 700);
delay(1000);
noTone(buzzerPin1);
Serial.println("done 🠲 Okay 😀");
delay(3000);
// Is 50% PWM the same as a resolution of 1 ?
// I think it is.
// Is 1MHz base clock exaggerated high ?
// Probably.
Serial.print("Testing ledcWriteTone() ... ");
ledcAttach(buzzerPin1, 1000000UL, 1);
ledcWriteTone(buzzerPin1, 700);
delay(1000);
ledcDetach(buzzerPin1);
Serial.println("done 🠲 Okay 😀");
delay(3000);
Serial.print("Testing two tone() at the same time ... ");
tone(buzzerPin1, 700);
delay(500);
tone(buzzerPin2, 760);
delay(1000);
noTone(buzzerPin1);
delay(500);
noTone(buzzerPin2);
Serial.println("done 🠲 fail 🥲");
delay(3000);
Serial.print("Testing two ledcWriteTone() at the same time ... ");
ledcAttach(buzzerPin1, 1000000UL, 1);
ledcAttach(buzzerPin2, 1000000UL, 1);
ledcWriteTone(buzzerPin1, 700);
delay(500);
ledcWriteTone(buzzerPin2, 715);
delay(1000);
ledcDetach(buzzerPin1);
delay(500);
ledcDetach(buzzerPin2);
Serial.println("done 🠲 Success 🥳");
delay(3000);
Serial.print("All three buzzers in harmony ");
ledcAttach(buzzerPin1, 1000000UL, 1);
ledcAttach(buzzerPin2, 1000000UL, 1);
ledcAttach(buzzerPin3, 1000000UL, 1);
}
void loop()
{
// Create a wavy frequency by interference.
ledcWriteTone(buzzerPin1, frequency-1);
ledcWriteTone(buzzerPin2, frequency);
ledcWriteTone(buzzerPin3, frequency+3);
// Let the frequency go up and down.
if(frequency > 900UL || frequency < 400UL)
{
increment = -increment;
Serial.print("🚔");
}
frequency += increment;
delay(10);
}
🚔