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 » 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 microseconds)

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
Last edited by jgustavoam on Fri Aug 09, 2024 2:54 am, edited 1 time in total.
Retired IBM Brasil
Electronic hobbyist since 1976.

sairam59
Posts: 3
Joined: Sun Apr 28, 2024 10:05 am

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

Postby sairam59 » Sat May 04, 2024 5:27 am

I am using PC8574 with address 0 x 27 16 x 2 LCD. I tried the hello world sketch it is displaying the message on LCD but in the frequency counter sketch I am able to see the data on the serial monitor but I get only squares in the LCD display I changed the address in the sketch to 0x27.

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 May 05, 2024 12:36 pm

Hi Sairam,

You must change this line to ON:

#define LCD_I2C_ON // LCD_I2C_ON, if want use I2C LCD (PCF8574)
Retired IBM Brasil
Electronic hobbyist since 1976.

fenderer101
Posts: 1
Joined: Fri May 10, 2024 10:46 am

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

Postby fenderer101 » Fri May 10, 2024 10:56 am

Hello I am trying to use your code to create a tachometer gauge that reads a pulsed signal and displays the output.

The signal to be read by the ESP32 is 2.6v and has pulses of 1m/s. But the problem is the frequency I read in the Serial Monitor is 5 times bigger than the actual frequency. The frequency is correctly displayed on the oscilloscope. (see picture attached). Can you tell me what is wrong?
freq.jpg
freq.jpg (148.17 KiB) Viewed 6130 times

sairam59
Posts: 3
Joined: Sun Apr 28, 2024 10:05 am

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

Postby sairam59 » Sun May 12, 2024 9:35 am

Hi Sairam,

I recommend checking these items
1) Your display is LCD 16 char x 2 lines + I2C PCF8574?
2) Check the I2c Interface lines:
#define I2C_SDA 21 // LCD I2C SDA - GPIO_21
#define I2C_SCL 22 // LCD I2C SCL - GPIO_22
3) Which PCF8574 module are you using? What is the I2C address?
LiquidCrystal_PCF8574 lcd(0x3F);
4) Run the tests with I2C scanner:


My I2C is PCF8574 with 16 x 2 (162A) lcd and the address is set to 0x27 and #define LCD_I2C_ON but still getting only squares but with lcd test and same connection it is working can you please guide me in this matter. I tried the link you provided but could not solve the issue.

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 May 12, 2024 12:06 pm

Hi Fenderer,
Post your code so that the problem can be analyzed.
Retired IBM Brasil
Electronic hobbyist since 1976.

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 » Mon May 13, 2024 10:42 am

Hi Sairam,

"My I2C is PCF8574 with 16 x 2 (162A) lcd and the address is set to 0x27 and #define LCD_I2C_ON but still getting only squares but with lcd test and same connection it is working can you please guide me in this matter"

You need to change this line to address 0x27? You did it?
LiquidCrystal_PCF8574 lcd(0x27);

Check the LCD module VCC. It must be 5V!
And adjust the brightness on the module's protentiometer.
Retired IBM Brasil
Electronic hobbyist since 1976.

ligteltelecom
Posts: 11
Joined: Wed Oct 25, 2023 10:37 pm

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

Postby ligteltelecom » Sat May 25, 2024 12:26 am

Hi, when I set pcnt_config.ctrl_gpio_num = -1, to not use ports 35 and 32 it becomes inaccurate. example I will measure 500khz it shows 550khz

Can someone help me?

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 May 26, 2024 11:36 am

Hi ligteltelecom,

You must use these two pins for the frequency meter to work.
#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

Why don't you want to use them?
Retired IBM Brasil
Electronic hobbyist since 1976.

ligteltelecom
Posts: 11
Joined: Wed Oct 25, 2023 10:37 pm

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

Postby ligteltelecom » Mon May 27, 2024 9:34 pm

jgustavoam wrote:
Sun May 26, 2024 11:36 am
Hi ligteltelecom,

You must use these two pins for the frequency meter to work.
#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

Why don't you want to use them?
Hello, I want to add frequency counter to an existing product and I don't have these ports available and the end customer would have to modify the product's hardware. I'm from Itaúna- MG, close to Belo Horizonte, thank you for your help.

Who is online

Users browsing this forum: No registered users and 119 guests