
Arduino Quick Start


G9.#include <M5Unified.h>
constexpr uint8_t buzzer_pin = 9;
constexpr uint32_t freq = 4000;
constexpr uint8_t resolution = 10;
uint32_t beepCount = 0;
void showStatus(const char* status, uint32_t duty) {
// Draw the current PWM state on the display.
M5.Display.clear();
M5.Display.setTextSize(2);
M5.Display.setCursor(10, 20);
M5.Display.println("LEDC Buzzer");
M5.Display.setCursor(10, 60);
M5.Display.printf("Pin: %u", buzzer_pin);
M5.Display.setCursor(10, 90);
M5.Display.printf("Freq: %lu Hz", static_cast<unsigned long>(freq));
M5.Display.setCursor(10, 120);
M5.Display.printf("Duty: %lu", static_cast<unsigned long>(duty));
M5.Display.setCursor(10, 150);
M5.Display.printf("Status: %s", status);
M5.Display.setCursor(10, 180);
M5.Display.printf("Count: %lu", static_cast<unsigned long>(beepCount));
}
void setup() {
// Initialize the CoreS3 display.
M5.begin();
M5.Display.setRotation(1);
showStatus("Starting", 0);
// Attach the buzzer to the Arduino-ESP32 3.x LEDC output.
if (!ledcAttach(buzzer_pin, freq, resolution)) {
showStatus("Attach failed", 0);
// Stop here when the PWM output cannot be attached.
while (true) {
delay(1000);
}
}
showStatus("Ready", 0);
delay(1000);
}
void loop() {
// Generate a short PWM beep.
beepCount++;
ledcWrite(buzzer_pin, 200);
showStatus("ON", 200);
delay(100);
// Stop the PWM output before the next beep.
ledcWrite(buzzer_pin, 0);
showStatus("OFF", 0);
delay(3000);
}
