Page 1 of 1

Problems in syncing MCPWM output for dimming purpose

Posted: Mon Jun 04, 2018 1:17 pm
by amehta
Hello there,

I am new to ESP-IDF and C, so please forgive my mistakes. I am trying to create a wifi dimmer using ESP32 wroom module. I have configured to use MCPWM APIs for the same. However I am seeing incorrect output on the connected load(bulb).

I understand that we will need to take zero crossing of incoming AC signal into account. We have created a full wave rectified for the same and the output of the rectifier have been connected to an optocouple to give out pulse. Following is the calculation :

Household freq of AC signal : 50 Hz
Output of the full wave rectified : 100 Hz ( 2 * incoming AC signal)

So according to above calculation I am getting a period of 10 ms(100 Hz) from my optocoupler output. So if my duty resolution is say 60%, then my MCPWM should fire the TRIAC on the zero crossing optocoupler interrupt for 6 ms (60% of 10 ms) and then close the triac gate for the remaining 4 ms till the next interrupt comes and this process is repeated forever.

I have given zero interrupt pin as sync input to the mcpwm module. Following is the cleaned up code

Code: Select all

#include <stdio.h>
#include "string.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_attr.h"
#include "soc/rtc.h"
#include "driver/mcpwm.h"
#include "soc/mcpwm_reg.h"
#include "soc/mcpwm_struct.h"
#include "esp_err.h"
#include "esp_log.h"

#include "utils/general.h"
#include "utils/node_storage.h"
#include "node/pwm.h"

static const char *TAG = "sample.pwm";

#define MCPWM_EN_SYNC 1

#define GPIO_SYNC_IN   18

#define FREQ_HZ		   100

static void mcpwm_example_gpio_initialize()
{
	ESP_LOGI(TAG, "Initializing all pwm pins");
	mcpwm_pin_config_t pin_config = {
	        .mcpwm0a_out_num = 16,
	        .mcpwm0b_out_num = 17,
	        .mcpwm1a_out_num = 21,
	        .mcpwm1b_out_num = 26,
	        .mcpwm_sync0_in_num  = GPIO_SYNC_IN,
	    };

	mcpwm_set_pin(MCPWM_UNIT_0, &pin_config);

	gpio_pulldown_en(GPIO_SYNC_IN); // needs to be pull down
}


/**
 * @brief Configure whole MCPWM module
 */
void initialize_gpios()
{
    //1. mcpwm gpio initialization
    mcpwm_example_gpio_initialize();

    //2. initialize mcpwm configuration
    ESP_LOGD(TAG, "Configuring Initial Parameters of mcpwm");
    mcpwm_config_t pwm_config;
    pwm_config.frequency = FREQ_HZ;
    pwm_config.cmpr_a = 0;       //duty cycle of PWMxA in %
    pwm_config.cmpr_b = 0;       //duty cycle of PWMxb in %
    pwm_config.counter_mode = MCPWM_UP_COUNTER;
    pwm_config.duty_mode = MCPWM_DUTY_MODE_1;
    mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config);   //Configure PWM0A & PWM0B with above settings

    mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_1, &pwm_config);   //Configure PWM1A & PWM1B with above settings


#if MCPWM_EN_SYNC
    mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_SELECT_SYNC0, 0);
    mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_1, MCPWM_SELECT_SYNC0, 0);
#endif

}

int change_status(int gpio, int duty) {
	if (gpio == 16) {
		ESP_ERROR_CHECK( mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, (float)value) );
	}
	else { //gpio 17
		ESP_ERROR_CHECK( mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_B, (float)value) );
	}

	return ret;
}

void main() {
	initialize_gpios();
	
	int ret = change_status(16, 50);

}
Also I have oscillloscope output on the following link.

In the attached video, BLUE line on the oscilloscope is for full wave rectifier(100 Hz) and yellow is for the 50% pwm duty cycle.

https://drive.google.com/open?id=1wtdYq ... Qt-NmgzNHO

Can you please help me out where I am going wrong?

Re: Problems in syncing MCPWM output for dimming purpose

Posted: Wed Jun 06, 2018 6:57 pm
by amehta
any directions on this please?

Re: Problems in syncing MCPWM output for dimming purpose

Posted: Sat Jun 09, 2018 6:56 pm
by amehta
Attaching circuit diagram with this comment. Also I have opened another post with sync question https://esp32.com/viewtopic.php?f=2&t=6033

Re: Problems in syncing MCPWM output for dimming purpose

Posted: Sat Jun 09, 2018 8:00 pm
by WiFive
Image

Re: Problems in syncing MCPWM output for dimming purpose

Posted: Sat Jun 09, 2018 9:03 pm
by amehta
@WiFive

Thank you for the reply. I understand this, hence I am using MCPWM unit of ESP32 with zero crossing interrupt signal as "sync" signal. My sync signal is currently the output of the optocoupler which is giving me a positive sign wave(its zero is the zero crossing). However I suspect the "sync" signal just needs to be an interrupt which will be high every few ms(10 ms) and then goes low (and not a sine wave like signal).

Can you please let me know if I am correct in my findings?

Re: Problems in syncing MCPWM output for dimming purpose

Posted: Sun Jun 10, 2018 6:08 am
by Deouss
Just curiosity - how high frequency can be set with MCPWM ?

Re: Problems in syncing MCPWM output for dimming purpose

Posted: Sun Jun 10, 2018 11:23 am
by WiFive
amehta wrote:So if my duty resolution is say 60%, then my MCPWM should fire the TRIAC on the zero crossing optocoupler interrupt for 6 ms (60% of 10 ms) and then close the triac gate for the remaining 4 ms till the next interrupt comes and this process is repeated forever.
I posted that drawing because you have to delay starting from zero crossing then fire triac, not the other way.

Re: Problems in syncing MCPWM output for dimming purpose

Posted: Sun Jun 10, 2018 11:46 am
by amehta
WiFive wrote:
amehta wrote:So if my duty resolution is say 60%, then my MCPWM should fire the TRIAC on the zero crossing optocoupler interrupt for 6 ms (60% of 10 ms) and then close the triac gate for the remaining 4 ms till the next interrupt comes and this process is repeated forever.
I posted that drawing because you have to delay starting from zero crossing then fire triac, not the other way.
Sync pin in the mcpwm is intended for the same purpose, right? In my case on receiving the zero crossing signal(which is the sync signal), the triac will need to fire.

Re: Problems in syncing MCPWM output for dimming purpose

Posted: Sun Jun 10, 2018 12:32 pm
by WiFive
Zero crossing sync should reset pwm timer to zero. Triac should trigger at +4ms.

Re: Problems in syncing MCPWM output for dimming purpose

Posted: Sun Jun 10, 2018 12:42 pm
by amehta
Okay, so you mean, when sync happens if my duty cycle is 60% then TRIAC should not conduct for 4ms(40% off time) and then start for 6ms. Will try that.

Also can sync pin be defined as interrupt and set its interrupt type to falling edge/rising edge?? Because output from my circuit is a sine wave and not just an impulse signal.