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.
ESP 32 Pulse Counter
arduino - how to use esp32 ulp interrupt pulse counter and periodic wake up deepsleep mode - Stack Overflow
Implementing the Pulse Counter - Mixing Arduino and ESP Libraries
ESP32 pulse counter read/write to EEPROM
Videos
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.
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.
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
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:
Does anyone know where I could find a good reference to pulse counter ESP32 Arduino code?
Can you use Arduino and ESP libraries at the same time?
Will the ESP32 have enough space in flash (4MB?) to store both libraries and include instruction space?
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.
1500hz with counting logic only is no problem. Use interrupts on the signal and do an increment. Have timer task do the the addition and another task to display the value.
Here is an (Arduino) tutorial on interrupts: https://techtutorialsx.com/2017/09/30/esp32-arduino-external-interrupts/
PS, you will need do something about the 5v on the hall signals, you cannot connect them directly to the esp32. You can use a level shifter if you want a simple solution.
I wrote a libraty to use the PCNT module as an encoder:
https://github.com/madhephaestus/ESP32Encoder
also availible through the Arduino Library Manager, but it is also usable without arduino since its just a simple class.