Using timers and interrupts - AC dimmer application

matiasjhz
Posts: 2
Joined: Wed Feb 05, 2020 1:27 pm

Using timers and interrupts - AC dimmer application

Postby matiasjhz » Sun Jun 07, 2020 8:05 pm

Hello to everyone,

I want to build an AC dimmer with rotary enconder using the esp32, and add WiFi or bluetooth capability later. So far, I'm able to use timers and interrupts to read my rotary encoder, as well, pin interrupts to switch on/off the lamp.

My code goes like this:

1) I have a dimmer board which has zero-cross detection (from 50Hz mains), which is connected to an input pin (DIMMER_SYNC_PIN). I also use BTN1 as an on/off switch for the dimmer board (and hence the lamp). For both of pins I add ISR handlers.

Code: Select all

//install gpio isr service
	gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
	//hook isr handler for specific gpio pin
	gpio_isr_handler_add(BTN1_PIN, BTN1_ISR_handler,NULL);
	gpio_isr_handler_add(DIMMER_SYNC_PIN, dimmer_sync_handler,NULL);
2) I create 2 timers:

Code: Select all

TimerHandle_t timerEncoder;
TimerHandle_t timerDimmer;
	timerEncoder = xTimerCreate("EncoderTimer",encoder_timer_period,pdTRUE,(void*)0,encoder_timer_callback);
	timerDimmer = xTimerCreate("DimmerTimer",dimmer_timer_period,pdFALSE,(void*)0,dimmer_timer_callback);
The first timer, "EncoderTimer", is set with a period of encoder_timer_period, and its always running. For each period, calls the function encoder_timer_callback which reads the rotary encoder. This works perfectly fine.

The second timer, "DimmerTimer", is set with autoreload to FALSE, thus it will be a one-shot timer and enters the dormant state after it expires. The idea is to, every time there is a zero-cross detection at the DIMMER_SYNC_PIN, the ISR dimmer_sync_handler() is called:

Code: Select all

void IRAM_ATTR dimmer_sync_handler(void* arg){

	BaseType_t xHigherPriorityTaskWoken = pdFALSE;
	TickType_t newPeriod = pdMS_TO_TICKS(5);
	xTimerChangePeriodFromISR(timerDimmer,newPeriod,&xHigherPriorityTaskWoken);
        xTimerStartFromISR(timerDimmer,&xHigherPriorityTaskWoken);

}
in this part of the code, the period of the timer timerDimmer is set to 5ms (however this value corresponds to the proper delay in order to dim the lamp. It ranges from 0ms to 10ms [50Hz mains]. I'm using 5ms just for testing 50% dimming light). The timer should start from this ISR and after it expires (delay for dimmer TRIAC) I call the function dimmer_timer_callback which manages to send the pulse to the gate of the TRIAC.

My problem is that timerDimmer is not running, is not able start or run from the ISR function dimmer_sync_handler(). This is my second try but first I tried to use espressif sdk for timers rather than timers in FreeRTOS, yielding the same results, timer not able to start from ISR. What could be the problem? Thanks a lot!

If you are interested in more information, I'm sharing my two codes:
This is my first code, using timers from espressif timer driver: https://drive.google.com/file/d/11WvCDv ... sp=sharing

The second code, which I shared in this post, using FreeRTOS timers:
https://drive.google.com/file/d/11SREzd ... sp=sharing

lafar6502
Posts: 25
Joined: Fri Mar 27, 2020 8:26 pm

Re: Using timers and interrupts - AC dimmer application

Postby lafar6502 » Mon Jun 08, 2020 12:46 pm

some hints
1. zero detection - check for noise / oscillations and debounce if necessary. I dont know why but my zero detection circuit generated a lot of oscillations at each zero transition and this led to lots of WTF moments. So just count them - should be equal to 2 x mains frequency.
2. RTOS timers arent' accurate enough for power control, use esp high resolution timer
https://docs.espressif.com/projects/esp ... r_handle_t
or a hardware timer

matiasjhz
Posts: 2
Joined: Wed Feb 05, 2020 1:27 pm

Re: Using timers and interrupts - AC dimmer application

Postby matiasjhz » Tue Jun 09, 2020 10:33 pm

Hi,

1) My zero-cross detection works pretty well, I've already checked that. I'm pretty sure its an issue with timers.
2) I haven't tried high resolution timers but Ive already tried timers from esp32 library and they dont work. I uploaded my code using these timers.

Thanks a lot!

Who is online

Users browsing this forum: Baidu [Spider] and 271 guests