Quadrature Decoding with Arduino
I want to understand the internal workings of a Canon Pixma MX340 multi-function inkjet. Right now my focus is on its paper feed motor assembly, and I want to record data reported by a quadrature rotation encoder inside that assembly. I want to track behavior over several seconds, possibly a minute of two, which gets a little unwieldy with a logic analyzer timeline interface. So I thought I should create a tool tailored to my project and I found a promising lead using an ESP32's pulse counter (PCNT) peripheral.
As as I started preparing for the project, thinking through and writing down what I'd need to do, a lot of details felt very familiar in a "wait... I've done this before" way. I had forgotten I've played with quadrature encoders before! A search for "quadrature" on my project notebook (this blog site) found entries on reading the knob on a Toyota audio head unit, an inexpensive knob from Amazon, and investigative detour during Honda audio head unit adventures.
Following my earlier footsteps would be an easier way to go, because the Arduino IDE and Paul Stoffregen's quadrature decoder library are already installed on my machine. But this will be the first time I apply it to something turned by a motor instead of by a human hand. Is it fast enough to keep up? Decoder library documentation says 100-127kHz sampling rate is possible on a Teensy 3, which was the library's original target hardware. Running on an ATmega328 would be slower.
Aside: I found this Gammon forum thread listing technical detail on ATmega328 interrupt service routines, which laid out work just for ISR overhead that would take 5.125us before any ISR code actually runs. This puts a hard upper bound of ~200 kHz on response rate of an ISR that does nothing.
In the spirit of "try the easy thing first" I'll start with ATmega328 Arduino. If it proves too slow, I have a Teensy LC somewhere, and I definitely have ESP8266 boards. In the unlikely case they all fail to meet my need, I can resume my examination of ESP32's pulse counter (PCNT) peripheral.
This teardown ran far longer than I originally thought it would. Click here to rewind back to where this adventure started.