Best Frequency Meter ever made with ESP32 - awesome!

User avatar
jgustavoam
Posts: 165
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 Aug 04, 2024 2:42 am

ESP32 FREQUENCY METER - 2 CHANNELS - Version 3 - Checking out

Testing my old Crystal Oscillators - 20.000 MHz and 14.318180 MHz. Very Good!

Breadboards and long wires are susceptible to noise and can interfere with measurements. The ideal is to assemble the entire circuit on a printed circuit board and use shielded cables on the frequency meter inputs. But I will wait for other new implementations to the project.

The two outputs of oscillators 0 and 1 are not being used in this test.


ESP32 Freq Meter 2CH board 20 MHz.JPG
ESP32 Freq Meter 2CH board 20 MHz.JPG (159.02 KiB) Viewed 4129 times
Retired IBM Brasil
Electronic hobbyist since 1976.

OSCPUDEV
Posts: 20
Joined: Fri Jun 23, 2023 7:02 pm

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

Postby OSCPUDEV » Tue Aug 27, 2024 2:56 am

Have you considered using the Capture Submodule of MCPWM module instead of the PCNT module? You can set the resolution down to 12.5ns (80MHz). There are 3 channels available, and they are fully hardware driven. An edge change on a GPIO causes the module to grab the current 32 bit timer value and store it in a register. Either edge polarity is supported. The timer is dedicated for the MCPWM module, so it's separate from all other timers. The interrupt doesn't need to be so fast because all it has to do is get the already latched value and subtract it from the last saved value to give you an exact time. The value being latched by hardware gets rid of jitter and lets you handle it anytime afterwards.

User avatar
jgustavoam
Posts: 165
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 Aug 27, 2024 3:51 pm

Hi OSCPUDEV,

We hadn't thought of this alternative of using the MCPWM peripheral in our project. Maybe at the time of the project's development, there wasn't much documentation available. But thanks for the tips!
Retired IBM Brasil
Electronic hobbyist since 1976.

DavyBoy
Posts: 2
Joined: Mon Sep 09, 2024 11:53 pm

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

Postby DavyBoy » Tue Sep 10, 2024 12:13 am

Thanks a lot for this !!!

The meter was working well using the embedded oscillator, however when I tried to connect a function generator, I was ready 0 (when negative connected to the esp32 ground). After some research, someone had a similar issue and added a thermistor, I did the same and now it works.

Now I am trying to increase the sampling rate because I want to use this device for an overspeed protection for my miniature turbine engine. I only need to read up to 30 kHz so I don't need the full 40MHz capability.

The problem is, if I change the sampling_time from 1,000,000 to 100,000, or 10,000, or 1000.... the frequency read is affected (10, 100, 1000 times lower)

Exemple:
Simulated frequence: 12,600Hz
Sampling time: 1,000,000
Frequency read: 12,600Hz

Simulated frequence: 12,600Hz
Sampling time: 100,000
Frequency read: 1,260Hz

Anyone can help me on this??

Thanks in advance
Dave

User avatar
jgustavoam
Posts: 165
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 Sep 10, 2024 12:21 pm

Hi DavyBoy,

First of all, this frequency meter only accepts digital signals up to 3.3V. Do not use analog signals directly. You will have to use a circuit to adapt analog signals. I may implement this in the future.

"Now I am trying to increase the sampling rate because I want to use this device for an overspeed protection for my miniature turbine engine. I only need to read up to 30 kHz so I don't need the full 40MHz capability."!
You don't need to change the sample rate to measure signals up to 30KHz. The accuracy of this frequency meter for this type of frequency is great!

What is your real need? To increase the frequency of measurements? For example - X measurements/second.
Retired IBM Brasil
Electronic hobbyist since 1976.

DavyBoy
Posts: 2
Joined: Mon Sep 09, 2024 11:53 pm

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

Postby DavyBoy » Thu Sep 12, 2024 10:01 pm

Hi Gustavo,

First, thanks a lot for taking the time to answer, I really appreciate.

I just realized that you are living in Belo Horizonte, I have been there couple of time (as well as Rio), we have a turbine engine test cell at Pratt & Whitney do Brasil (at IAS).

I was trying to reduce the value at sample_time, but it was affecting the reading. I tried Anas's suggestion, to modify the calculation frequency=((pulses + (multpulses * overflow)) / 2.0) * 1e6/sample_time and it works as expected.

However, I'am still facing two issues:

- the more I increase the sampling rate (more and more sample within 1 second), the more the reading is unstable. I don't know if it is hardware limitation or it can be tweek by software.
- the reading is pretty stable using the internal oscillator, but when I connect a function generator (amplitude 3v), Positive to GPIO34 and negative to GND, I read 0. If I disconnect the negative, it bounce to 0 and something, when I keep the probe in my hand, I read unstable frequency. I added a thermistor and 10k resistors between the positive and GPIO34 (based on a similar issue someone else had) and it works now, but not as stable as with the oscillator. Any idea why I cannot connect my frequency generator directly to GPIO34 and GND? For information, I use a precise calibrator Beamex MC6.

Thanks a lot for your time
Dave

User avatar
jgustavoam
Posts: 165
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 Sep 13, 2024 2:27 am

Hi Davyboy,

The ESP32 frequency counter takes approximately 1 second (1 million microseconds) to make one measurement.

Code: Select all

uint32_t sample_time = 1000000;  // Sample time of 1 second to count pulses
To modify the program for frequency measurements up to 50 KHz, make the following changes:

Code: Select all

uint32_t sample_time = 100000;  //  - Sample time of 0,1 second to count pulses
Calculation of frequency inside the void loop:

Code: Select all

frequency = (pulses + (multPulses * overflow)) * 5;    // Calculation of frequency
The count must be multiplied by 10 and divided by 2. Therefore multiplied by 5.

Some time is spent adjusting the oscillator frequency. If you are not going to use the ESP32 oscillator, remove this part of the program (inside void loop) :

Code: Select all

String inputString = "";  // clear temporary string
  osc_freq = 0;             // Clear oscillator frequency
  while (Serial.available()) {
    char inChar = (char)Serial.read();  // Reads a byte on the console
    inputString += inChar;              // Add char to string
    if (inChar == '\n')                 // If new line (enter)
    {
      osc_freq = inputString.toInt();  // Converts String into integer value
      inputString = "";                // Clear string
    }
  }
  if (osc_freq != 0)  // If some value inputted to oscillator frequency
  {
    init_osc_freq();  // reconfigure ledc function - oscillator
  }

Using the frequency generator with the Beamex MC6 (read the manual - page 26):
https://www.beamex.com/calibrators/beam ... #resources

- Adjust the amplitude to 3V. If it doesn't work, increase it to 4V.
- Select waveform - square
- Select Duty Cycle - 50%
- Configure the frequency (up to 50KHz)

I don't know what the output impedance of the Beamex is. Test it by inserting a 10 Kohm resistor between the generator output pins. And connect this output to the ESP32 Frequency Meter input.

I recommend that you measure the signal generated by this MC6 with an oscilloscope, to make sure it is correct.
See previous posts for a square waveform in scope.
Retired IBM Brasil
Electronic hobbyist since 1976.

wukongxuetang
Posts: 1
Joined: Tue Oct 15, 2024 4:45 am

How to generate a float frequency by modifying the code, such as 24.12HZ

Postby wukongxuetang » Tue Oct 15, 2024 4:47 am

How to generate a float frequency by modifying the code, such as 24.12HZ?

User avatar
jgustavoam
Posts: 165
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 Oct 15, 2024 1:33 pm

Hi Wukon,
In this project the minimum resolution of the oscillator is 1.
A floating value is not allowed.

if (resolution < 1) resolution = 1; // set min resolution

Code: Select all

//----------------------------------------------------------------------------
void init_osc_freq()  // Initialize Oscillator to test Freq Meter
{
  resolution = (log(80000000 / osc_freq) / log(2)) / 2;  // Calc of resolution of Oscillator
  if (resolution < 1) resolution = 1;                    // set min resolution
  Serial.println(resolution);                            // Print
  mDuty = (pow(2, resolution)) / 2;                      // Calc of Duty Cycle 50% of the pulse
  Serial.println(mDuty);                                 // Print

  ledc_timer_config_t ledc_timer = {};  // LEDC timer config instance

  ledc_timer.duty_resolution = ledc_timer_bit_t(resolution);  // Set resolution
  ledc_timer.freq_hz = osc_freq;                              // Set Oscillator frequency
  ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE;               // Set high speed mode
  ledc_timer.timer_num = LEDC_TIMER_0;                        // Set LEDC timer index - 0
  ledc_timer_config(&ledc_timer);                             // Set LEDC Timer config

  ledc_channel_config_t ledc_channel = {};  // LEDC Channel config instance

  ledc_channel.channel = LEDC_CHANNEL_0;           // Set HS Channel - 0
  ledc_channel.duty = mDuty;                       // Set Duty Cycle 50%
  ledc_channel.gpio_num = LEDC_HS_CH0_GPIO;        // LEDC Oscillator output GPIO 33
  ledc_channel.intr_type = LEDC_INTR_DISABLE;      // LEDC Fade interrupt disable
  ledc_channel.speed_mode = LEDC_HIGH_SPEED_MODE;  // Set LEDC high speed mode
  ledc_channel.timer_sel = LEDC_TIMER_0;           // Set timer source of channel - 0
  ledc_channel_config(&ledc_channel);              // Config LEDC channel
}

//----------------------------------------------------------------------------------
Retired IBM Brasil
Electronic hobbyist since 1976.

henrik04
Posts: 2
Joined: Sat May 16, 2020 6:06 am

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

Postby henrik04 » Sat Nov 09, 2024 6:15 am

Look like a great project.. but doesn't compile using arduino 1.8.18:


Arduino: 1.8.18 (Windows 10), Board: "ESP32 Wrover Module, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), QIO, 80MHz, 921600, None, Disabled"

D:\_S\F_METER_I2C\F_METER_I2C.ino: In function 'void inicializa_frequencimetro()':

F_METER_I2C:151:3: error: 'gpio_pad_select_gpio' was not declared in this scope; did you mean 'esp_rom_gpio_pad_select_gpio'?

151 | gpio_pad_select_gpio(OUTPUT_CONTROL_GPIO); // Define o port decontrole

| ^~~~~~~~~~~~~~~~~~~~

| esp_rom_gpio_pad_select_gpio

F_METER_I2C:159:3: error: 'gpio_matrix_in' was not declared in this scope

159 | gpio_matrix_in(PCNT_INPUT_SIG_IO, SIG_IN_FUNC226_IDX, false); // Direciona a entrada de pulsos

| ^~~~~~~~~~~~~~

F_METER_I2C:160:3: error: 'gpio_matrix_out' was not declared in this scope; did you mean 'gpio_iomux_out'?

160 | gpio_matrix_out(IN_BOARD_LED, SIG_IN_FUNC226_IDX, false, false); // Para o LED do ESP32

| ^~~~~~~~~~~~~~~

| gpio_iomux_out

exit status 1

'gpio_pad_select_gpio' was not declared in this scope; did you mean 'esp_rom_gpio_pad_select_gpio'?

Who is online

Users browsing this forum: No registered users and 74 guests