ESP32 PWM Max frequency output
Posted: Thu Mar 25, 2021 5:51 am
Can I know what's the Max ESP32 PWM frequency?
Need to drive 2.7MHz Piezoelectric transducer.
Need to drive 2.7MHz Piezoelectric transducer.
Code: Select all
#include <stdio.h>
#include "driver/ledc.h"
#include "esp_err.h"
#define LEDC_OUTPUT_IO 15 // Output GPIO of a sample 1 Hz pulse generator
static void ledc_init(void)
{
// Prepare and then apply the LEDC PWM timer configuration
ledc_timer_config_t ledc_timer;
ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE;
ledc_timer.timer_num = LEDC_TIMER_1;
ledc_timer.duty_resolution = LEDC_TIMER_4_BIT;
ledc_timer.freq_hz = 2700000; // set output frequency at 2.7 MHz
ledc_timer.clk_cfg = LEDC_APB_CLK;
ledc_timer_config(&ledc_timer);
// Prepare and then apply the LEDC PWM channel configuration
ledc_channel_config_t ledc_channel;
ledc_channel.speed_mode = LEDC_HIGH_SPEED_MODE;
ledc_channel.channel = LEDC_CHANNEL_1;
ledc_channel.timer_sel = LEDC_TIMER_1;
ledc_channel.intr_type = LEDC_INTR_DISABLE;
ledc_channel.gpio_num = LEDC_OUTPUT_IO;
ledc_channel.duty = 8; // set duty at about 50%
ledc_channel.hpoint = 0;
ledc_channel_config(&ledc_channel);
}
void app_main(void)
{
ledc_init();
}
Code: Select all
#define LEDC_CHANNEL_0 0
// use 4 bit precission for LEDC timer
#define LEDC_TIMER_4_BIT 4
// use 2.7MHz Hz as a LEDC base frequency
#define LEDC_BASE_FREQ 2700000
#define LED_PIN 5
void setup() {
// Setup timer and attach timer to a led pin
ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_4_BIT);
ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);
ledcWrite(LEDC_CHANNEL_0, 7);
}
void loop() {
}