If you want to count pulses while in deep sleep youuse the ULP. Code on the ULP continues to execute when the board wakes up and goes to normal power mode. So when it is awake, it will still run the counter on the ULP processor unless you stop the ULP periodic wake up timer, ULP will keep waking up and running while the main CPU is active.

As you gave already checked with this example , it should be pretty close to what you need. The only difference seems to be that the example is set to wake up after a given number of pulses, rather than a fixed amount of time. However it should be easy to change that, by enabling deep sleep wake up from timer.
For the Arduino you could check
Some additional info:
ULP doesn't have GPIO interrupts. So you use deep sleep wake stub (small piece of code which runs immediately after deep sleep, prior to loading application from flash into RAM) you can increment the pulse counter variable, and go to sleep again. This way you can get low power consumption (~5uA) between pulses and moderate power consumption while running the wake stub (around 13mA), for a very short time.
So its up to you to experiment with your specific scenario.

Answer from Codebreaker007 on Stack Overflow
🌐
ESP32 Forum
esp32.com › viewtopic.php
How to use "Pulse Counter" module usage under Arduino framework - ESP32 Forum
March 10, 2020 - #include "driver/pcnt.h" // Biblioteca de pulse count #define PCNT_FREQ_UNIT PCNT_UNIT_0 // Unidade de Pulse Count 0 #define PCNT_H_LIM_VAL 10000 // Limite superior de contagem 32767 #define PCNT_INPUT_SIG_IO 4 // Pulse Input GPIO 4 int16_t contador = 0; // Contador de pulsos - valor max 65536 int contadorOverflow; // Contador de overflow do Contador de Pulsos float frequencia = 0; // Frequencia medida String unidade; // Unidade de medida da escala unsigned long tempo; // base de tempo da medida dos pulsos int prescaler; // Divisor de frequencia do Timer bool contadorOK = false; pcnt_isr_handl
🌐
GitHub
github.com › mikegofton › ESP32PulseCounter
GitHub - mikegofton/ESP32PulseCounter: Arduino library for the 16 bit hardware counters available on the ESP32 · GitHub
Arduino library for the 8 x signed 16 bit hardware counters available on the ESP32. This library was developed on PlatformIO. The PCNT (Pulse Counter) module is designed to count the number of rising and/or falling edges of an input signal.
Starred by 4 users
Forked by 5 users
Languages   C++
Discussions

ESP 32 Pulse Counter
I don't use freertos in my projects for now. To count pulses and calc frequency I use just interrupts. attachInterrupt(ENCODER_IN_PIN, ENCODER_ISR, CHANGE); uint32_t timeBetweenPulses, TimePulse; void IRAM_ATTR ENCODER_ISR() { timeBetweenPulses = micros() - TimePulse  ; TimePulse = micros(); } With 1000000L/TimeBetweenPulses you can have the frequency. More on reddit.com
🌐 r/esp32
18
1
May 21, 2024
arduino - how to use esp32 ulp interrupt pulse counter and periodic wake up deepsleep mode - Stack Overflow
I am trying to measure power usage using dds353 kWh meter. This meter has a pulse output. I am interested in using the esp32 since I can periodically send the data over the internet to nodered dash... More on stackoverflow.com
🌐 stackoverflow.com
Implementing the Pulse Counter - Mixing Arduino and ESP Libraries
Take a look at ESP32Encoder in the Arduino library manager. I wrote it to use the pulse counter as a half-quadrature encoder library. More on reddit.com
🌐 r/esp32
1
2
March 27, 2019
ESP32 pulse counter read/write to EEPROM
hello folks I am a newbie here, and i am working on my first project with an ESP32 DEVkit. My project is to count pulse that is generated from a pulse generator board using an ESP32 board and it displays the count on the LCD 16x2 (JHD 161A). i was successful in getting the pulse to display ... More on forum.arduino.cc
🌐 forum.arduino.cc
8
0
May 20, 2020
🌐
Arduino Forum
forum.arduino.cc › projects › programming
ESP32, Pulse counter (PCNT) & interrupts question - Programming - Arduino Forum
December 12, 2024 - Hello, I have a code, which uses PCNT to measure a frequency. The code uses interrupts to catch counter overflow (counter is 16bit only). Now I need to run 4 counters in parallel but it is not clear what to do with inte…
🌐
PlatformIO
registry.platformio.org › libraries › mike-gofton › ESP32PulseCounter
mike-gofton/ESP32PulseCounter: library for ESP32 hardware pulse counters
December 4, 2022 - Arduino library for the 8 x signed 16 bit hardware counters available on the ESP32. This library was developed on PlatformIO. The PCNT (Pulse Counter) module is designed to count the number of rising and/or falling edges of an input signal.
🌐
Reddit
reddit.com › r/esp32 › esp 32 pulse counter
r/esp32 on Reddit: ESP 32 Pulse Counter
May 21, 2024 -

Hi ESP folks , I used the pcnt module to calculate the pulses on every falling edge to get the frequency. I am using a single pcnt_unit_handle in a sole pcnt_channel_handle. But the problem is, even when I send the pulses at same frequency, The pulse counter value is fluctuating between -2 to +2 pulses. I am calculating the number of pulses received per 500ms, using freeRTOS task which I configured to run the task for 500ms,but here also I am getting the problem, this task sometimes runs for 550ms.providing the glitch of about -5ms to +50ms. Can you tell me an alternate way for acquiring pulses to get the frequency from it using freeRTOS task.At the end what I want is to calculate the frequency from the pulses and acquire the RPM from it in every 500ms or even less time.

Top answer
1 of 3
1

If you want to count pulses while in deep sleep youuse the ULP. Code on the ULP continues to execute when the board wakes up and goes to normal power mode. So when it is awake, it will still run the counter on the ULP processor unless you stop the ULP periodic wake up timer, ULP will keep waking up and running while the main CPU is active.

As you gave already checked with this example , it should be pretty close to what you need. The only difference seems to be that the example is set to wake up after a given number of pulses, rather than a fixed amount of time. However it should be easy to change that, by enabling deep sleep wake up from timer.
For the Arduino you could check
Some additional info:
ULP doesn't have GPIO interrupts. So you use deep sleep wake stub (small piece of code which runs immediately after deep sleep, prior to loading application from flash into RAM) you can increment the pulse counter variable, and go to sleep again. This way you can get low power consumption (~5uA) between pulses and moderate power consumption while running the wake stub (around 13mA), for a very short time.
So its up to you to experiment with your specific scenario.

2 of 3
0

You can use Pulse Counter(PCNT) feature in ESP32 to count the number of pulse in background, Understanding by using same you can able to do some periodic wake-up and read the count.. Its also possible to configure event when number of counts reached certain threshold and had lot of options,

For get information and available Interfaces and API's for Pulse Counter(PCNT) please follow below link, https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/pcnt.html

Initially I faced lot of issue to make Pulse Counter(PCNT) work in Adrino IDE for ESP-32, After multiple attempt I make it working, And same sample code is uploaded in GitHub for reference. I have not use all the API's in the official documentation but but used few of them and are working..

I have created sample program for a water flow meter, there also we use to get pulse which needs to count to measure the water flow rate, understanding simile to kWh meter.

GitHub Sample code Path:- https://github.com/Embedded-Linux-Developement/Arduino_Sample_Programs/tree/main/ESP_32/Water_Flow_Pulse_counter_WithOut_Interrupt_Using_PCNT

I have not placing the code here, because its there in GitHub and not directly for the asked question, but simile one and can use it. Its a working code I tested in HW.

Hopes Its helpful, Regards, Jerry James

🌐
Medium
medium.com › @ricardo_66479 › counting-pulses-with-esp32-70b0d8dd2829
Counting pulses with ESP32. This is an article to show how to count… | by Programonauta | Medium
March 5, 2019 - unsigned long counterPulses;unsigned ... &count); counterPulses = multPulses * PCNT_H_LIM_VAL + count; printf("Current counter value :%lu\n", counterPulses); } } To calculate the actual number of pulses, multiply the “multiplier” multPulses by limit PCNT_H_LIM_VAL and sum the current counter count...
Find elsewhere
🌐
Espressif
docs.espressif.com › projects › esp-idf › en › stable › esp32 › api-reference › peripherals › pcnt.html
Pulse Counter (PCNT) - ESP32 - — ESP-IDF Programming Guide v6.0.2 documentation
The PCNT (Pulse Counter) module is designed to count the number of rising and/or falling edges of input signals. The ESP32 contains multiple pulse counter units in the module. [1] Each unit is in effect an independent counter with multiple channels, where each channel can increment/decrement ...
🌐
Reddit
reddit.com › r/esp32 › implementing the pulse counter - mixing arduino and esp libraries
r/esp32 on Reddit: Implementing the Pulse Counter - Mixing Arduino and ESP Libraries
March 27, 2019 -

I've been putting together a project where I'm interested in counting the number of pulses given by a pulse train from a LMT01 chip to determine body temperature. Currently, I have implemented a BLE client and ADC channels utilizing the Arduino libraries, but I'm having issues finding documentation for the pulse counter in Arduino. I've only found the documentation for the ESP pulse counter library: https://github.com/espressif/esp-idf/blob/da9096682/components/driver/include/driver/pcnt.h

This leads me to a few questions I have in how I can approach this:

  1. Does anyone know where I could find a good reference to pulse counter ESP32 Arduino code?

  2. Can you use Arduino and ESP libraries at the same time?

  3. Will the ESP32 have enough space in flash (4MB?) to store both libraries and include instruction space?

🌐
Arduino Forum
forum.arduino.cc › projects › general guidance
ESP32 pulse counter read/write to EEPROM - General Guidance - Arduino Forum
May 20, 2020 - hello folks I am a newbie here, and i am working on my first project with an ESP32 DEVkit. My project is to count pulse that is generated from a pulse generator board using an ESP32 board and it displays the count on the LCD 16x2 (JHD 161A). i was successful in getting the pulse to display on the LCD and it works fine, but the problem i seem to be facing is when power shuts down/Fails, the pulse resets to ZERO. my requirement is to save the pulse right before power fails and with a few reads ...
🌐
Espressif
docs.espressif.com › projects › esp-idf › en › latest › esp32 › api-reference › peripherals › pcnt.html
Pulse Counter (PCNT) - ESP32 - — ESP-IDF Programming Guide latest documentation
January 4, 2017 - The PCNT (Pulse Counter) module is designed to count the number of rising and/or falling edges of input signals. The ESP32 contains multiple pulse counter units in the module. [1] Each unit is in effect an independent counter with multiple channels, where each channel can increment/decrement ...
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Measure time between pulses with ESP32 and interrupt - Programming - Arduino Forum
January 28, 2023 - Hello All, I'm trying to measure the time between two pulses with a ESP32 Wroom with interrupts. I have seen some examples but all use the TomerOne.h library and that one I cant use with an ESP32. So I get a bit stuck on how to fix this. Previously I did the same with a MEGA with some help here on the forum but with the MEGA I used a pin read to see which pin had changed and computed the time between change.
🌐
GitHub
github.com › DevX8000 › ESP32-PCNT-Arduino-Example
GitHub - DevX8000/ESP32-PCNT-Arduino-Example: Example of using the ESP32's hardware pulse counter to find the RPM of a PC fan from it's tachometer output.
Example of using the ESP32's hardware pulse counter to find the RPM of a PC fan from it's tachometer output. - DevX8000/ESP32-PCNT-Arduino-Example
Starred by 22 users
Forked by 8 users
Languages   C++ 100.0% | C++ 100.0%
🌐
ESP32 Forum
esp32.com › viewtopic.php
PCNT (Pulse Counter) example (arduino ide) - ESP32 Forum
July 17, 2021 - I googled: "esp32 pulse count" This looks promising: https://docs.espressif.com/projects/esp ... /pcnt.html What did you look for? Tom · IT Professional, Maker Santiago, Dominican Republic ... Yes I looked at it, but I am not able to understand it properly. could you please provide a sample program for arduino ide in which I can connect a encoder using PCNT along with hardware pin schematic?
🌐
GitHub
github.com › DavidAntliff › esp32-freqcount
GitHub - DavidAntliff/esp32-freqcount: ESP32 Frequency Counter using Pulse Counter and Remote Control modules · GitHub
This ESP32 component facilitates accurately measuring the frequency of square pulses on a GPIO using Pulse Counter, RMT and Interrupt.
Starred by 71 users
Forked by 22 users
Languages   C 67.8% | C++ 31.2%
🌐
Espressif
docs.espressif.com › projects › esp-idf › en › v5.1 › esp32 › api-reference › peripherals › pcnt.html
Pulse Counter (PCNT) - ESP32 - — ESP-IDF Programming Guide v5.1 documentation
The PCNT (Pulse Counter) module is designed to count the number of rising and/or falling edges of input signals. The ESP32 contains multiple pulse counter units in the module. 1 Each unit is in effect an independent counter with multiple channels, where each channel can increment/decrement ...
🌐
FreqCountESP
kapraran.github.io › FreqCountESP
FreqCountESP | A frequency counter library for esp32
A frequency counter library for esp32. It counts the numbers of pulses on a specified pin during a fixed time frame using native interrupts and timers. It only supports one instance per sketch for now. To use this library you have to import it into Arduino IDE as follows:
🌐
NodeMCU Documentation
nodemcu.readthedocs.io › en › dev-esp32 › modules › pulsecnt
Pulse Counter Module - NodeMCU Documentation
April 7, 2019 - Event will be PCNT_EVT_THRES_0 (Threshold 0 hit), PCNT_EVT_THRES_1 (Threshold 1 hit), PCNT_EVT_L_LIM (Minimum counter value), PCNT_EVT_H_LIM (Maximum counter value), or PCNT_EVT_ZERO (counter value zero event) isDebug Optional. Turn on extra logging by passing in true. ... -- Example Pulse Counter for 1 channel with polling of pulse count pinPulseInput = 2 llim = -32768 hlim = 32767 pcnt = pulsecnt.create(7) -- Use unit 7 (0-7 are allowed) pcnt:chan0Config( pinPulseInput, --pulse_gpio_num pulsecnt.PCNT_PIN_NOT_USED, --ctrl_gpio_num If no control is desired specify PCNT_PIN_NOT_USED pulsecnt.PC
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Pulse Counting - Programming - Arduino Forum
April 24, 2020 - Greetings Arduino community! I am relatively new to this community however I have...how do you say...been a long time listener...first time caller.... I will come out with it immediately...I am more hardware than software and have been using this time under quarantine to dig deeper into the ...
🌐
Reddit
reddit.com › r/esp32 › using the internal pulse counter?
r/esp32 on Reddit: Using the internal Pulse Counter?
July 24, 2019 -

Hi, I have two identical sensors side by side and both send 5vdc pulses from a hall sensor at around 1500Hz.

I need to count these pulses then add both counts together to display a single value on an LCD once per second. I have been reading the Espressif docs, but they are way beyond my skill set. I did read "the pulse duration should be longer than one APB_CLK cycle (12.5 ns)," is that going to be within the incoming Hz?

Could someone please point me at, or give me some code snippets to show how this might be done.

Thanks

ETA: The pulses will vary from zero to about 1500Hz up and down and at any time, much like a car a speedometer sender sensor.