KY-040 Rotary Encoder Module (HCMODU0063)

Modules for various types of sensors including accelerometers. gyro's, IR motion, etc
Post Reply
admin
Site Admin
Posts: 866
Joined: Sun Aug 05, 2012 4:02 pm

KY-040 Rotary Encoder Module (HCMODU0063)

Post by admin » Tue Jul 15, 2014 11:51 am

Image

Working voltage: 5V or 3.3V
Pulses per rotation: 20

Dimensions
Image



Pinout
1...GND
2...+5V or +3.3V supply
3...SW connects pin to GND when shaft is pressed
4...DT direction pulse. Pin has 10K pullup connected to supply.
5...CLK clock pulse. Pin has 10K pullup connected to supply.




Example Arduino Sketch:

  1. /* FILE:    Rotary_Encoder_Example
  2.    DATE:    31/03/23
  3.    VERSION: 1.0
  4.    AUTHOR:  Andrew Davies
  5.    BY:      HobbyComponents.com
  6.    
  7. 31/03/23 version 1.0: Original version
  8.  
  9. This sketch uses the pin interrupt feature of the Arduino to automatically
  10. record the state of the encoder when it is rotated. The result will be stored
  11. as a position in the variable 'pos'. To use this sketch you will need
  12. the following rotary encoder:
  13.  
  14. https://hobbycomponents.com/sensors/502-ky-040-rotary-encoder-module
  15.  
  16. Connect it to your Arduino as follows
  17.  
  18. Encoder.....Arduino
  19. CLK.........3
  20. DT..........2
  21. SW..........4
  22. +...........5V or 3V depending on your Arduino
  23. GND.........GND
  24.  
  25.  
  26. This sketch is provided free to support the open source community.
  27. PLEASE SUPPORT HOBBY COMPONENTS so that we can continue to provide free content
  28. like this by purchasing items from our store -
  29.  
  30. HOBBYCOMPONENTS.COM
  31.  
  32. You may copy, alter and reuse this code in any way you like, but please leave
  33. reference to HobbyComponents.com in your comments if you redistribute this code.
  34. This software may not be used directly for the purpose of selling products that
  35. directly compete with Hobby Components Ltd's own range of products.
  36. THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
  37. EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  38. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
  39. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
  40. INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
  41. REASON WHATSOEVER.
  42. */
  43.  
  44.  
  45. /* Define digital pins used to read the encoder */
  46. #define DT 2
  47. #define CLK 3
  48. #define SW 4
  49.  
  50. // Delay in ms to wait for encoder contacts to settle
  51. #define DEBOUNCE_DELAY  5
  52.  
  53. byte pos = 0, lastPos = 0, lastState;
  54.  
  55.  
  56. void setup()
  57. {
  58.   Serial.begin(9600);
  59.   pinMode(SW, INPUT_PULLUP);
  60.   pinMode(CLK, INPUT_PULLUP);
  61.   attachInterrupt(digitalPinToInterrupt(CLK), encoder, CHANGE);
  62.  
  63.   lastState = (digitalRead(DT) << 1) | digitalRead(CLK);
  64. }
  65.  
  66. void loop()
  67. {
  68.   // Check if encoder position counter has changed
  69.   if(pos != lastPos)
  70.   {
  71.     Serial.println(pos);
  72.     lastPos = pos;
  73.   }
  74.  
  75.   // Check if encoder is pressed
  76.   if(digitalRead(SW) == false)
  77.     Serial.println("Pressed!");
  78.  
  79.     delay(100);
  80. }
  81.  
  82.  
  83. // Interrupt service routine to read the encoder state
  84. void encoder()
  85. {
  86.   // Wait for encoder contacts to settle
  87.   delay(DEBOUNCE_DELAY);
  88.  
  89.   // Read the encoder outputs
  90.   byte state = (digitalRead(DT) << 1) | digitalRead(CLK);
  91.  
  92.   // If the state has changed then update the counter
  93.   if(state != lastState)
  94.     (state == 3 || state == 0) ? pos-- : pos++;
  95.  
  96.   lastState = state;
  97. }



Libraries, example code, and diagrams are provided as an additional free service by Hobby Components and are not sold as part of this product. We do not provide any guarantees or warranties as to their accuracy or fitness for purpose.

Post Reply

Return to “Sensors”