Adafruit Rotary Encoder

https://www.adafruit.com/product/377

Don't forget to get a knob for it:

https://www.adafruit.com/product/2047

https://www.adafruit.com/product/2048

https://www.adafruit.com/product/2056

https://www.adafruit.com/product/2055

https://www.adafruit.com/product/2058

https://www.adafruit.com/product/2046

https://www.adafruit.com/product/2058

 

Similar Pin Compatible Alternatives

PEC11L-4220F-S0015 (long shaft)

PEC11R-4215F-S0024

 

Rotary Encoder Breakout Board

rotary-encoder-breakout PCB found here

Solarbotics rotary encoder on PCB

DFROBOT rotary encoder on PCB

 

Hardware

This is a 24-pulse mechanical incremental rotary encoder with a built in pushbutton.   The rotary encoder has three signal pins that come into contact with the common ground pin in a particular order that can be used to determine the direction.   The signals are shifted out of phase with each other, in a way that is called quadrature encoding.   The three pins on the one side of the rotary encoder are for the encoder, and the two on the opposite side of the rotary encoder are for the pushbutton.   The center encoder pin is ground, and the two outboard pins are the signal pins.   When looking at the top of the rotary encoder, the pin to the left is "A", and the pin to the right is "C".  

Software

Most libraries for a rotary encoder use digital pins usable for interrupts.   I chose the libraries referenced in the Arduino code below.  

/*
  Adafruit Rotary Encoder
  https://www.adafruit.com/product/377
  DataSheet: https://cdn-shop.adafruit.com/datasheets/pec11.pdf
  https://learn.adafruit.com/rotary-encoder?view=all
  
  Rotary encoder library by Paul Stoffregen
  https://github.com/PaulStoffregen/Encoder/blob/master/examples/Basic/Basic.pde

  Pushbutton library by Matthias Hertel
  https://github.com/mathertel/OneButton
  http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
  
 */

// Rotary encoder
#include   
// Assign digital inputs that can be assigned to interrupts
// Arduino Uno, Fio, other 328-based .. pins 2 & 3
// Mega, Mega2560, MegaADK 2, 3, 18, 19, 20, 21
// Micro, Leonardo, other 32u4-based 0, 1, 2, 3, 7
// Zero all digital pins, except 4
// Due all digital pins
Encoder myEnc(3,2);
long lOldPosition = -999;

// Rotary encoder pushbutton connected to A1 (interrupt NOT required)
#include "OneButton.h"
OneButton button(A1, true);


void setup() {
  Serial.begin(9600);
  
  // initialize the rotary encoder pushbutton myClickFunction function 
  // to be called on a click and double click event
  // (a long press event is available too)
  button.attachClick(btnSingleClick);
  button.attachDoubleClick(btnDoubleClick);
  // set 80 msec. debouncing time. Default is 50 msec.
  button.setDebounceTicks(80);
  
  pinMode(LED_BUILTIN, OUTPUT);
  blinkLED(LED_BUILTIN);
  delay(1);

  blinkLED(LED_BUILTIN);
} // setup()

void loop() {

  // Rotary encoder
  long lNewPosition = myEnc.read();
  if (lNewPosition != lOldPosition) {
    lOldPosition = lNewPosition;
    Serial.println(lNewPosition);
  }

  // Watch the pushbutton on the rotary encoder
  button.tick();
  
} // loop()


// this function will be called when the button was pressed 1 time 
// and them some time has passed.
void btnSingleClick() {
  blinkLED(LED_BUILTIN);
  Serial.println("btnSingleClick");
} // myClickFunction


// this function will be called when the button was pressed 2 times 
// in a short timeframe.
void btnDoubleClick() {
  blinkLED(LED_BUILTIN);
  Serial.println("btnDoubleClick");
} // myDoubleClickFunction


void blinkLED(byte ledPIN){
  //  consumes 300 ms.
  for(int i = 5; i > 0; i--){
    digitalWrite(ledPIN, HIGH);
    delay(30);
    digitalWrite(ledPIN, LOW);
    delay(30);
  }    
} // blinkLED()

Related Links

https://www.riyas.org/2015/12/adding-rotary-encoder-to-arduino.html

Adafruit - Rotary Encoder in CircuitPython

 


Do you need help developing or customizing a IoT product for your needs?   Send me an email requesting a free one hour phone / web share consultation.  

 

The information presented on this website is for the author's use only.   Use of this information by anyone other than the author is offered as guidelines and non-professional advice only.   No liability is assumed by the author or this web site.