Counting pulses with ESP32

Programonauta
3 min readMar 1, 2019

--

This is an article to show how to count pulses generated by a flow sensor.

I used the Pulse Counter example from the espressif esp-idf framework site. You can find the example on the espressif Github.

But I’d like to use a sensor flow to measure water flow. I bought a very cheap sensor. And my test used a ESP32 TTGO board from WeMos, as depicted in the Figure:

Flowmeter with TTGO Board

Changes made on example program

The example program uses GPIO4 as Pulse Input PIN. The pulse is generated internally by a pulse generator on GPIO18.

To demonstrate that pulse can increase or decrease the counting, they use a signal on GPIO5. If GPIO5 is connected to GND, the count value will be decreased, if leave floating with internal pull up, the count will increase.

Remove Pulse Generator and Control Setup

All references do PWM generator was removed. On the original code, try to find ledc .

Remove reverse counting

Makes no sense a reverse counting in a flow sensor, so all commands and variables that define low limits, and controls to make counter decrease values was removed.

In the events setup,( pcnt_example_init function), was removed the events threshold 0 and Low Limit reached.

Make the counter bigger

The type of variable used to store the pulses counting is intl16_t , so to assure there the counter will not reach the upper limit, I create an unsigned long variable, a multiplier and set upper limit to 1000. So each time the limit is reached the multiplier is increased.

You can find the complete code on my Github.

unsigned long counterPulses;unsigned long multPulses = 0;...
...
} else {
pcnt_get_counter_value(PCNT_TEST_UNIT, &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

Run the test

Connections

This board has few documentation in the Internet, but the pinout is enough to the test:

TTGO WeMos Pinout

Connection

PIN 19 → 5V

PIN 50 → GND

PIN 28 → GPIO4

Connection TTGO Board — Flow sensor

Finally running the program

  1. Connect your board on the computer using serial port.
  2. Compile and flash the code
idf.py build
idf.py -p <your serial port> flash

3. View the results using the serial monitor

When the sensor identifies the flow (you can blow it) the count will start.

I (0) cpu_start: App cpu up.
I (205) heap_init: Initializing. RAM available for dynamic allocation:
I (212) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (218) heap_init: At 3FFB3308 len 0002CCF8 (179 KiB): DRAM
I (224) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (231) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (237) heap_init: At 40088BF0 len 00017410 (93 KiB): IRAM
I (243) cpu_start: Pro cpu start user code
I (261) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Current counter value :0
Current counter value :0
Current counter value :78
Current counter value :217
Current counter value :361
Event PCNT unit[0]; cnt: 500
THRES1 EVT
Current counter value :614
Current counter value :890
Event PCNT unit[0]; cnt: 0
H_LIM EVT
ZERO EVT
Current counter value :1068
Current counter value :1077
Current counter value :1293
...

I defined the High Limit in 1000 and Threshold 1 in 500, so you can notice in the monitor the register of these events.

--

--

Programonauta

Engenheiro de Computação e um apaixonado por tecnologia. Atualmente procurando as conexões entre blockchain, IoT e Inteligência Artificial.