Keyes KY-040 Arduino Rotary Encoder User Manual

KY040 EncoderThe Keyes KY-040 Rotary Encoder

The Keyes KY-040 rotary encoder is a rotary input device (as in knob) that provides an indication of how much the knob has been rotated AND what direction it is rotating in.

It’s a great device for stepper and servo motor control.  You could also use it to control devices like digital potentiometers.

How to Get One

These are sold in several places, including the vendors below.  Each of these vendor offers solid buyer protection through PayPal.


IC Station

Bang Good

Rotary Encoder Basics

A rotary encoder has a fixed number of positions per revolution.   These positions are easily felt as small “clicks” you turn the encoder.
The Keyes module that I have has thirty of these positions.

Rotary Encoder PinsOn one side of the switch there are three pins.   They are normally referred to as A, B and C.   In the case of the KY-040,  they are oriented as shown.

Inside the encoder there are two switches.  Once switch connects pin A to pin C and the other switch connects pin B to C.

In each encoder position, both switches are either opened or closed.  Each click causes these switches to change states as follows:

  • If both switches are closed,  turning the encoder either clockwise or counterclockwise one position will cause both switches to open
  • If both switches are open, turning the encoder either clockwise or counterclockwise one position will cause both switches to close.

The illustration below is representative of how the switch is constructed.

Rotary Encoder RepresentationAs you can see, the angular position of the A terminal and the B terminal is such that:

  • Rotating the switch clockwise will cause the switch connecting A and C to change states first.
  • Rotating the switch counterclockwise will cause the switch connecting B and C to change states first.

If we were to represent the opening an closing of the switches as wave forms, it would look something like this.

Rotary Encoder PhasesEssentially,   determining which switch changed states first is how the direction of rotation is determined.

If A changed states first, the switch is rotating in a clockwise direction.

If B changed states first, the switch is rotating in a counter clockwise direction.

KY-040 Pin Outs

The pin outs for this rotary encoder are identified in the illustration below.

Keyes KY-040 Rotary Encoder Pin OutsThe module is designed so that a low is output when the switches are closed and a high when the switches are open.

The low is generated by placing a ground at Pin C and passing it to the CLK and DT pins when switches are closed.

The high is generated with a 5V supply input and pullup resistors, such that CLK and DT are both high when switches are open.

Not previously mentioned is the existence of of push button switch that is integral to the encoder.   If you push on the shaft, a normally open switch will close.    The feature is useful if you want to change switch function.   For example,  you may wish to have the ability to between coarse and fine adjustments.

Keyes Rotary Encoder Schematic

A schematic for this module is provided below.   R2 and R3 in the schematic are pull up resistors.

Click on it for a bigger view.

Keyes KY-040 Rotary Encoder Module SchematicKeyes KY-040 Evaluation Circuit

Successfully implementing the Rotary Encoder into any project requires a clear understanding of everything that has been discussed thus far.    If you’re still a little fuzzy,  you may wish to throw together the evaluation circuit illustrated below:

Keyes KY-040 Rotary Encoder Evaluation SchematicVERY SLOWLY rotate then encoder shaft both clockwise and counterclockwise.   Notice which LEDs change state first with rotation.

KY-040 Arduino Tutorial

Module Connection to the Arduino

Pretty straight forward…   All you need to do is connect four wires to the module.

Keyes KY-040 Rotary Encoder Arduino ConnectionsThe Arduino Sketch

This is  a simple sketch that shows how to count the encoder position and how to determine direction of rotation.    It has no switch debounce,  nor does it use interrupts.    A fully developed application might need to incorporate these in order to make it robust.

 int pinA = 3;  // Connected to CLK on KY-040
 int pinB = 4;  // Connected to DT on KY-040
 int encoderPosCount = 0; 
 int pinALast;  
 int aVal;
 boolean bCW;

 void setup() { 
   pinMode (pinA,INPUT);
   pinMode (pinB,INPUT);
   /* Read Pin A
   Whatever state it's in will reflect the last position   
   pinALast = digitalRead(pinA);   
   Serial.begin (9600);

 void loop() { 
   aVal = digitalRead(pinA);
   if (aVal != pinALast){ // Means the knob is rotating
     // if the knob is rotating, we need to determine direction
     // We do that by reading pin B.
     if (digitalRead(pinB) != aVal) {  // Means pin A Changed first - We're Rotating Clockwise
       encoderPosCount ++;
       bCW = true;
     } else {// Otherwise B changed first and we're moving CCW
       bCW = false;
     Serial.print ("Rotated: ");
     if (bCW){
       Serial.println ("clockwise");
     Serial.print("Encoder Position: ");
   pinALast = aVal;


  1. Surendra November 21, 2017
  2. Alan Andrews June 24, 2017
  3. rid May 26, 2017
  4. Bob May 19, 2017
  5. mrx May 2, 2017
  6. capnfatz@gmail.comAuthor April 23, 2017
  7. Aris April 15, 2017
  8. Alex March 29, 2017
  9. Mo February 13, 2017
  10. Dan Aunspach January 24, 2017
  11. Lowell Bahner January 10, 2017
  12. Lowell Bahner January 9, 2017
  13. Lowell Bahner January 9, 2017
  14. Curtis Parrish December 14, 2016
  15. Bill Williams December 4, 2016
  16. Taurai November 24, 2016
  17. dhi June 13, 2016
  18. bishops May 8, 2016
  19. Phil April 26, 2016
  20. Ibrahim April 14, 2016
  21. Jorge April 13, 2016
  22. kbellco April 6, 2016
  23. Francois Bujold February 22, 2016
  24. February 9, 2016
  25. fabware February 5, 2016
  26. Eddie Arnold December 25, 2015
  27. Gebriel December 14, 2015
  28. capnfatz@gmail.comAuthor November 12, 2015
  29. Anil November 12, 2015
  30. capnfatz@gmail.comAuthor September 27, 2015
  31. kuhaku September 27, 2015
  32. Boby September 8, 2015
  33. Sorin August 28, 2015
  34. Kon August 22, 2015
  35. capnfatz@gmail.comAuthor July 31, 2015
  36. Julio F2Me July 31, 2015
  37. capnfatz@gmail.comAuthor July 19, 2015
  38. Mike July 19, 2015

Add a Comment

Your email address will not be published. Required fields are marked *