Rotation Encoder Module (KY-040) [S043]


* Specs

By rotating the rotary encoder can be counted in the positive direction and the reverse direction during rotation of the output pulse frequency, unlike rotary potentiometer counter, which Species rotation counts are not limited. With the buttons on the rotary encoder can be reset to its initial state, that starts counting from 0.

How it works: incremental encoder is a displacement of the rotary pulse signal is converted to a series of digital rotary sensors. These pulses are used to control Angular displacement. In Eltra angular displacement encoder conversion using a photoelectric scanning principle. Reading system of alternating light transmitting window and the window is not Consisting of radial indexing plate (code wheel) rotating basis, while being an infrared light source vertical irradiation light to the code disk image onto the receiving

On the surface. Receiver is covered with a diffraction grating, which has the same code disk window width. The receiver's job is to feel the rotation of the disc Resulting changes, and change the light into corresponding electrical changes. Then the low-level signals up to a higher level, and generates no interference Square pulse, which must be processed by electronic circuits. Reading systems typically employ a differential manner, about the same but the phase difference of the two waveforms Different by 180 ° compared to the signal in order to improve the quality and stability of the output signal. Reading is then the difference between the two signals formed on the basis, Thus eliminating the interference.

* Contents

- SW : Button Push 

- DT (D3) : A, CLK(D2) : B

- When Turn Left

  : DT=1, CLK=1 6

  : DT=0, CLK=1 5

  : DT=0, CLK=0 4

  : DT=1, CLK=0 3

  : DT=1, CLK=1 2

- When Turn Right

  : DT=1, CLK=1 6

  : DT=1, CLK=0 7

  : DT=0, CLK=0 8

  : DT=0, CLK=1 9

  : DT=1, CLK=1 10

- Connect

GND ----- GND

+ ----- 5V

SW ----- D4 (Detect Button)

DT ----- D3 (A, Interr Pin)

CLK ----- D2 (B, Interr Pin)

- Key Code

#include <Wire.h> 

#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27,20,4);  // LCD2004

int encoderPin1 = 2; // (CLK)

int encoderPin2 = 3; // (DT)

int encoderSwitchPin = 4; //push button switch (SW)


volatile int lastEncoded = 0;

volatile long encoderValue = 0;


long lastencoderValue = 0;


int lastMSB = 0;

int lastLSB = 0;


void setup()


  lcd.init();  // initialize the lcd 


  lcd.print("start LCD2004");


  pinMode(encoderPin1, INPUT); 

  pinMode(encoderPin2, INPUT);


  pinMode(encoderSwitchPin, INPUT);


  digitalWrite(encoderPin1, HIGH); //turn pullup resistor on

  digitalWrite(encoderPin2, HIGH); //turn pullup resistor on


  digitalWrite(encoderSwitchPin, HIGH); //turn pullup resistor on


  //call updateEncoder() when any high/low changed seen

  //on interrupt 0 (pin 2), or interrupt 1 (pin 3) 

  attachInterrupt(0, updateEncoder, CHANGE); 

  attachInterrupt(1, updateEncoder, CHANGE);







int nowCnt = 0;


void loop()






  //Do stuff here



     lcd.print("button not pushed"); 

    //button is not being pushed



     lcd.print("button pushed     "); 





  lcd.print("encoderValue="+(String)encoderValue+"   "); 



  lcd.print("DT="+(String)digitalRead(encoderPin2)+" CLK="+(String)digitalRead(encoderPin1)+" "); 






void updateEncoder(){

  int MSB = digitalRead(encoderPin1); //MSB 

  int LSB = digitalRead(encoderPin2); //LSB 


  int encoded = (MSB << 1) LSB; 

  int sum  = (lastEncoded << 2) encoded; 


  if(sum == 0b1101 sum == 0b0100 sum == 0b0010 sum == 0b1011) encoderValue ++;

  if(sum == 0b1110 sum == 0b0111 sum == 0b0001 sum == 0b1000) encoderValue --;


  lastEncoded = encoded; //store this value for next time


'2) Sensor > Rotary' 카테고리의 다른 글

Rotary Encoder Module (KY-040) [S120]  (0) 2016.09.12
Rotation Encoder Module (KY-040) [S043]  (0) 2016.09.12
Posted by RDIoT

댓글을 달아 주세요