•  Rotary Encoder (ALPS) 11mm, 15pulse/30position, 6/20mm, button

Description:
This rotary encoder can be easily used with an interrupt handler routine. Please check out our open source sample code (free to use - without warranties of any kind, either expressed or implied).

Specifications:

  • Encoder type: Alps
  • Size Base: 11mm
  • 15pulse/30position
  • Diameter/length: 6/20mm
  • Button function: yes


resources:
Code to use this encoder:

 

-----------------------------------------------------------------------------------------------------------------

// Alps Encoder

#define PINenc0a       2     // Rotary encoder0 pin a. 5v -> Encoder middle pin -> Right pin -> PINenc0a
#define PINenc0b       13    // Rotary encoder0 pin b. 5v -> Encoder middle pin -> Left pin -> PINenc0b
#define PINenc0but     5     // Rotary encoder0 button. 5V - Switch - Analog5 - 10k Ohm - Ground
#define ENC0changedelay 100  // Delay per change (steps of 100 uSeconds)
#define ENC0maxdelay    250  // Max total delay per interrupt (steps of 100 uSeconds)

volatile unsigned char ENC0=0;                 // Rotary encoder state
volatile unsigned char ENC0old=0;                 // Rotary encoder state

volatile int ENC0pos=0;                        // Rotary encoder position
int          ENC0oldpos=0;                     // Rotary encoder old position
volatile unsigned char ENC0speed=1;            // Rotary encoder accelleration
volatile char ENC0dir=0;                       // Rotary encoder direction


int ABenc0button()
{
  return (analogRead(PINenc0but)>500);
}

// Rotary encoder interrupt 1 - Panasonic encoder - single interrupt
void ABenc0()
{
  unsigned char pins;
  unsigned long t1=0, t2=0;
  
  while ((t1<ENC0maxdelay) && (t2<ENC0changedelay))
  {
    pins=digitalRead(PINenc0b)+(digitalRead(PINenc0a) << 4);
  
    if (pins!=ENC0)
    {
       if ((ENC0==0x00) && (pins==0x10)) { ENC0pos-=ENC0speed; ENC0dir=1; }
       if ((ENC0==0x10) && (pins==0x01)) { ENC0pos-=ENC0speed; ENC0dir=1; }
       if ((ENC0==0x01) && (pins==0x10)) { ENC0pos-=ENC0speed; ENC0dir=1; }    
       if ((ENC0==0x11) && (pins==0x01)) { ENC0pos-=ENC0speed; ENC0dir=1; }

       if ((ENC0==0x10) && (pins==0x00)) { ENC0pos+=ENC0speed; ENC0dir=1; }      
       if ((ENC0==0x01) && (pins==0x11)) { ENC0pos+=ENC0speed; ENC0dir=1; }
       if ((ENC0==0x11) && (pins==0x00)) { ENC0pos+=ENC0speed; ENC0dir=-1; }
       if ((ENC0==0x00) && (pins==0x11)) { ENC0pos+=ENC0speed; ENC0dir=-1; }
      
       ENC0=pins;
       t2=0;
    }
    
    delayMicroseconds(100);
    t1++;
    t2++;
  }
}


void ABenc0setup()
{
  // Encoder 0
  pinMode(PINenc0a, INPUT);
  pinMode(PINenc0b, INPUT);

  attachInterrupt(0, ABenc0, RISING+FALLING);
}


void setup()
{
  Serial.begin(9600);
  Serial.print("Bootn");

  ABenc0setup();
}


void loop()
{
  Serial.print("Encoder position="); Serial.print(ENC0pos); Serial.print(" button="); Serial.print(ABenc0button());
  Serial.println("");

  delay(250);
}

Write a review

Please login or register to review

Rotary Encoder (ALPS) 11mm, 15pulse/30position, 6/20mm, button

  • Brand: Others
  • Product Code: 18008
  • Availability: In Stock
  • 4.39€

  • Ex Tax: 3.63€