30 Days Lost in Space → Help Center

Libraries

BasicEncoder: Rotary Encoder Library

Count pulses from rotary encoder knobs with minimal code. BasicEncoder handles debouncing and direction detection so you can focus on what to do with the turns.

What is BasicEncoder?

BasicEncoder is a lightweight library for reading simple rotary encoder control knobs. It counts clicks (detents), handles contact bounce, and tracks direction.

Installation

Search for "BasicEncoder" by Peter Harrison in the Arduino Library Manager.

Setup

#include <BasicEncoder.h>

BasicEncoder encoder(2, 3);  // Pin A, Pin B

Basic Usage

Polling Method

void loop() {
  encoder.service();  // Call frequently!

  int change = encoder.get_change();
  if (change != 0) {
    Serial.print("Turned: ");
    Serial.println(change);  // +1 or -1
  }
}

With Timer Interrupt (Recommended)

#include <BasicEncoder.h>

BasicEncoder encoder(2, 3);

// Call from a timer interrupt at 1kHz+
void timerISR() {
  encoder.service();
}

Key Functions

| Function | Description | |----------|-------------| | service() | Check for changes — call often (polling or ISR) | | get_change() | Get clicks since last call (+/- direction) | | get_count() | Get total accumulated clicks | | reset() | Reset the count to zero | | set_reverse() | Swap the counting direction |

Constructor Options

// Full constructor
BasicEncoder encoder(pinA, pinB, active_state, steps);

// Defaults: active_state = LOW, steps = 4
BasicEncoder encoder(2, 3);
  • active_state: Set to HIGH if your encoder's detent rest state is HIGH
  • steps: Steps per detent click (usually 4 for standard encoders)

Tips

  • Call service() as frequently as possible — at least every millisecond
  • get_change() is a destructive read (resets to 0 after reading)
  • get_count() is non-destructive (running total)
  • For smooth UI: multiply get_change() by a speed factor
  • Not suitable for high-speed motor encoders — designed for control knobs