Skip to content
Bendeguz-Cs edited this page Oct 10, 2025 · 16 revisions

Functions

Encoder encoder()

Description

Sets up the pins. If the value goes up for the wrong direction of rotation then flip the pins:

//value goes up for right turn
Encoder encoder(4, 5);
//value goes up for left turn
Encoder encoder(5, 4);

Parameters

  • CLK_pin - CLK pin of the encoder

  • DT_pin - DT pin of the encoder

Example

//Set the pins of the encoder
Encoder encoder(4, 5);

Encoder.begin()

Description

Initializes the library.

Parameters

  • none

Example

//initialize the library
encoder.begin();

Encoder.read()

Description

Returns an integer that can be changed by the rotary encoder.

Parameters

  • none

Example

//Returns the value
encoder.read();

Encoder.limitedRead()

Description

Returns an integer that can be changed by the rotary encoder but can be given a lower and an upper limit.

Parameters

  • int Minval - lower stop

  • int Maxval - upper stop

Example

//Returns the value constrained between 0 and 10
encoder.limitedRead(0, 10);

Encoder.setPosition()

Description

Sets the value oft the encoder to the pos parameter and returns HIGH if succesful else it returns LOW.

Parameters

  • int pos - the position to set the encoder value to

Example

//sets the position of the encoder to zero
encoder.setPosition(0);
//if succesful at setting the value to zero it will print "1"
Serial.println(encoder.setPosition(0));

Encoder.scale()

Description

Sets increments of the output

Parameters

  • int scale - the amount the position changes per click

Example

//sets the scaling to 10
encoder.scale(10);

Encoder.motion()

Description

Returns true when the encoder was moved

Parameters

  • none

Example

//checks for motion
if(encoder.motion()) {  //only print the value of the encoder if it was moved
  Serial.println(encoder.read());
}

Encoder.setDebounceTime()

Description

Sets the time waiting between checking the encoder's pins

Parameters

  • int debounce_time

Example

//sets the debounce time to 2 milliseconds
encoder.setDebounceTime(2)
}

Clone this wiki locally