// Audio alert example using PWM
// Connections: Output connected between pin 26 and ground
#define LEDC_PIN 6 // Define output pin for speaker with 220ohm resistor
#define LEDC_RESOLUTION 10 // Set resolution to 10 bits
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32-S3!");
}
void loop() {
// 100Hz tone for 1 second
ledcAttach(LEDC_PIN, 50, LEDC_RESOLUTION); // Attach pin before starting tone
ledcWriteTone(LEDC_PIN, 100);
delay(1000);
ledcDetach(LEDC_PIN); // Detatch pin to stop the tone
delay(2000); // Delay before starting the next example
// 1000Hz tone for 1 second
ledcAttach(LEDC_PIN, 50, LEDC_RESOLUTION); // Attach pin before starting tone
ledcWriteTone(LEDC_PIN, 1000);
delay(1000);
ledcDetach(LEDC_PIN); // Detatch pin to stop the tone
delay(2000); // Delay before starting the next example
// Short rising tone (100 to 2000Hz for 200ms)
ledcAttach(LEDC_PIN, 50, LEDC_RESOLUTION); // Attach pin before starting tone
for (int i=1; i<20; i++) {
ledcWriteTone(LEDC_PIN, i * 100);
delay(10);
}
ledcDetach(LEDC_PIN); // Detatch pin to stop the tone
delay(2000); // Delay before starting the next example
// Sound alert - Short rise and fall sound effect
ledcAttach(LEDC_PIN, 50, LEDC_RESOLUTION); // Attach pin before starting tone
for (int i=1; i<20; i++) {
ledcWriteTone(LEDC_PIN, i * 100);
delay(20);
}
for (int i=10; i>0; i--) {
ledcWriteTone(LEDC_PIN, i * 100);
delay(10);
}
ledcDetach(LEDC_PIN); // Detatch pin to stop the tone
delay(2000);
// Slow fall (500 to 50 Hz 1800 ms)
ledcAttach(LEDC_PIN, 50, LEDC_RESOLUTION); // Attach pin before starting tone
for (int i=500; i>50; i--) {
ledcWriteTone(LEDC_PIN, i);
delay(4);
}
ledcDetach(LEDC_PIN); // Detatch pin to stop the tone
delay(2000); // Delay before starting loop again
}