Arduino library · Input

GyverEncoder: an example that runs

Yes. You can use the GyverEncoder library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Rotary encoder, KY-040). The example below, "Turns, clicks and holds with GyverEncoder", runs on the Arduino Uno R3.

Built in on Mokxi: #include <GyverEncoder.h> and it builds, with nothing to install. Free, and no account needed.

What GyverEncoder does

A rotary encoder and its button polled with tick(): turns, turns with the knob held, fast turns, clicks, double clicks and holds. That is what GyverEncoder, by AlexGyver, covers. The sketch below uses it on the Arduino Uno: “Turns, clicks and holds with GyverEncoder”.

The interface of AlexGyver's "GyverEncoder" library (4.10): a rotary encoder and its push switch, polled with tick(): turns, turns with the knob held, fast turns, clicks, double clicks and holds.

Use the rotary part (a KY-040): CLK, DT and SW to three pins, all with the pull-up on (HIGH_PULL, the default). A KY-040 detent is a whole cycle of the two signals, which is TYPE2: one step a detent, counted when both contacts open again.

The calls follow release 4.10.0 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameGyverEncoder
AuthorAlexGyver
Include#include <GyverEncoder.h>
Boardsevery board Mokxi compiles for
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesRotary encoder, KY-040

Install it

It builds on every board Mokxi compiles for.

In Mokxi it is built in: #include <GyverEncoder.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "GyverEncoder" by AlexGyver from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add GyverEncoder to lib_deps in PlatformIO. Mokxi follows the calls of release 4.10.0, so a sketch written here builds there too.

What to know in the simulator: Only upstream’s default BINARY_ALGORITHM is here. Its class is called Encoder, as is Encoder.h’s: a sketch includes one header or the other.

The calls you will use

In the order a sketch meets them.

Encoder enc1(CLK, DT, SW)

Makes enc1, the Encoder object the calls below are made on.

enc1.setType(TYPE2)

Sets the type to the value you pass in.

enc1.isRight()

Returns true or false for "is right".

enc1.isLeft()

Returns true or false for "is left".

enc1.isRightH()

Returns true or false for "is right h".

enc1.isLeftH()

Returns true or false for "is left h".

enc1.isClick()

Returns true or false for "is click".

enc1.isSingle()

Returns true or false for "is single".

enc1.isDouble()

Returns true or false for "is double".

Example: Turns, clicks and holds with GyverEncoder

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#define CLK 2
#define DT 3
#define SW 4

#include "GyverEncoder.h"
Encoder enc1(CLK, DT, SW);

void setup() {
  Serial.begin(9600);
  enc1.setType(TYPE2);  // a KY-040: one step a detent
  Serial.println("ready");
}

void loop() {
  enc1.tick();

  if (enc1.isRight()) Serial.println("Right");
  if (enc1.isLeft()) Serial.println("Left");

  if (enc1.isRightH()) Serial.println("Right holded");
  if (enc1.isLeftH()) Serial.println("Left holded");

  if (enc1.isClick()) Serial.println("Click");
  if (enc1.isSingle()) Serial.println("Single");
  if (enc1.isDouble()) Serial.println("Double");

  if (enc1.isHolded()) Serial.println("Holded");
}

Wiring

  • Arduino Uno R3 2 to Rotary encoder, KY-040 CLK
  • Arduino Uno R3 3 to Rotary encoder, KY-040 DT
  • Arduino Uno R3 4 to Rotary encoder, KY-040 SW
  • Arduino Uno R3 5V to Rotary encoder, KY-040 VCC
  • Arduino Uno R3 GND to Rotary encoder, KY-040 GND

Open it in the editor, wired and ready to run

Try GyverEncoder without installing anything

Open the example, change the sketch, and keep your version in a free account.