ledc example does not work at all

murray_lang
Posts: 24
Joined: Fri Oct 07, 2016 4:43 pm

ledc example does not work at all

Postby murray_lang » Tue Feb 28, 2017 6:10 am

Hi,

I've been struggling to get a PWM signal from my ESP32, so I built the ledc example from the SDK verbatim.

Still no PWM output on any of the configured pins. All I get is a logic high output when the duty is above 0 and logic low output when the duty is zero.

I suspected the problem might have been with my Nano32 board, so I fired up a WROOM-32 - with the same result.

Is the ledc example supposed to work with the current version of the SDK? If not could somebody please update the example or post errata here. Is there a poorly documented trick to getting PWM working? Or am I simply losing my faculties?

Regards,
Murray

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: ledc example does not work at all

Postby kolban » Tue Feb 28, 2017 2:41 pm

Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

murray_lang
Posts: 24
Joined: Fri Oct 07, 2016 4:43 pm

Re: ledc example does not work at all

Postby murray_lang » Wed Mar 01, 2017 12:40 am

Hi Neil,

Thanks for helping.

Here's what I did:
  • Copied the Hello World sample from the SDK
    Copied the code verbatim from the github link you provided
    Pasted that code to replace the example task
    Changed names in xTaskCreate() accordingly
    Successfully flashed my WROOM-32
    Connected my oscilloscope to GPIO4 and reset the module
What I see at GPIO4 is a DC signal that stays low for a varying number of seconds, then goes high for a varying number of seconds. Not PWM.

Have you tested this code on a recent version of the SDK?

Regards,
Murray

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: ledc example does not work at all

Postby kolban » Wed Mar 01, 2017 12:57 am

Howdy Murray,
I just rebuilt the test using my sample code and it worked as before. In this test I used GPIO 14 as GPIO 4 wasn't already wired to my logic analyzer and I also used a logic analyzer to look at the results. All good and a nice clean PWM signal. Not sure what to say ... maybe check your ground lead for the oscilloscope or some other oscilloscope settings (I don't own one of those and so am speaking from pure ignorance).

I'm using the latest ESP-IDF from github master as well as the latest ESP-IDF template app.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

murray_lang
Posts: 24
Joined: Fri Oct 07, 2016 4:43 pm

Re: ledc example does not work at all

Postby murray_lang » Wed Mar 01, 2017 1:21 am

Thanks Neil

Odd! I get the same result with my Nano32. I have been developing on both the ESP32 and the ESP8266, and can see PWM signals on the ESP8266 (after I eventually understood the documentation). My work so far on the ESP32 has been with a web server and plain GPIO driving stepper motors. These are now driven through a shift register so that only 3 GPIOs are used between both motors. There is no problem with the CRO connection as I have been using it all along to test signals.

I'm now moving on to DC motor control, and so need PWM. As I said in the original post, the ledc example was built with zero changes. All default values kept with menuconfig. No PWM, just DC that appears to be LOW when the duty cycle is set to 0, and HIGH when it is set to anything else.

Frustrating!

Murray

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: ledc example does not work at all

Postby kolban » Wed Mar 01, 2017 1:52 am

If you can recreate in a cleansed/minimal ESP32 application, you might post it to a download site and we can take a look at the source and/or compile it on other environments ... maybe a new set of eyes might see something helpful.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

murray_lang
Posts: 24
Joined: Fri Oct 07, 2016 4:43 pm

Re: ledc example does not work at all

Postby murray_lang » Wed Mar 01, 2017 3:29 am

OK, I have taken the ledc example and applied Occam's Razor to get a bare minimum example. I believe that it's short enough to simple paste here. This time I get no PWM at all. What I do get is a flat line LOW level with a HIGH pulse of 0.5 second duration every 37 seconds.
Since my last post I created a fresh clone of the SDK to make sure that it wasn't in some strange state.
Here is the code:

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/xtensa_api.h"
#include "driver/ledc.h"
#include "esp_attr.h"   
#include "esp_err.h"

#define LEDC_IO_0    (19)

esp_err_t app_main() 
{
 
    ledc_timer_config_t ledc_timer = {
       .bit_num = LEDC_TIMER_13_BIT,
        .freq_hz = 5000,
        .speed_mode = LEDC_HIGH_SPEED_MODE,
        .timer_num = LEDC_TIMER_0
    };
    ledc_timer_config(&ledc_timer);

    ledc_channel_config_t ledc_channel = {
        .channel = LEDC_CHANNEL_0,
        .duty = 100,
        .gpio_num = LEDC_IO_0,
        .intr_type = LEDC_INTR_DISABLE,
        .speed_mode = LEDC_HIGH_SPEED_MODE,
        .timer_sel = LEDC_TIMER_0
    };
    ledc_channel_config(&ledc_channel);

    while(1) {
        ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 8000);
        vTaskDelay(2000 / portTICK_PERIOD_MS);

        ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0);
        vTaskDelay(2000 / portTICK_PERIOD_MS);
    }
}

murray_lang
Posts: 24
Joined: Fri Oct 07, 2016 4:43 pm

Re: ledc example does not work at all

Postby murray_lang » Wed Mar 01, 2017 3:43 am

That was with my Nano32 board. I get the same result with my WROOM-32 except that the period between the pulses is nearly 28 seconds.]
Attachments
IMG_20170301_114049.jpg
IMG_20170301_114049.jpg (135.71 KiB) Viewed 16945 times

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: ledc example does not work at all

Postby ESP_igrr » Wed Mar 01, 2017 4:45 am

You probably need to add a call to ledc_update_duty(mode, channel) after the call to ledc_set_duty.

http://esp-idf.readthedocs.io/en/latest ... _channel_t

If you check the LEDC example, you can see this being done after the "printf("LEDC set duty without fade\n");" line

murray_lang
Posts: 24
Joined: Fri Oct 07, 2016 4:43 pm

Re: ledc example does not work at all

Postby murray_lang » Wed Mar 01, 2017 5:34 am

Thanks iggr

That explains the difference in behaviour between the above code and my previous tests. However, remember that I tried the ledc example verbatim.

Anyway, I added the ledc_update_duty() calls to the simple test above and I am back to the original problem. Scope trace is attached. Note the time scale.
Attachments
IMG_20170301_132639.jpg
IMG_20170301_132639.jpg (116.11 KiB) Viewed 16939 times

Who is online

Users browsing this forum: Majestic-12 [Bot] and 201 guests