Deprecated: The PSR-0 `Requests_...` class names in the Requests library are deprecated. Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience. in /home3/aneindia/public_html/wp-includes/class-requests.php on line 24
Rotary Encoder Module Brick Sensor Development for arduino KY-040 - Absolute Native Electronics

Rotary Encoder Module Brick Sensor Development for arduino KY-040

195.00

(3 customer reviews)

17 in stock

Description

Rotary Encoder Module KY-040 for Arduino

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.

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.

 

The 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;
       encoderPosCount--;
     }
     Serial.print ("Rotated: ");
     if (bCW){
       Serial.println ("clockwise");
     }else{
       Serial.println("counterclockwise");
     }
     Serial.print("Encoder Position: ");
     Serial.println(encoderPosCount);
     
   } 
   pinALast = aVal;
 }

3 reviews for Rotary Encoder Module Brick Sensor Development for arduino KY-040

  1. Amazed_abhi

    Works okay but you will need more processing to eliminate heavy switching noise. Other encoders have worked better without any such problem.

  2. Badhur Ali Ahamed Jalaludeen

    Good worked as described

  3. Nisha Sarath

    Exactly as described

Add a review

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