Page 1 of 1

ESP32 Hardware Timer Gives Unreliable Results

Posted: Tue Jan 19, 2021 2:07 am
by physiii
I need create a PWM signal using a hardware timer. I would have used RMT or something more native to esp32 but I am using an i2c gpio expander. I wrote the code which was straight forward but am getting unreliable results (eg. pulse width being much to short or the io sticking high or low).

To rule out the i2c and my code, I started back with the timer example. I changed TIMER_INTERVAL1_SEC to 0.009 (9ms)

Code: Select all

#define TIMER_INTERVAL1_SEC   (0.009)   // sample test interval for the second timer
And toggled io26 on the esp32:

Code: Select all

static void timer_example_evt_task(void *arg)
{
    bool on = true;
    while (1) {
        timer_event_t evt;
        xQueueReceive(timer_queue, &evt, portMAX_DELAY);

        /* Print information that the timer reported an event */
        if (evt.type == TEST_WITHOUT_RELOAD) {
            // printf("\n    Example timer without reload\n");
          	// gpio_set_level(BLOWER_MOTOR_PIN, on);
        } else if (evt.type == TEST_WITH_RELOAD) {
            // printf("\n    Example timer with auto reload\n");
          	gpio_set_level(BLOWER_MOTOR_PIN, on);
            on = !on;
        } else {
            // printf("\n    UNKNOWN EVENT TYPE\n");
        }
        // printf("Group[%d], timer[%d] alarm event\n", evt.timer_group, evt.timer_idx);
        //
        // /* Print the timer values passed by event */
        // printf("------- EVENT TIME --------\n");
        // print_timer_counter(evt.timer_counter_value);
        //
        // /* Print the timer values as visible by this task */
        // printf("-------- TASK TIME --------\n");
        // uint64_t task_counter_value;
        // timer_get_counter_value(evt.timer_group, evt.timer_idx, &task_counter_value);
        // print_timer_counter(task_counter_value);
    }
}
Scope looks like this:
SDS00001.png
SDS00001.png (22.38 KiB) Viewed 2297 times

And this is what I am trying to create (pwm at 36.41hz with sub ms resolution):
blower.png
blower.png (274.37 KiB) Viewed 2297 times
blower2.png
blower2.png (275.05 KiB) Viewed 2297 times

I tried adjusting the divider and setting the io value in the ISR instead of using a queue but saw the same results. Why am I not able to get reliable output when using the timer to set gpio especially at smaller time scales?

Re: ESP32 Hardware Timer Gives Unreliable Results

Posted: Tue Jan 19, 2021 9:37 am
by ESP_Minatel
Hi,

Have you tried to use the High Resolution Timer Example?