SunFounder ESP32 Starter Kit
void playFrequency(int frequency, int duration) {
ledcWriteTone(0, frequency); // Start the tone
delay(duration); // Wait for the specified duration
ledcWriteTone(0, 0); // Stop the buzzer
}
• uint32_t ledcWriteTone(uint8_t chan, uint32_t freq);: This function is used to
setup the LEDC channel to 50 % PWM tone on selected frequency.
– chan select LEDC channel.
– freq select frequency of pwm signal.
This function will return frequency set for channel. If 0 is returned, error occurs and ledc cahnnel
was not configured.
4. Configure the PWM channel and attach the buzzer pin in the setup() function.
void setup() {
ledcSetup(0, 2000, resolution); // Set up the PWM channel
ledcAttachPin(buzzerPin, 0); // Attach the buzzer pin to the PWM channel
}
• uint32_t ledcSetup(uint8_t channel, uint32_t freq, uint8_t
resolution_bits);: This function is used to setup the LEDC channel frequency and
resolution. It will return frequency configured for LEDC channel. If 0 is returned, error occurs
and ledc channel was not configured.
– channel select LEDC channel to config.
– freq select frequency of pwm.
– resolution_bits select resolution for ledc channel. Range is 1-14 bits (1-20 bits for
ESP32).
• void ledcAttachPin(uint8_t pin, uint8_t chan);: This function is used to attach the
pin to the LEDC channel.
– pin select GPIO pin.
– chan select LEDC channel.
5. In the loop() function, play the sequence of 7 notes with a brief pause between each note and a 1-second pause
before repeating the sequence.
void loop() {
for (int i = 0; i < 7; i++) {
playFrequency(frequencies[i], 300); // Play each note for 300ms
delay(50); // Add a brief pause between the notes
}
delay(1000); // Wait for 1 second before replaying the sequence
}
4. Actuators
1.13. 3.2 Custom Tone 51