Skip to main content

Encoder

Digital
input
Small RAM
0.4.5

#Description

This can be used similarly to a rotary potentiometer, however, it does not have a limit, it can rotate indefinitely. This is why it counts up and down and does not give a clear degree unit.

This component requires 2 pins.

#Aliases

  • Rotary Encoder

#Functions



#JavaScript / Node.js

ClassEncoderextends ComponentBase

Rotary encoder component. Tracks a signed count value and emits directional events on each detent.

#Constructor options

OptionTypeRequiredDescription
pin1number | DigitalPinrequired
pin2number | DigitalPinrequired
initialValuenumberoptional
invertbooleanoptional

#Events

EventParametersDescription
changevalue: number
up
down

#Methods

updateOptions(options: { invert?: boolean })

Update runtime options such as invert direction.

setCountTo(value: number)

Set the encoder count to a specific value. Note: This updates the local state but does not communicate with the Arduino. For hardware reset, use a serial command.

countUp()

Increment the encoder count by 1.

countDown()

Decrement the encoder count by 1.

get countnumber

Get the current encoder count value

get valuenumber | undefinedfrom ComponentBase

Get the current value of the component.

#Example

JavaScript
import { Board, Encoder } from "@blokdots/components";

const board = new Board("/dev/ttyACM0");
await board.open();

const encoder = new Encoder({ board, pin1: "D5", pin2: "D5" });

encoder.on("change", (value) => {
  console.log("Encoder changed:", value);
});


#C++ (Arduino)

ClassEncoderComponentextends ComponentBase

Emits events: - evtChanged : whenever the value changes - evtUp : when the value increments - evtDown : when the value decrements Notes: - The Encoder library counts transitions; many encoders produce 2 or 4 counts per detent. Use `step` to normalize (common values: 1, 2, 4).

#Required libraries

  • #include <Encoder.h>Encoder v1.4.4

#Setup

C++
#include "blokdots.h"

EncoderComponent myComponent(eventBus, 0, 0, MY_EVENT, MY_EVENT, MY_EVENT);

#Constructor

EncoderComponent(bus, pinA, pinB, evtChanged, evtUp, evtDown, initialValue, invert, pollIntervalMs, step)
ParameterTypeDefault
busEventBusrequired
pinAuint8_trequired
pinBuint8_trequired
evtChangeduint8_trequired
evtUpuint8_trequired
evtDownuint8_trequired
initialValueint0
invertboolfalse
pollIntervalMsuint16_t2
stepint4

#Methods

value()int
emittedValue()int
lastDelta()int

Optional: expose last delta (+/-)