oudesnepelaar / Rotary-KY040-Arduino

Arduino sketch demonstrating how to read and process the KY-040 rotary encoder output to interpret them as clockwise and counterclockwise turns.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This Arduino sketch allows easy interpretation of the Data/Clock pin values of a KY040 rotary encoder
and translates them into practically usable CW and CCW turns.

One needs to sample the rotation status in a function and poll the values with a high enough frequency
to capture all state changes in the CLK / DT output values.

CCW turns are represented by an int value of -1, CW turns by 1, no movement by 0.

Because this is not a very reliable way to process the turn data, as the polling will be easily delayed
by the other code running in the Arduino sketch main loop, a better way is to use interrupt pins.
Using interrupts will guarantee that no changes in the output pins are missed.

Check out the Library version of this sketch by the same author to see an example of interrupt usage.

Ferrie J Bank,
Amsterdam 21 February 2020

About

Arduino sketch demonstrating how to read and process the KY-040 rotary encoder output to interpret them as clockwise and counterclockwise turns.

License:Other


Languages

Language:C++ 100.0%