{"id":53308,"date":"2017-04-12T11:42:05","date_gmt":"2017-04-12T10:42:05","guid":{"rendered":"https:\/\/www.microplanet.ma\/?post_type=product&#038;p=53308"},"modified":"2026-10-19T18:28:08","modified_gmt":"2026-10-19T18:28:08","slug":"ky-040-rotary-encoder-module","status":"publish","type":"product","link":"https:\/\/www.microplanet.ma\/en\/produit\/ky-040-rotary-encoder-module\/","title":{"rendered":"KY-040 Rotary Encoder Module"},"content":{"rendered":"<h3>KY-040 Rotary Encoder Module<\/h3>\n<p>The KY-040 is an electromechanical incremental rotary encoder module. Its two quadrature signals allow a controller to determine relative rotation and direction. The module also includes a push switch for selection or confirmation.<\/p>\n<h4>\ud83d\udd27 Listed specifications<\/h4>\n<ul>\n<li>Supply: 5 V DC as stated in the listing<\/li>\n<li>Encoder type: incremental, with quadrature signals<\/li>\n<li>Resolution: multiple pulses per revolution; the exact number is not specified in the listing<\/li>\n<li>Integrated push switch: normally open (N.O.)<\/li>\n<li>Compact module for control panels and robotics projects<\/li>\n<\/ul>\n<h4>\u2699\ufe0f How it works<\/h4>\n<p>As the shaft turns, the CLK and DT outputs change state in sequence. Comparing the signals indicates the direction of rotation; counting transitions gives relative movement. Pressing the shaft operates the SW switch.<\/p>\n<h4>\ud83d\udca1 Applications<\/h4>\n<ul>\n<li>Rotary controls for user interfaces<\/li>\n<li>Relative motor-speed adjustment<\/li>\n<li>Robot navigation controls<\/li>\n<li>Interactive interfaces<\/li>\n<li>DIY electronics projects<\/li>\n<\/ul>\n<h4>\ud83d\udccc Pinout<\/h4>\n<ul>\n<li><strong>GND<\/strong>: ground<\/li>\n<li><strong>VCC<\/strong>: supply (5 V as listed)<\/li>\n<li><strong>SW<\/strong>: push-switch output (N.O.)<\/li>\n<li><strong>DT<\/strong>: quadrature output B<\/li>\n<li><strong>CLK<\/strong>: quadrature output A<\/li>\n<\/ul>\n<h4>\ud83d\udd0c Using it with an Arduino Uno<\/h4>\n<p>Connect GND and VCC to the Arduino ground and a compatible supply. Connect DT and CLK to digital inputs to read rotation, and SW to a digital input to detect a press. The examples below are retained from the source listing; add suitable debouncing if the readings are unstable.<\/p>\n<h4>Basic Arduino example<\/h4>\n<pre class=\"prism undefined-numbers lang-cpp\" data-lang=\"C++\"><code>#define CLK 4\r\n#define DT 3\r\n#define SW 2\r\n\r\n\r\nint counter = 0;\r\nint currentStateCLK;\r\nint lastStateCLK;\r\nString currentDir = \"\";\r\n\r\n\r\nvoid setup() {\r\n\u00a0 Serial.begin(9600);\r\n\u00a0 pinMode(CLK, INPUT);\r\n\u00a0 pinMode(DT, INPUT);\r\n\u00a0 pinMode(SW, INPUT_PULLUP);\r\n\u00a0 lastStateCLK = digitalRead(CLK);\r\n}\r\n\r\n\r\nvoid loop() {\r\n\u00a0 currentStateCLK = digitalRead(CLK);\r\n\u00a0 if (currentStateCLK != lastStateCLK) {\r\n\u00a0 \u00a0 if (digitalRead(DT) != currentStateCLK) {\r\n\u00a0 \u00a0 \u00a0 counter--;\r\n\u00a0 \u00a0 \u00a0 currentDir = \"CCW\";\r\n\u00a0 \u00a0 } else {\r\n\u00a0 \u00a0 \u00a0 counter++;\r\n\u00a0 \u00a0 \u00a0 currentDir = \"CW\";\r\n\u00a0 \u00a0 }\r\n\u00a0 \u00a0 Serial.print(\"Direction: \");\r\n\u00a0 \u00a0 Serial.print(currentDir);\r\n\u00a0 \u00a0 Serial.print(\" | Counter: \");\r\n\u00a0 \u00a0 Serial.println(counter);\r\n\u00a0 }\r\n\u00a0 lastStateCLK = currentStateCLK;\r\n\r\n\r\n\u00a0 if (digitalRead(SW) == LOW) {\r\n\u00a0 \u00a0 Serial.println(\"Button Pressed\");\r\n\u00a0 } else {\r\n\u00a0 \u00a0 Serial.println(\"Button Released\");\r\n\u00a0 }\r\n\u00a0 delay(50);\r\n}<\/code><\/pre>\n<p>This sketch reads the encoder signals and reports direction, count, and the push-switch state in the Serial Monitor.<\/p>\n<h4>Advanced Arduino example with an I2C LCD<\/h4>\n<pre class=\"prism undefined-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;Wire.h&gt;\r\n#include &lt;LiquidCrystal_I2C.h&gt;\r\n\r\n\r\nLiquidCrystal_I2C lcd(0x27, 16, 2);\r\n#define CLK 4\r\n#define DT 3\r\n#define SW 2\r\n\r\n\r\nint counter = 0;\r\nint currentStateCLK;\r\nint lastStateCLK;\r\nString currentDir = \"\";\r\n\r\n\r\nvoid setup() {\r\n\u00a0 lcd.begin();\r\n\u00a0 lcd.backlight();\r\n\u00a0 Serial.begin(9600);\r\n\u00a0 pinMode(CLK, INPUT);\r\n\u00a0 pinMode(DT, INPUT);\r\n\u00a0 pinMode(SW, INPUT_PULLUP);\r\n\u00a0 lastStateCLK = digitalRead(CLK);\r\n}\r\n\r\n\r\nvoid loop() {\r\n\u00a0 currentStateCLK = digitalRead(CLK);\r\n\u00a0 if (currentStateCLK != lastStateCLK) {\r\n\u00a0 \u00a0 if (digitalRead(DT) != currentStateCLK) {\r\n\u00a0 \u00a0 \u00a0 counter--;\r\n\u00a0 \u00a0 \u00a0 currentDir = \"CCW\";\r\n\u00a0 \u00a0 } else {\r\n\u00a0 \u00a0 \u00a0 counter++;\r\n\u00a0 \u00a0 \u00a0 currentDir = \"CW\";\r\n\u00a0 \u00a0 }\r\n\u00a0 \u00a0 lcd.clear();\r\n\u00a0 \u00a0 lcd.setCursor(0, 0);\r\n\u00a0 \u00a0 lcd.print(\"Pos: \");\r\n\u00a0 \u00a0 lcd.print(counter);\r\n\u00a0 \u00a0 lcd.setCursor(0, 1);\r\n\u00a0 \u00a0 lcd.print(\"Dir: \");\r\n\u00a0 \u00a0 lcd.print(currentDir);\r\n\u00a0 }\r\n\u00a0 lastStateCLK = currentStateCLK;\r\n\r\n\r\n\u00a0 if (digitalRead(SW) == LOW) {\r\n\u00a0 \u00a0 Serial.println(\"Button Pressed\");\r\n\u00a0 } else {\r\n\u00a0 \u00a0 Serial.println(\"Button Released\");\r\n\u00a0 }\r\n\u00a0 delay(50);\r\n}<\/code><\/pre>\n<p>This example displays the relative count and direction on an I2C LCD and reports button state.<\/p>\n<h4>Components for the advanced example<\/h4>\n<ul>\n<li>Arduino Uno<\/li>\n<li>KY-040 rotary encoder module<\/li>\n<li>I2C 1602 LCD<\/li>\n<li>Connecting wires<\/li>\n<\/ul>\n<h4>Code overview<\/h4>\n<ol>\n<li><strong>Include libraries:<\/strong> <code>#include &lt;Wire.h&gt;<\/code> and <code>#include &lt;LiquidCrystal_I2C.h&gt;<\/code> for the LCD.<\/li>\n<li><strong>Initialization:<\/strong> Initializes serial communication, the display, and encoder pins.<\/li>\n<li><strong>Read and display:<\/strong> Reads the encoder outputs to show direction and relative count on the display and in the Serial Monitor.<\/li>\n<\/ol>\n<h4>Advanced wiring<\/h4>\n<ul>\n<li><strong>VCC<\/strong>: 5V (Arduino)<\/li>\n<li><strong>GND<\/strong>: GND (Arduino)<\/li>\n<li><strong>CLK<\/strong>: D4 (Arduino)<\/li>\n<li><strong>DT<\/strong>: D3 (Arduino)<\/li>\n<li><strong>SW<\/strong>: D2 (Arduino)<\/li>\n<li><strong>LCD VCC<\/strong>: 5V (Arduino)<\/li>\n<li><strong>LCD GND<\/strong>: GND (Arduino)<\/li>\n<li><strong>LCD SCL<\/strong>: A5 (Arduino Uno)<\/li>\n<li><strong>LCD SDA<\/strong>: A4 (Arduino Uno)<\/li>\n<\/ul>\n<h4>Library installation<\/h4>\n<p>To use the I2C LCD, install the LiquidCrystal I2C library through Arduino IDE\u2019s Library Manager: open Sketch \u2192 Include Library \u2192 Manage Libraries, search for \u201cLiquidCrystal I2C\u201d, and install it.<\/p>\n<p>Check the pin labels before wiring. The precise pulse count and behavior can vary between encoder versions; the source listing does not identify an exact resolution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>KY-040 incremental rotary encoder module with quadrature CLK\/DT signals and an integrated normally-open push switch. Listed for 5 V DC operation; the listing does not specify pulses per revolution.<\/p>\n","protected":false},"featured_media":53309,"template":"","meta":[],"product_brand":[],"product_cat":[314,316],"product_tag":[],"class_list":["post-53308","product","type-product","status-publish","has-post-thumbnail","product_cat-developpement-en","product_cat-modules-capteurs-en","first","instock","taxable","shipping-taxable","purchasable","product-type-simple"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>KY-040 Rotary Encoder Module for Arduino | MicroPlanet<\/title>\n<meta name=\"description\" content=\"KY-040 incremental rotary encoder with CLK, DT, and SW pins for direction, relative movement, and push input. Includes Arduino examples; check your board pinout.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.microplanet.ma\/produit\/module-encodeur-rotatif-ky-040\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"KY-040 Rotary Encoder Module for Arduino | MicroPlanet\" \/>\n<meta property=\"og:description\" content=\"KY-040 incremental rotary encoder with CLK, DT, and SW pins for direction, relative movement, and push input. Includes Arduino examples; check your board pinout.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.microplanet.ma\/produit\/module-encodeur-rotatif-ky-040\/\" \/>\n<meta property=\"og:site_name\" content=\"MicroPlanet Maroc\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ArduinoPlanet\/\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-19T18:28:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.microplanet.ma\/wp-content\/uploads\/2017\/04\/158-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"1000\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.microplanet.ma\\\/produit\\\/module-encodeur-rotatif-ky-040\\\/\",\"url\":\"https:\\\/\\\/www.microplanet.ma\\\/produit\\\/module-encodeur-rotatif-ky-040\\\/\",\"name\":\"KY-040 Rotary Encoder Module for Arduino | MicroPlanet\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.microplanet.ma\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.microplanet.ma\\\/produit\\\/module-encodeur-rotatif-ky-040\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.microplanet.ma\\\/produit\\\/module-encodeur-rotatif-ky-040\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.microplanet.ma\\\/wp-content\\\/uploads\\\/2017\\\/04\\\/158-1.png\",\"datePublished\":\"2017-04-12T10:42:05+00:00\",\"dateModified\":\"2026-10-19T18:28:08+00:00\",\"description\":\"KY-040 incremental rotary encoder with CLK, DT, and SW pins for direction, relative movement, and push input. Includes Arduino examples; check your board pinout.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.microplanet.ma\\\/produit\\\/module-encodeur-rotatif-ky-040\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.microplanet.ma\\\/produit\\\/module-encodeur-rotatif-ky-040\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.microplanet.ma\\\/produit\\\/module-encodeur-rotatif-ky-040\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.microplanet.ma\\\/wp-content\\\/uploads\\\/2017\\\/04\\\/158-1.png\",\"contentUrl\":\"https:\\\/\\\/www.microplanet.ma\\\/wp-content\\\/uploads\\\/2017\\\/04\\\/158-1.png\",\"width\":1000,\"height\":1000},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.microplanet.ma\\\/produit\\\/module-encodeur-rotatif-ky-040\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.microplanet.ma\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Shop\",\"item\":\"https:\\\/\\\/www.microplanet.ma\\\/shop\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"KY-040 Rotary Encoder Module\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.microplanet.ma\\\/#website\",\"url\":\"https:\\\/\\\/www.microplanet.ma\\\/\",\"name\":\"MicroPlanet Maroc\",\"description\":\"Arduino, Electronique DIY, Modules, Composants, Raspberry PI\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.microplanet.ma\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.microplanet.ma\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.microplanet.ma\\\/#organization\",\"name\":\"MicroPlanet\",\"url\":\"https:\\\/\\\/www.microplanet.ma\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.microplanet.ma\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.microplanet.ma\\\/wp-content\\\/uploads\\\/2023\\\/05\\\/logo5.png\",\"contentUrl\":\"https:\\\/\\\/www.microplanet.ma\\\/wp-content\\\/uploads\\\/2023\\\/05\\\/logo5.png\",\"width\":400,\"height\":133,\"caption\":\"MicroPlanet\"},\"image\":{\"@id\":\"https:\\\/\\\/www.microplanet.ma\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/ArduinoPlanet\\\/\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"KY-040 Rotary Encoder Module for Arduino | MicroPlanet","description":"KY-040 incremental rotary encoder with CLK, DT, and SW pins for direction, relative movement, and push input. Includes Arduino examples; check your board pinout.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.microplanet.ma\/produit\/module-encodeur-rotatif-ky-040\/","og_locale":"en_US","og_type":"article","og_title":"KY-040 Rotary Encoder Module for Arduino | MicroPlanet","og_description":"KY-040 incremental rotary encoder with CLK, DT, and SW pins for direction, relative movement, and push input. Includes Arduino examples; check your board pinout.","og_url":"https:\/\/www.microplanet.ma\/produit\/module-encodeur-rotatif-ky-040\/","og_site_name":"MicroPlanet Maroc","article_publisher":"https:\/\/www.facebook.com\/ArduinoPlanet\/","article_modified_time":"2026-10-19T18:28:08+00:00","og_image":[{"width":1000,"height":1000,"url":"https:\/\/www.microplanet.ma\/wp-content\/uploads\/2017\/04\/158-1.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.microplanet.ma\/produit\/module-encodeur-rotatif-ky-040\/","url":"https:\/\/www.microplanet.ma\/produit\/module-encodeur-rotatif-ky-040\/","name":"KY-040 Rotary Encoder Module for Arduino | MicroPlanet","isPartOf":{"@id":"https:\/\/www.microplanet.ma\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.microplanet.ma\/produit\/module-encodeur-rotatif-ky-040\/#primaryimage"},"image":{"@id":"https:\/\/www.microplanet.ma\/produit\/module-encodeur-rotatif-ky-040\/#primaryimage"},"thumbnailUrl":"https:\/\/www.microplanet.ma\/wp-content\/uploads\/2017\/04\/158-1.png","datePublished":"2017-04-12T10:42:05+00:00","dateModified":"2026-10-19T18:28:08+00:00","description":"KY-040 incremental rotary encoder with CLK, DT, and SW pins for direction, relative movement, and push input. Includes Arduino examples; check your board pinout.","breadcrumb":{"@id":"https:\/\/www.microplanet.ma\/produit\/module-encodeur-rotatif-ky-040\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.microplanet.ma\/produit\/module-encodeur-rotatif-ky-040\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.microplanet.ma\/produit\/module-encodeur-rotatif-ky-040\/#primaryimage","url":"https:\/\/www.microplanet.ma\/wp-content\/uploads\/2017\/04\/158-1.png","contentUrl":"https:\/\/www.microplanet.ma\/wp-content\/uploads\/2017\/04\/158-1.png","width":1000,"height":1000},{"@type":"BreadcrumbList","@id":"https:\/\/www.microplanet.ma\/produit\/module-encodeur-rotatif-ky-040\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.microplanet.ma\/"},{"@type":"ListItem","position":2,"name":"Shop","item":"https:\/\/www.microplanet.ma\/shop\/"},{"@type":"ListItem","position":3,"name":"KY-040 Rotary Encoder Module"}]},{"@type":"WebSite","@id":"https:\/\/www.microplanet.ma\/#website","url":"https:\/\/www.microplanet.ma\/","name":"MicroPlanet Maroc","description":"Arduino, Electronique DIY, Modules, Composants, Raspberry PI","publisher":{"@id":"https:\/\/www.microplanet.ma\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.microplanet.ma\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.microplanet.ma\/#organization","name":"MicroPlanet","url":"https:\/\/www.microplanet.ma\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.microplanet.ma\/#\/schema\/logo\/image\/","url":"https:\/\/www.microplanet.ma\/wp-content\/uploads\/2023\/05\/logo5.png","contentUrl":"https:\/\/www.microplanet.ma\/wp-content\/uploads\/2023\/05\/logo5.png","width":400,"height":133,"caption":"MicroPlanet"},"image":{"@id":"https:\/\/www.microplanet.ma\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ArduinoPlanet\/"]}]}},"_links":{"self":[{"href":"https:\/\/www.microplanet.ma\/en\/wp-json\/wp\/v2\/product\/53308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.microplanet.ma\/en\/wp-json\/wp\/v2\/product"}],"about":[{"href":"https:\/\/www.microplanet.ma\/en\/wp-json\/wp\/v2\/types\/product"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.microplanet.ma\/en\/wp-json\/wp\/v2\/media\/53309"}],"wp:attachment":[{"href":"https:\/\/www.microplanet.ma\/en\/wp-json\/wp\/v2\/media?parent=53308"}],"wp:term":[{"taxonomy":"product_brand","embeddable":true,"href":"https:\/\/www.microplanet.ma\/en\/wp-json\/wp\/v2\/product_brand?post=53308"},{"taxonomy":"product_cat","embeddable":true,"href":"https:\/\/www.microplanet.ma\/en\/wp-json\/wp\/v2\/product_cat?post=53308"},{"taxonomy":"product_tag","embeddable":true,"href":"https:\/\/www.microplanet.ma\/en\/wp-json\/wp\/v2\/product_tag?post=53308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}