Rotary encoders

Pull-up rotary encoder

This sketch demonstrates the implementation of a rotary encoder in pull-up configuration.


The code

#include <CtrlEnc.h>

// Define an onTurnLeft handler.
void onTurnLeft() {
  Serial.println("Pull-up rotary encoder turn left");
}

// Define an onTurnRight handler.
void onTurnRight() {
  Serial.println("Pull-up rotary encoder turn right");
}

// Create a rotary encoder with the clk signal pin number, dt signal pin number,
// onTurnLeft & onTurnRight handler.
CtrlEnc encoder(1, 2, onTurnLeft, onTurnRight);

void setup() {
  Serial.begin(9600);
  
  // If you use external pull-up resistors, uncomment the following line:
  // encoder.setPinMode(INPUT, PULL_UP);

  // If you use external pull-down resistors, uncomment the following line:
  // encoder.setPinMode(INPUT, PULL_DOWN);

  // If you have a board that has internal pull-down resistors, and want to 
  // use those instead, you can uncomment the following line:
  // encoder.setPinMode(INPUT_PULLDOWN);

  // If you have a board that has internal pull-up resistors, and want to use those instead, you 
  // don't need to call the setPinMode() method, as the encoder is set to 'INPUT_PULLUP' by default.
}

void loop() {
  // The process method will poll the rotary encoder object and handle all it's functionality.
  encoder.process();
}
Previous
Rotary encoder methods