Configuring

#include "driver/ledc.h"

#define	MY_PWM_CHANNEL			LEDC_CHANNEL_0
#define	MY_PWM_MODE			LEDC_LOW_SPEED_MODE

	//--------------------------
	//----- SETUP LEDC PWM -----
	//--------------------------
	//Used for: 
	ledc_timer_config_t LedcTimer = {
		.speed_mode       = MY_PWM_MODE,
		.timer_num        = LEDC_TIMER_0,
		.duty_resolution  = LEDC_TIMER_13_BIT,		//Set duty resolution to 13 bits (0-8191)
		.freq_hz          = 2730,					//Frequency in Hz
		.clk_cfg          = LEDC_AUTO_CLK
	};
	ESP_ERROR_CHECK(ledc_timer_config(&LedcTimer));

	ledc_channel_config_t LedcChannel = {
		.speed_mode     = MY_PWM_MODE,
		.channel        = MY_PWM_CHANNEL,
		.timer_sel      = LEDC_TIMER_0,
		.intr_type      = LEDC_INTR_DISABLE,
		.gpio_num       = 8,						//<<<<<<<SET GPIO NUMBER<<<<<<<<<<<<<<<<<<<<<<<
		.duty           = 0, 						//Startup with 0% duty
		.hpoint         = 0
	};
	ESP_ERROR_CHECK(ledc_channel_config(&LedcChannel));
Set duty cycle
	//Set PWM Duty
	int TargetPwmDutyCycle = 4095;			//13bit resolution = 0-8191
	if (ledc_get_duty(MY_PWM_MODE, MY_PWM_CHANNEL) != TargetPwmDutyCycle)
	{
		ESP_ERROR_CHECK(ledc_set_duty(MY_PWM_MODE, MY_PWM_CHANNEL, TargetPwmDutyCycle));
		ESP_ERROR_CHECK(ledc_update_duty(MY_PWM_MODE, MY_PWM_CHANNEL));		//Update duty to apply the new value
	}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *