
How it works
This is ESP-IDF's rotary_encoder example on the S31's pulse counter (PCNT). An encoder has two contacts, A and B, that open and close a quarter cycle apart, so the order of their edges gives the direction of rotation. The program sets up two PCNT channels on GPIO0 (A) and GPIO2 (B). Each counts edges on one input while the level of the other decides whether to count up or down.
The contacts switch to ground through the common pin, so something must hold each line high while its contact is open. The program enables the chip's internal pull-ups, and the bench also adds two 10 kΩ resistors from 3V3, so each line sits near 3.3 V while its contact is open. A glitch filter ignores pulses shorter than 1 µs. The counter has limits of -100 and 100, where it returns to 0, and watch points at -100, -50, 0, 50 and 100 that log an event when reached. Once a second the program prints the current pulse count.
What to watch
Enable verbose in Serial to see a Pulse count line every second. Select the encoder and turn it: one direction raises the count, the other lowers it, and Watch point event, count: 50 appears when the count reaches 50. The setup lines run from install pcnt unit to start pcnt unit.
What's on the bench
- 2× Resistor
- Encoder
- ESP32-S31-Function-CoreBoard-1
How it's wired
- ESP32-S31-Function-CoreBoard-1 · g0→Encoder · a
- ESP32-S31-Function-CoreBoard-1 · 3v3→Resistor · a
- Resistor · b→Encoder · a
- ESP32-S31-Function-CoreBoard-1 · g2→Encoder · b
- ESP32-S31-Function-CoreBoard-1 · 3v3→Resistor · a
- Resistor · b→Encoder · b
- ESP32-S31-Function-CoreBoard-1 · gnd→Encoder · com
This project runs prebuilt firmware on the emulated board; open it in the simulator to watch it work and inspect every part live.
Try this
- Keep turning forward past 50 until the count reaches 100. A watch point event fires and the counter starts again from 0.
- Turn backward below 0. The count goes negative, and the watch points at 0 and -50 log on the way down.
- Turn one step at a time and hover over A and B between steps. An open contact reads near 3.3 V, and a closed contact reads near 0 V. The encoder can stop with either contact closed.



