ESP32-C3 trying to output 40MHz on GPIO10

espmich
Posts: 41
Joined: Tue Jan 16, 2024 8:32 pm

ESP32-C3 trying to output 40MHz on GPIO10

Postby espmich » Mon Jul 07, 2025 9:46 pm

In order to probe the crystal frequency without touching it, I want to create a periodic wave on GPIO10 to measure using my frequency counter. This is commonplace in other MCUs with a MCO (master clock output) function.

Code: Select all

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

void app_main() {
    // Configure the LEDC timer
    ledc_timer_config_t timer_conf;
    timer_conf.speed_mode = LEDC_LOW_SPEED_MODE;
    timer_conf.duty_resolution = LEDC_TIMER_1_BIT;
    timer_conf.timer_num = LEDC_TIMER_0;
    timer_conf.freq_hz = 40000000;
    //timer_conf.clk_cfg = APB_CLK;
    timer_conf.clk_cfg = LEDC_USE_XTAL_CLK;
    timer_conf.deconfigure = false;
    ledc_timer_config(&timer_conf);

    // Configure the LEDC channel
    ledc_channel_config_t channel_conf;
    channel_conf.gpio_num = 10; // Pin 16 GPIO-10
    channel_conf.speed_mode = LEDC_LOW_SPEED_MODE;
    channel_conf.channel = LEDC_CHANNEL_0;
    channel_conf.intr_type = LEDC_INTR_DISABLE;
    channel_conf.timer_sel = LEDC_TIMER_0;
    channel_conf.duty = 0;
    channel_conf.hpoint = 0;
    ledc_channel_config(&channel_conf);

    // Start the output
    ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);

    // Keep the task running
    while (1) {
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}
Unfortunately when I program the ESP32-C3 chip with the above code, it fails to ouput a periodic wave on GPIO10. I'm not sure what is wrong, want to take a look?

Sprite
Espressif staff
Espressif staff
Posts: 10617
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-C3 trying to output 40MHz on GPIO10

Postby Sprite » Thu Jul 10, 2025 5:02 am

Code: Select all

    //timer_conf.clk_cfg = APB_CLK;
    timer_conf.clk_cfg = LEDC_USE_XTAL_CLK;
You cannot get a 40MHz signal from the LEDC if you use the XTAL clock. Does it also not work if you use the APB clock?

espmich
Posts: 41
Joined: Tue Jan 16, 2024 8:32 pm

Re: ESP32-C3 trying to output 40MHz on GPIO10

Postby espmich » Fri Jul 18, 2025 7:49 pm

Before seeing your reply I tried switching code to the:

Code: Select all

esp-idf/examples/peripherals/ledc/ledc_basic
project. I was able to generate 4KHz signal but at 40KHz the MCU fails again, and of course 4MHz fails as you mentioned.

This project uses:

Code: Select all

ledc_timer.clk_cfg = LEDC_AUTO_CLK
Actually all these clock optoins confuse me. I changed the above line to:

Code: Select all

ledc_timer.clk_cfg = LEDC_USE_APB_CLK
...and will now try to test it again with a frequency counter. Please stay tuned.

espmich
Posts: 41
Joined: Tue Jan 16, 2024 8:32 pm

Re: ESP32-C3 trying to output 40MHz on GPIO10

Postby espmich » Fri Jul 18, 2025 8:09 pm

In case the LEDC peripheral is totally incapable of generating a clean 40MHz signal, what other options are there to do this?

espmich
Posts: 41
Joined: Tue Jan 16, 2024 8:32 pm

Re: ESP32-C3 trying to output 40MHz on GPIO10

Postby espmich » Fri Jul 18, 2025 9:18 pm

I tested the LEDC peripheral as described above, using the LEDC_USE_APB_CLK but it failed at about 9-10KHz.

I'm confused, can the ESP chips not output a stable high frequency signal?

espmich
Posts: 41
Joined: Tue Jan 16, 2024 8:32 pm

Re: ESP32-C3 trying to output 40MHz on GPIO10

Postby espmich » Mon Jul 21, 2025 4:04 pm

Any idea how to generate a 40 MHz signal, can a hardware timer be used or some configuration value in LEDC?

Sprite
Espressif staff
Espressif staff
Posts: 10617
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-C3 trying to output 40MHz on GPIO10

Postby Sprite » Tue Jul 22, 2025 2:37 am

That does not sound right. What frequency counter and what setup are you using?

espmich
Posts: 41
Joined: Tue Jan 16, 2024 8:32 pm

Re: ESP32-C3 trying to output 40MHz on GPIO10

Postby espmich » Thu Jul 24, 2025 12:51 pm

The frequency counter I'm using to measure the ESP32-C3 is the same one I use to measure all the STM32 MCUs successfully. It is an Philips PM-6672 with a standard (oscilloscope style) probe:

https://www.archive.org/details/manual_ ... OM_PHILIPS

This device indicates the frequency correctly on my ESP32-C3, whenever I set the MCU LEDC peripheral to output at 4KHz.

What I already did:
  • 1) Use Google search and copy the AI answer provided, real source code.
    2) Use the LEDC basic example in the ESP-IDF 5.42 development environment.
    3) Adjust #2 above to use Sprite TM's suggestion changing the clock to try all (AUTO, XTAL, APB) clocks.
Would you @Sprite please show example code here that is reasonably expected to work correctly? Please assume the GPIO10 (pin 16 on a ESP32-C3FH4 chip) as output. Thank you!

Sprite
Espressif staff
Espressif staff
Posts: 10617
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-C3 trying to output 40MHz on GPIO10

Postby Sprite » Fri Jul 25, 2025 1:01 am

- Using ESP-IDF v5.4.2
- Using an ESP32-C3-DevkitM-1 v1.0
- Example examples/peripherals/ledc/ledc_basic
- Modify main/ledc_basic_example_main.c as such:

Code: Select all

#define LEDC_OUTPUT_IO          (10) // Define the output GPIO
#define LEDC_DUTY_RES           LEDC_TIMER_1_BIT // Set duty resolution to 1 bit
#define LEDC_FREQUENCY          (40000000) // Frequency in Hertz. Set frequency at 40MHz
- Probing GPIO10:
RigolDS5.png
RigolDS5.png (95.9 KiB) Viewed 319 times
Given that I get a proper 40MHz, I'd imagine there may be something wrong with your setup. Do you have something else (a logic analyzer or an oscilloscope, for instance) that can verify your measurement?

espmich
Posts: 41
Joined: Tue Jan 16, 2024 8:32 pm

Re: ESP32-C3 trying to output 40MHz on GPIO10

Postby espmich » Sat Jul 26, 2025 5:15 pm

Thanks @Sprite and everything is working after your LEDC suggestion.

It appears my AI generated code set the duty cycle to 0 (should be 4096) and I had not modified the ESP-IDF LECD basic sample from LEDC_TIMER_13_BIT to LEDC_TIMER_1_BIT resolution. Nothing was working with those mistakes.

Who is online

Users browsing this forum: Amazon [Bot] and 4 guests