rotary encoder/Counting every click/10. When the count goes wrong
Counting every click · 10 of 10

When the count goes wrong

Six symptoms and where to look first. No count at all is the supply. A wrong direction is not a fault. A count that fails only when turned quickly is a slow loop(). Two counts a click is the steps-per-click setting, and a button that reads pushed all the time is a sketch testing for LOW.

Six symptoms

When the count goes wrong
What it does
A slow loop(). The polling sketch has to read CLOCK within a quarter of a cycle of its fall, and a turned knob gives it a few milliseconds. Take out delay() and long prints, or move the counting into an interrupt, which runs whatever loop() is doing.

Pick what the encoder is doing. The ringed part of the board is where to look first, and the list beside it is the order to check in.

Nothing counts

The contacts need both ends of the supply. Without VCC the pull-ups have nothing to pull CLOCK up to; without GND the contacts have nothing to close to, so CLOCK never falls. Then the cable: count from the square pad, GND, VCC, NC, BTN, CLOCK, DATA. After that, the pin numbers: CLOCK_PIN and DATA_PIN are GPIO numbers, not positions along the header.

It counts the wrong way

Not a fault. Which contact leads when turned clockwise depends on the part, and this book never claimed it. Swap count++ and count--, or swap the numbers in CLOCK_PIN and DATA_PIN.

It loses count when turned fast

A slow loop(). The polling sketch has to read CLOCK within a quarter of a cycle of each fall, and a brisk turn leaves it a few milliseconds. Why polling misses steps shows it; counting with an interrupt fixes it.

Two or four counts a click

The sketch counts more steps than the part has clicks. The state table counts four steps a cycle and divides by STEPS_PER_CLICK; if your part rests every half-cycle, set it to 2. Count one full turn against the clicks you feel.

It counts on its own

A line with nothing on it reads noise: VCC unconnected leaves CLOCK and DATA floating, and a loose CLOCK or DATA wire leaves your pin with nothing on it. If the count only flickers by one with the knob at rest, the knob is sitting between two clicks with a contact on its edge; turn it firmly into a click.

The button reads pushed all the time, or never

BTN has a pull-down: released is LOW, pushed is HIGH. A sketch copied from a pull-up button board tests for LOW and reads pushed the whole time you are not touching it. INPUT_PULLUP fights the pull-down. And without VCC the switch has nothing to join BTN to, so it never reads HIGH.

When it does not work

How can I test the block without any code?

Wire GND and VCC only, and put a multimeter between CLOCK and GND. It should read VCC with the knob at rest on most clicks, and drop to 0 V at some positions as you turn slowly. BTN should read 0 V, and VCC while you push the knob in.

Which ESP32 pins should I avoid for the encoder?

The flash pins, which stop the board booting if touched, and the strapping pins read at reset, 0, 2, 5, 12 and 15: CLOCK and DATA rest HIGH, which could change how the chip starts. The input-only pins 34 to 39 would work, since the block brings its own pull-ups. GPIO 25, GPIO 26 and GPIO 27 are used here because they have no job at boot.

It worked yesterday and does nothing today.

Look for a wire that has come out of the breadboard. The right-angle header pins lie flat, and turning and pushing the knob can walk the block out of its row. Press it back in, or hold the block down with its mounting holes.

Where this goes next

The rest of the TinkerBlock blocks, each with its own handbook or reference page.

Back to the blocks

Edit this page — content/books/rotary-encoder/when-the-count-goes-wrong.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

Rotary Encoder

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum