Arduino library · Input

ClickEncoder: an example that runs

Yes. You can use the ClickEncoder 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, "Turn a knob, click it, and count the notches", runs on the Arduino Uno R3.

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

What ClickEncoder does

A knob with a click button: ClickEncoder encoder(A, B, BTN, 4), service(), getValue() in notches and getButton() as Clicked, DoubleClicked or Held. That is what ClickEncoder, by 0xPIT, covers. The sketch below uses it on the Arduino Uno: “Turn a knob, click it, and count the notches”.

The ClickEncoder library's interface (Peter Dannegger's and Christian Dannhofer's rotary encoder with a click button, as packaged by 0xPIT), for a KY-040 style knob.

Use the rotary part: CLK and DT to the two pins, SW to the button pin.

The calls follow release 1.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 nameClickEncoder
Author0xPIT
Include#include <ClickEncoder.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 <ClickEncoder.h> and the sketch compiles, with no download, no zip and no Library Manager.

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

What to know in the simulator: Acceleration approximates upstream's curve; service() runs from loop() or a timer and times the button from millis().

The calls you will use

In the order a sketch meets them.

ClickEncoder encoder(2, 3, 4, 4)

Makes encoder, the ClickEncoder object the calls below are made on.

encoder.setAccelerationEnabled(false)

Sets the acceleration enabled to the value you pass in.

encoder.getValue()

Returns the value as a whole number.

encoder.getButton()

Returns the button as a Button.

Example: Turn a knob, click it, and count the notches

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

sketch.ino · Arduino Uno R3
#include <ClickEncoder.h>

ClickEncoder encoder(2, 3, 4, 4);
int16_t value = 0;

void setup() {
  Serial.begin(9600);
  encoder.setAccelerationEnabled(false);
  Serial.println("ready");
}

void loop() {
  encoder.service();
  int16_t step = encoder.getValue();
  if (step != 0) {
    value += step;
    Serial.print("value ");
    Serial.println(value);
  }
  ClickEncoder::Button b = encoder.getButton();
  if (b == ClickEncoder::Clicked) Serial.println("clicked");
}

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 ClickEncoder without installing anything

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