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 name | ClickEncoder |
|---|---|
| Author | 0xPIT |
| Include | #include <ClickEncoder.h> |
| Boards | every board Mokxi compiles for |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Rotary 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().
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- ATtiny85
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
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.
#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
Try ClickEncoder without installing anything
Open the example, change the sketch, and keep your version in a free account.