Best Frequency Meter ever made with ESP32 - awesome!

User avatar
jgustavoam
Posts: 146
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: Best Frequency Meter ever made with ESP32 - awesome!

Postby jgustavoam » Tue May 28, 2024 12:01 pm

Hi ligteltelecom,
I don't remember the ESP32 documentation, but I think you can change these two pins. But be careful, as some ESP32 pins are reserved and cannot be used. Which pins do you intend to use?

#define PCNT_INPUT_CTRL_IO GPIO_NUM_35 // Set Pulse Counter Control GPIO pin - HIGH = count up, LOW = count down
#define OUTPUT_CONTROL_GPIO GPIO_NUM_32 // Timer output control port - GPIO_32

References:

https://docs.espressif.com/projects/esp ... -esp32.pdf

https://www.espressif.com/sites/default ... ual_en.pdf
Retired IBM Brasil
Electronic hobbyist since 1976.

Araqel
Posts: 2
Joined: Wed Apr 29, 2020 4:39 pm

Re: Best Frequency Meter ever made with ESP32 - awesome!

Postby Araqel » Mon Jun 03, 2024 8:42 am

I tried to compile the sketch, but it is not compatible with latest IDF core 5.1 in Arduino IDE. Please, review the code.

User avatar
jgustavoam
Posts: 146
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: Best Frequency Meter ever made with ESP32 - awesome!

Postby jgustavoam » Tue Jun 04, 2024 8:55 pm

Hi Araquel,
The code created by the topic is for Arduino. Please inform which IDF code you are using.
It's this one? Arduino Release v3.0.0 based on ESP-IDF v5.1.4 (latest)?
Please report the error message.

If you want to test compiling with the ESP32 IDF, try this:
https://github.com/Gustavomurta/ESP32_f ... 2freqMeter
Retired IBM Brasil
Electronic hobbyist since 1976.

t_tuku
Posts: 3
Joined: Wed May 01, 2024 4:47 pm

Re: Best Frequency Meter ever made with ESP32 - awesome!

Postby t_tuku » Thu Jul 04, 2024 8:03 am

jgustavoam wrote:
Thu May 02, 2024 10:34 am
Hi Tuku,

First define the frequency range you want to use in your project.
For example from 1 Hz to 100KHz. If it is for frequencies below 1 Hz, the sample time will have to be changed.
sample_time = 1000000; ( one million miliseconds)

1. The pulse counter uses the ESP32 pcnt.
Pcnt has the following parameters:
a. input port;
b. input channel;
c. control port;
d. count on Pulse raising;
e. count on Pulse falling;
f. counting only with Control - high level;
g. maximum count limit.

The high level of control port enable the counter to count the pulses that arrive at the input port.
Pulses are counted both as the pulse rising and falling, to improve the counting average.
The sampling time is defined by the high resolution esp-timer, and it is defined in 1 second, in the sample-time variable.
If the count is greater than 20000 pulses during the counting time, overflow occurs and for with each overflow that occurs
the multPulses variable is incremented. Then pulse counter is cleared and proceed to counting.
Unfortunately the Pulse Counter has only 16 bits that may be used.


When the sampling time ends, a routine is called and the value in the pulse counter is read and saved.
A flag is set on to indicating that the pulse reading has ended.

In the loop, when verifying if the flag is on indicates that the pulse reading has finished, the value is calculated by multiplying
the number of overflow by 20000 and adding to the number of remaining pulses and dividing by 2, because it counted 2 times.

You must use two counters. One to count the positive part of the pulse and one for the negative part.

void init_PCNT(void) // Initialize and run PCNT unit
{
pcnt_config_t pcnt_config = { }; // PCNT unit instance

pcnt_config.pulse_gpio_num = PCNT_INPUT_SIG_IO; // Pulse input GPIO 34 - Freq Meter Input
pcnt_config.ctrl_gpio_num = PCNT_INPUT_CTRL_IO; // Control signal input GPIO 35
pcnt_config.unit = PCNT_COUNT_UNIT; // Unidade de contagem PCNT - 0
pcnt_config.channel = PCNT_COUNT_CHANNEL; // PCNT unit number - 0
pcnt_config.counter_h_lim = PCNT_H_LIM_VAL; // Maximum counter value - 20000
pcnt_config.pos_mode = PCNT_COUNT_INC; // PCNT positive edge count mode - inc
pcnt_config.neg_mode = PCNT_COUNT_INC; // PCNT negative edge count mode - inc
pcnt_config.lctrl_mode = PCNT_MODE_DISABLE; // PCNT low control mode - disable
pcnt_config.hctrl_mode = PCNT_MODE_KEEP; // PCNT high control mode - won't change counter mode
pcnt_unit_config(&pcnt_config); // Initialize PCNT unit

pcnt_counter_pause(PCNT_COUNT_UNIT); // Pause PCNT unit
pcnt_counter_clear(PCNT_COUNT_UNIT); // Clear PCNT unit

pcnt_event_enable(PCNT_COUNT_UNIT, PCNT_EVT_H_LIM); // Enable event to watch - max count
pcnt_isr_register(pcnt_intr_handler, NULL, 0, NULL); // Setup Register ISR handler
pcnt_intr_enable(PCNT_COUNT_UNIT); // Enable interrupts for PCNT unit

pcnt_counter_resume(PCNT_COUNT_UNIT); // Resume PCNT unit - starts count
}

Good luck
Hi, thanks for your reply. I was busy in other works in the past months. So could not read all your reply. Now while studying the above code, I understand that for getting duty cycle, one need to know the timing of rising edge and total time for rising and falling edge.
But sorry to say that from the above code I could not figured out where to start.. Please let me know how to calculate DUTY CYCLE from this code.

User avatar
jgustavoam
Posts: 146
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: Best Frequency Meter ever made with ESP32 - awesome!

Postby jgustavoam » Fri Jul 05, 2024 2:55 pm

Tuku, (highlighted in the previous answer)

You must use two counters. One to count the positive part of the pulse and one for the negative part.

pcnt_config.pos_mode = PCNT_COUNT_INC; // PCNT positive edge count mode - inc
pcnt_config.neg_mode = PCNT_COUNT_INC; // PCNT negative edge count mode - inc
Retired IBM Brasil
Electronic hobbyist since 1976.

rin67630
Posts: 122
Joined: Sun Mar 11, 2018 5:13 pm

Re: Best Frequency Meter ever made with ESP32 - awesome!

Postby rin67630 » Fri Jul 05, 2024 6:14 pm

Isn't the very purpose if a frequency meter to be precise, very precise?
If you use a microcontroller with a wonky timebase, what accuracy do you expect?

User avatar
jgustavoam
Posts: 146
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: Best Frequency Meter ever made with ESP32 - awesome!

Postby jgustavoam » Sun Jul 07, 2024 12:36 pm

Hi RIn67630,
Within the features presented, the ESP32 oscillator is very stable. I don't know how you came to the conclusion that the oscillator is unstable. I did numerous tests with the frequency meter on my oscilloscope.

I'm not comparing it to a professional frequency meter. Furthermore, this project can be calibrated with another precision meter.Thank you for your interest in the project.
Retired IBM Brasil
Electronic hobbyist since 1976.

rin67630
Posts: 122
Joined: Sun Mar 11, 2018 5:13 pm

Re: Best Frequency Meter ever made with ESP32 - awesome!

Postby rin67630 » Sun Jul 07, 2024 3:16 pm

jgustavoam wrote:
Sun Jul 07, 2024 12:36 pm
Within the features presented, the ESP32 oscillator is very stable...
It is unfortunately not. Its crystal deviates by up to several minutes a day.

If your aim is to make the "Best Frequency Meter ever made with ESP32" you ought to auto-calibrate your readings.
The most precise way to do it for only a few bucks is to use a GPS module like that one:
https://www.ebay.de/itm/401756103486
You can use it to set your ESP clock during setup() (that is very convenient)
You can also let your internal clock run for a day and compare at the end of the day with the precise GPS time.
That will give you a good measure if your oscillator deviation. You can then store the correction and compensate your frequency readings accordingly.
Only then, you could come with "Best Frequency Meter ever made with ESP32".

Regards...
Laszlo

P.S. it provides also an atomic-clock-grade 1-second pulse output, that you can use as a timebase.

boarchuz
Posts: 576
Joined: Tue Aug 21, 2018 5:28 am

Re: Best Frequency Meter ever made with ESP32 - awesome!

Postby boarchuz » Sun Jul 07, 2024 4:30 pm

rin67630 wrote:
Sun Jul 07, 2024 3:16 pm
It is unfortunately not. Its crystal deviates by up to several minutes a day.
The hardware design guidelines recommend <=10ppm for the 40MHz XTAL, so you can be confident that any espressif module and any reputable 3rd party product will have much less error than "several minutes a day".

In sleep modes, this is usually powered down and the RTC slow clock (using internal RC oscillator) keeps track of time, which can lead to the error of the magnitude you cited. I doubt sleep modes are in play in this particular application, though, so timing should be very accurate.

User avatar
jgustavoam
Posts: 146
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: Best Frequency Meter ever made with ESP32 - awesome!

Postby jgustavoam » Mon Jul 08, 2024 2:31 am

I agree with Boarchuz.
I think maybe Rin67630 is mistaken.
I do not recommend comparing the internal RTC circuit with the ESP32 counter circuits.
Retired IBM Brasil
Electronic hobbyist since 1976.

Who is online

Users browsing this forum: No registered users and 29 guests