#include <Arduino.h>
// Parameters
uint32_t freq_start = 100000; // Start frequency in Hz
uint32_t freq_end = 2000000; // End frequency in Hz
uint32_t step_time = 5; // Time spent at each frequency in milliseconds
uint32_t step_size = 10000; // Frequency delta between steps in Hz
uint8_t pwm_channel = 0; // LEDC PWM channel
uint8_t pwm_pin = 16; // Pin for PWM output
uint8_t pwm_resolution = 8; // PWM resolution in bits (max: 13 bits)
void setup() {
Serial.begin(115200);
// Attach the LEDC module to the specified pin
ledcSetup(pwm_channel, freq_start, pwm_resolution);
ledcAttachPin(pwm_pin, pwm_channel);
}
void loop() {
uint32_t current_freq = freq_start;
// Sweep frequency from freq_start to freq_end
while (current_freq <= freq_end) {
// Set the new frequency
ledcWriteTone(pwm_channel, current_freq);
Serial.printf("Frequency: %u Hz\n", current_freq);
// Wait for step_time duration
delay(step_time);
// Increment frequency by step_size
current_freq += step_size;
}
// Optional: Sweep back down to freq_start
current_freq = freq_end;
while (current_freq >= freq_start) {
ledcWriteTone(pwm_channel, current_freq);
Serial.printf("Frequency: %u Hz\n", current_freq);
delay(step_time);
current_freq -= step_size;
}
}