arduino esp32 pcnt example

Arduino ESP32 Pulse Counter (PCNT) Tutorial

By Anthony Martin
AD

Welcome to our comprehensive guide on using the Arduino ESP32's Pulse Counter (PCNT) functionality, an essential component for counting pulses or measuring Rotary Encoders. This tutorial will delve into the intricacies of the ESP32 PCNT, its benefits, and practical examples to help you master this feature.

The ESP32, a low-cost, low-power system on a chip microcontroller with integrated Wi-Fi and dual-mode Bluetooth, is a game-changer in the Internet of Things (IoT) landscape. Its Pulse Counter functionality enhances this powerhouse by offering a simple and efficient way to count pulses from sensors or peripherals.

The ESP32 PCNT: An Overview

The Pulse Counter on the ESP32 is designed to handle counting events or pulses from various sources like Rotary Encoders, Hall Effect sensors, or other similar sensors. It simplifies the hardware and software design, making it an invaluable tool for Projects such as motor controllers, encoder interfacing, and more.

AD
Simulate ESP32 Projects in VS Code with Wokwi (Beginner Guide!)
Simulate ESP32 Projects in VS Code with Wokwi (Beginner Guide!)
Custom Arduino Library for ESP32 📦⚙️
Custom Arduino Library for ESP32 📦⚙️
10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn

Understanding the ESP32 PCNT unit involves knowing its core components – the eight PCNT pins and the PCNT logic. The former includes pins like PCNT filo, PCNT_miso, PCNT_mosi, etc., while the latter governs the counting operations, thresholds, and event handling.

Configuring the PCNT unit

To start using the PCNT, configure the PCNT unit using the `pcnt_config()` function. This function allows you to set the count direction, threshold, and maximum value, among other parameters. Here's a simple example:

DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
⏱️ ESP32 NTP Time Sync – How to Get Accurate Time via Wi-Fi!
⏱️ ESP32 NTP Time Sync – How to Get Accurate Time via Wi-Fi!
🌐 ESP32 Static IP Address – Assign a Fixed IP in Arduino IDE!
🌐 ESP32 Static IP Address – Assign a Fixed IP in Arduino IDE!

pcnt_config(pcnt_unit, PCNT_CHANNEL_0, PCNT_COUNT_INC, PCNT travaillé_count_edg, 0, 0);

AD

Handling PCNT interrupts

PCNT interrupts allow the ESP32 to respond to specific counting conditions, like when a threshold is exceeded. To set up these interrupts, use `pcnt_isr_service()` and `pcnt_unit.enable_irq()`. Here's how:

ESP32 Capacitive Touch Pins 🤚⚡
ESP32 Capacitive Touch Pins 🤚⚡
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE
How to Send SMS from ESP32
How to Send SMS from ESP32
Control LED on Inernet using Arduino & ENC28J60 Ethernet Module
Control LED on Inernet using Arduino & ENC28J60 Ethernet Module
🔧 Install ESP32 in Arduino IDE – Quick & Easy Setup Guide!
🔧 Install ESP32 in Arduino IDE – Quick & Easy Setup Guide!
ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
ESP32 Pinout Guide: GPIO, Buttons & Ports Explained

pcnt_unit.service();
pcnt_unit.enable_irq(PCNT_INTR Zusammenarbeit);
pcnt_unit.isr_handler_register(pcnt_unit_isrbes, PCNT_INTR_ стало_counted);

AD

ESP32 PCNT in Action: Examples

Now that we've covered the basics let's explore two practical examples demonstrating the ESP32 PCNT.

🌐 Build an ESP32 Web Server to Control LEDs (Step-by-Step Guide!)
🌐 Build an ESP32 Web Server to Control LEDs (Step-by-Step Guide!)
ESP32 Tutorial: ESP32 OTA Update - Wireless Programming with Arduino IDE (Step-by-Step Guide)
ESP32 Tutorial: ESP32 OTA Update - Wireless Programming with Arduino IDE (Step-by-Step Guide)
AZ-Touch ESP running openHASP
AZ-Touch ESP running openHASP
ESP32 Arduino Proteus Ideas: Blink IoT Project Concepts
ESP32 Arduino Proteus Ideas: Blink IoT Project Concepts
ESP32 Tutorials for Beginners
ESP32 Tutorials for Beginners
🤖 Build an AI Attendance System using ESP32! 📡📝
🤖 Build an AI Attendance System using ESP32! 📡📝
ESP32 industrial
ESP32 industrial
How to Power the ESP32 Correctly ⚡
How to Power the ESP32 Correctly ⚡
ESP32-C3 Super Mini Pinout & Specs 📌⚡
ESP32-C3 Super Mini Pinout & Specs 📌⚡
an electronic device is shown in this poster, with instructions on how to use it
an electronic device is shown in this poster, with instructions on how to use it
esp 32 is a machine that can make you an engineer
esp 32 is a machine that can make you an engineer
Mini proiect (33) - Înregistrarea audio cu ESP32 și INMP441 într-un fisier wave, și salvat pe un card microSD
Mini proiect (33) - Înregistrarea audio cu ESP32 și INMP441 într-un fisier wave, și salvat pe un card microSD
Simple ESP32 Arduino Simulation Ideas: Create a Working Calculator
Simple ESP32 Arduino Simulation Ideas: Create a Working Calculator
IOT Made Simple: Playing With the ESP32 on Arduino IDE
IOT Made Simple: Playing With the ESP32 on Arduino IDE

The first example interfaces with a Rotary Encoder, where each rotation Increments or decrements a counter displayed on an OLED screen:

Rotary Encoder Interface

This example uses the Rotary Encoder library and demonstrates how to read and display encoder data using the ESP32 PCNT:

AD

#include "PCNT.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "driver/i2c.h"
#include "Adafruit_SSD1306.h"

#define OLED_ADDRESS           0x3C
#define PCNT_MISO              35
#define PCNT_MOSI              2
#define PCNT_CLK               4

SemaphoreHandle_t xSemaphore;
uint16_t g_count = 0;
uint32_t last interrupt_time;

void IRAM_ATTR pcnt_isr() {
    uint32_t intr_status = PCNT_unit.service();
    if (intr_status & PCNT_INTR_ATTRenable_count)`
        xSemaphoreGiveFromISR(xSemaphore, NULL);
}

void setup_pcnt() {
    PCNT_config_t pin_config = {
        .channel = PCNT_CHANNEL_0,
        .count_dir = PCNT_COUNT_DIR aumentar,
        .pos_mode = PCNT_INTR handsome_count_edg,
        .neg_mode = PCNT_INTR например lassen_count_edg,
        .lctrl_mode = PCNT_INTR-auflösen_count_invert,
        .filter_en = 1,
        .lctrlсной = {
            .lcm_thr = 5,
            .lcm_filter = 0,
            .dt Filter = 0,
        },
        .ount_latchDigIf_train = {0, 0},
        .max_run = 0,
        .ir modes = {0, 0, 0, 0, 0, 0},
    }
AD