Problems in syncing MCPWM output for dimming purpose

amehta
Posts: 22
Joined: Sat Feb 17, 2018 11:14 am

Problems in syncing MCPWM output for dimming purpose

Postby amehta » Mon Jun 04, 2018 1:17 pm

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?

amehta
Posts: 22
Joined: Sat Feb 17, 2018 11:14 am

Re: Problems in syncing MCPWM output for dimming purpose

Postby amehta » Wed Jun 06, 2018 6:57 pm

any directions on this please?

amehta
Posts: 22
Joined: Sat Feb 17, 2018 11:14 am

Re: Problems in syncing MCPWM output for dimming purpose

Postby amehta » Sat Jun 09, 2018 6:56 pm

Attaching circuit diagram with this comment. Also I have opened another post with sync question https://esp32.com/viewtopic.php?f=2&t=6033
Attachments
circuit_diagram.jpeg
circuit_diagram.jpeg (93.89 KiB) Viewed 11131 times

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Problems in syncing MCPWM output for dimming purpose

Postby WiFive » Sat Jun 09, 2018 8:00 pm

Image

amehta
Posts: 22
Joined: Sat Feb 17, 2018 11:14 am

Re: Problems in syncing MCPWM output for dimming purpose

Postby amehta » Sat Jun 09, 2018 9:03 pm

@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?

Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Re: Problems in syncing MCPWM output for dimming purpose

Postby Deouss » Sun Jun 10, 2018 6:08 am

Just curiosity - how high frequency can be set with MCPWM ?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Problems in syncing MCPWM output for dimming purpose

Postby WiFive » Sun Jun 10, 2018 11:23 am

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.

amehta
Posts: 22
Joined: Sat Feb 17, 2018 11:14 am

Re: Problems in syncing MCPWM output for dimming purpose

Postby amehta » Sun Jun 10, 2018 11:46 am

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.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Problems in syncing MCPWM output for dimming purpose

Postby WiFive » Sun Jun 10, 2018 12:32 pm

Zero crossing sync should reset pwm timer to zero. Triac should trigger at +4ms.

amehta
Posts: 22
Joined: Sat Feb 17, 2018 11:14 am

Re: Problems in syncing MCPWM output for dimming purpose

Postby amehta » Sun Jun 10, 2018 12:42 pm

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.

Who is online

Users browsing this forum: No registered users and 97 guests