{"id":29275,"date":"2020-01-19T18:13:46","date_gmt":"2020-01-19T10:13:46","guid":{"rendered":"\/blog\/?p=29275"},"modified":"2020-11-04T14:57:54","modified_gmt":"2020-11-04T06:57:54","slug":"rotary-encoders-how-it-works-how-to-use-with-arduino","status":"publish","type":"post","link":"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/","title":{"rendered":"Rotary Encoders &#8211; How it works, How to use it with Arduino"},"content":{"rendered":"\n<p>A rotary encoder is a position sensor used to determine the angular position of a rotating shaft. It can be used with an Arduino through modules to achieve such functionality. With two main types of rotary encoder available (Absolute encoder and Incremental encoder) that adopt different functional technologies, one would beg to wonder how does all of them work? <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/blog.seeedstudio.com\/wp-content\/uploads\/2020\/01\/rotary-encoders.png\" alt=\"\" width=\"498\" height=\"299\"\/><\/figure><\/div>\n\n\n\n<p>Hence, in today&#8217;s rotary encoder tutorial, I&#8217;ll be going through all you need to know about rotary encoders and how you can get started using it with Arduino!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Types of Rotary Encoders and How does it work?<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Mechanical Absolute Rotary Encoders<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/static-cdn.seeedstudio.com\/media\/catalog\/product\/cache\/9d0ce51a71ce6a79dfa2a98d65a0f0bd\/h\/t\/httpsstatics3.seeedstudio.comproductswt112a2b_01.jpg\" alt=\"\" width=\"400\" height=\"299\"\/><figcaption><a href=\"https:\/\/www.seeedstudio.com\/Rotary-Encoder-with-Switch-p-667.html\">Rotary Encoder with Switch<\/a><\/figcaption><\/figure><\/div>\n\n\n\n<p>An absolute rotary encoder is one that measures an absolute angle of the encoded shaft through having a unique code for each shaft position. With that, every position of the measurement range\/angle is being identified by a certain code on a disc. This means negates the need for counters as positional values are always directly available even when power is removed from the encoder. <\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>A mechanical absolute encoder is a common low-cost  option that is constructed with a metal disc and works as follow:  <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A metal disc on a shaft is used in conjunction with a stationary pickup device and rotates<\/li><li>When the shaft rotates, a unique code pattern is produced<\/li><li>Which means each position of the shaft has a pattern<\/li><li>This pattern is used to determine the exact position<\/li><\/ul>\n\n\n\n<p>The above explanation applies to how a mechanical absolute rotary sensor works, but there are two other ways to detect rotational position changes; Optical or magnetic sensors changes. We&#8217;ll discuss these two sensing methods below.  <\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Optical Absolute Rotary Encoders<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/media-cdn.seeedstudio.com\/media\/catalog\/product\/cache\/ab187aaa5f626ad16c8031644cd2de5b\/h\/t\/httpsstatics3.seeedstudio.comseeedfile2018-09bazaar924108_1.jpg\" alt=\"\" width=\"350\" height=\"263\"\/><figcaption><a href=\"https:\/\/www.seeedstudio.com\/Grove-Optical-Rotary-Encoder-TCUT1600X01.html\">Grove &#8211; Optical Rotary Encoder<\/a><\/figcaption><\/figure><\/div>\n\n\n\n<p>Optical absolute rotary encoders are constructed with either glass or plastic material disc with transparent and opaque surface areas to allow the light source and photodetector to detect optical patterns. Such detection helps in determining the disc position at any point in time.<\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>The process of determining position with optical rotary encoders:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Disk attached to the shaft rotates<\/li><li>Depending on the pattern of the disk, the light that passes through is either transmitted or blocked <\/li><li>Transmitted light that&#8217;s received is converted to electrical currents in the photodetector<\/li><li>Current converted then becomes digital signals to determine the position, speed, angle, etc.<\/li><\/ol>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Magnetic Absolute Encoders<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/static-cdn.seeedstudio.com\/media\/catalog\/product\/cache\/9d0ce51a71ce6a79dfa2a98d65a0f0bd\/g\/r\/grove-12-bit-magnetic-rotary-sensor-as5600-front.jpg\" alt=\"\" width=\"350\" height=\"263\"\/><figcaption><a href=\"https:\/\/www.seeedstudio.com\/Grove-12-bit-Magnetic-Rotary-Position-Sensor-AS5600-p-4192.html\">Grove &#8211; 12-bit Magnetic Rotary Position Sensor\/Encoder<\/a><\/figcaption><\/figure><\/div>\n\n\n\n<p>Magnetic absolute encoders are constructed with a series of magnetic poles to represent encoder position and sensors which are typically Hall Effect or magnetoresistive. It shares a similar working principle of optical encoders, but instead of light, sensors detect a change in magnetic fields.<\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>The process of determining position with magnetic rotary encoders:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Disk attached rotates<\/li><li>Magnetic sensors detect the change in the magnetic field due to rotation <\/li><li>Such changes are converted into sine waves and amplified into digital signals to produce the desired output<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Incremental Encoder<\/strong><\/h2>\n\n\n\n<p>As compared to absolute encoders, the incremental encoder works by reading changes in angular displacement instead of reading an absolute angle of the encoded shaft. <\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How incremental encoder work<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/1\/1e\/Incremental_directional_encoder.gif\/220px-Incremental_directional_encoder.gif\" alt=\"\" width=\"264\" height=\"154\"\/><figcaption><a href=\"https:\/\/en.wikipedia.org\/wiki\/File:Incremental_directional_encoder.gif\">Rotary Incremental Encoder Sensor Mechanism<\/a><\/figcaption><\/figure><\/div>\n\n\n\n<p>Also known as a quadrature, a rotary incremental encoder has two output signals, A and B, issuing square waves when the encoder shaft rotates. The square wave frequency indicates the speed of shaft rotation, while the A-B phase relationship indicates the direction of rotation.<\/p>\n\n\n\n<p>Whereas some rotary incremental encoders determine the amount of rotation through a separate counter that counts the number of pulses outputted in response to the amount of rotational displacement of the shaft. <\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>Such rotary incremental encoders go through such process of determining position with magnetic rotary encoders: <\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Rotary displacement of the shaft occurs<\/li><li>Resulting from that, the encoder outputs a pulse string accordingly<\/li><li>A separate counter placed at the reference point then counts the number of pulses to produce the desired output <\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Rotary Encoders vs Potentiometer<\/strong><\/h2>\n\n\n\n<p>To further understand rotary encoders, what better way than to compare it to its closest comparison; potentiometer since when considering devices that sense rotation of a shaft, it&#8217;s normally between either. <\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Without going heavy in the comparative details between the two, here&#8217;s why rotary encoders are the more preferred option nowadays:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Digital input as compared to Analog input, making it easily parable with a microcontroller for translation of output<\/li><li>Ability to rotate continuously, providing excellent resolution <\/li><li>Clear angular position, with higher precision, known-contact and no rotation angle limitation<\/li><\/ul>\n\n\n\n<p>However, with that said, there are still <a href=\"https:\/\/www.seeedstudio.com\/Grove-Slide-Potentiometer.html?utm_source=blog&amp;utm_medium=blog\">digital potentiometer<\/a> available that provide easy pairing with microcontroller boards<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Selecting a Rotary Encoder and Application<\/strong><\/h2>\n\n\n\n<p>To ease your selection process in choosing a rotary encoder for your next Arduino project, we&#8217;ve prepared some of our recommendations here from Seeed! You&#8217;ve probably already seen these placements earlier but here&#8217;s a breakdown of what each option has to offer.<\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.seeedstudio.com\/Rotary-Encoder-with-Switch-p-667.html?utm_source=blog&amp;utm_medium=blog\"><strong>Rotary Encoder with Switch <\/strong><\/a><\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/media-cdn.seeedstudio.com\/media\/catalog\/product\/cache\/9d0ce51a71ce6a79dfa2a98d65a0f0bd\/h\/t\/httpsstatics3.seeedstudio.comimagesproductswt112a2b.jpg\" alt=\"\" width=\"401\" height=\"300\"\/><\/figure><\/div>\n\n\n\n<p>Need a simple rotary encoder to help you get started in sensing rotation shaft with Arduino? This recommended option is the one for you!<\/p>\n\n\n\n<p>With a total of 5 pins, 3 on one side for rotary encoding which need a simple circuit to supply DC 5V while the other two go short whenever pressed, you&#8217;re not short of any functionality when using this!<\/p>\n\n\n\n<p>If you&#8217;re interested to find out more and how you can use this rotary encoder with Arduino, do check out <a href=\"https:\/\/www.seeedstudio.com\/Rotary-Encoder-with-Switch-p-667.html\">our product page!<\/a><\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.seeedstudio.com\/Grove-12-bit-Magnetic-Rotary-Position-Sensor-AS5600-p-4192.html?utm_source=blog&amp;utm_medium=blog\"><strong>Grove &#8211; 12-bit Magnetic Rotary Position Sensor \/ Encoder (AS5600)<\/strong> <\/a><\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/static-cdn.seeedstudio.com\/media\/catalog\/product\/cache\/9d0ce51a71ce6a79dfa2a98d65a0f0bd\/g\/r\/grove-12-bit-magnetic-rotary-sensor-as5600-preview.jpg\" alt=\"\" width=\"350\" height=\"263\"\/><\/figure><\/div>\n\n\n\n<p>Need an option that can both work as a magnetic potentiometer or magnetic encoder with excellent reliability and durability? This option is for you!<\/p>\n\n\n\n<p>Not only does it work both ways, but when compared with a traditional encoder\/potentiometer, the Grove &#8211; AS5600 is a non-contact, no angle rotation limited and high precision option! <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>All made possible with the onboard AS5600 that&#8217;s based on the Hall Effect, the built-in&nbsp;Hall sensor<\/li><\/ul>\n\n\n\n<p>Interfacing with Arduino is made simple through our onboard Grove Interface, allowing for plug and play instead of Jumper wires and breadboarding!<\/p>\n\n\n\n<p>If you&#8217;re interested to find out more; its datasheet and how you can use this rotary encoder with Arduino, do check out <a href=\"https:\/\/www.seeedstudio.com\/Grove-12-bit-Magnetic-Rotary-Position-Sensor-AS5600-p-4192.html\">our product page!<\/a><\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.seeedstudio.com\/Grove-Optical-Rotary-Encoder-TCUT1600X01.html?utm_source=blog&amp;utm_medium=blog\"><strong>Grove &#8211; Optical Rotary Encoder<\/strong><\/a><\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/media-cdn.seeedstudio.com\/media\/catalog\/product\/cache\/ab187aaa5f626ad16c8031644cd2de5b\/h\/t\/httpsstatics3.seeedstudio.comseeedfile2018-09bazaar924108_1.jpg\" alt=\"\" width=\"350\" height=\"263\"\/><\/figure><\/div>\n\n\n\n<p>Keeping the best for last is this optical rotary encoder that includes an infrared emitter and two phototransistor detectors. Suitable for not only rotary encoder usage to detect speed\/rotation, but rotation direction as well!<\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>Its features include:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Double phototransistor detectors, can determine the direction of rotation<\/li><li>On-board LED indicators<\/li><li>Grove Interface for ease of usage with Arduino<\/li><\/ul>\n\n\n\n<p><strong>Applications:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Automotive optical sensors<\/li><li>Accurate position sensor for encoder<\/li><li>Sensor for motion, speed, and direction<\/li><li>Sensor for \u201cturn and push\u201d encoding<\/li><\/ul>\n\n\n\n<p>Note: We&#8217;ll be using this rotary encoder for our Arduino tutorial afterwards, but if you&#8217;re looking to find out more information on it, you can head to<a href=\"https:\/\/www.seeedstudio.com\/Grove-Optical-Rotary-Encoder-TCUT1600X01.html\"> our product page!<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Rotary Encoder Tutorial with Arduino<\/strong><\/h2>\n\n\n\n<p>Now we&#8217;ve understood about the principles of Rotary Encoder, let us show you how you can start using one with Arduino through this tutorial!<\/p>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What you&#8217;ll need for this tutorial:<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-gallery columns-3 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\"><ul class=\"blocks-gallery-grid\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"210\" height=\"157\" src=\"https:\/\/blog.seeedstudio.com\/wp-content\/uploads\/2020\/01\/seeeduino-v4.2-9.jpg\" alt=\"\" data-id=\"29289\" data-link=\"\/blog\/?attachment_id=29289\" class=\"wp-image-29289\"\/><figcaption class=\"blocks-gallery-item__caption\"><a href=\"http:\/\/www.seeedstudio.com\/Seeeduino-V4.2-p-2517.html?utm_source=blog&amp;utm_medium=blog\">Seeeduino V4.2 <\/a><\/figcaption><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"210\" height=\"157\" src=\"https:\/\/blog.seeedstudio.com\/wp-content\/uploads\/2020\/01\/base-shield-10.jpg\" alt=\"\" data-id=\"29290\" data-link=\"\/blog\/?attachment_id=29290\" class=\"wp-image-29290\"\/><figcaption class=\"blocks-gallery-item__caption\"><a href=\"https:\/\/www.seeedstudio.com\/Base-Shield-V2-p-1378.html?utm_source=blog&amp;utm_medium=blog\">Grove &#8211; Base Shield<\/a><\/figcaption><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"210\" height=\"157\" src=\"https:\/\/blog.seeedstudio.com\/wp-content\/uploads\/2020\/01\/grove-optical-rotary-encoder.jpg\" alt=\"\" data-id=\"29291\" data-full-url=\"https:\/\/blog.seeedstudio.com\/wp-content\/uploads\/2020\/01\/grove-optical-rotary-encoder.jpg\" data-link=\"\/blog\/?attachment_id=29291\" class=\"wp-image-29291\"\/><figcaption class=\"blocks-gallery-item__caption\"><a href=\"https:\/\/www.seeedstudio.com\/Grove-Optical-Rotary-Encoder%28TCUT1600X01%29-p-3142.html?utm_source=blog&amp;utm_medium=blog\">Grove &#8211; Optical Rotary Encoder<\/a><\/figcaption><\/figure><\/li><\/ul><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>Seeeduino is Seeed&#8217;s very own Arduino board, built with relative advantages over the original<\/li><li>If you wish to use an Arduino board for this tutorial, it&#8217;s applicable for these boards<ul><li>Arduino UNO, Arduino Mega, Arduino Leonardo, Arduino 101, Arduino Due <\/li><\/ul><\/li><\/ul>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Before we begin with assembling the hardware, do take note of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Gently plug in the USB cable to avoid damaging the port and ensure you&#8217;re using the <a href=\"https:\/\/www.seeedstudio.com\/Micro-USB-Cable-48cm-p-1475.html?utm_source=blog&amp;utm_medium=blog\">USB cable with 4 wires<\/a> instead of 2 as it can&#8217;t transfer data<\/li><\/ul>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Hardware Assembly and Pinout:<\/strong><\/h3>\n\n\n\n<p>Before we assemble the hardware together, here&#8217;s a hardware overview of the rotary encoder that we&#8217;re using with its pinout:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/github.com\/SeeedDocument\/Grove-Optical_Rotary_Encoder-TCUT1600X01\/raw\/master\/img\/pin_map.jpg\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Hardware Assembly:<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Step 1:<\/strong> Connect the Grove &#8211; Optical Rotary Encoder to port D5 of the Base Shield<\/li><li><strong>Step 2:<\/strong> Plug Grove &#8211; Base Shield into Seeeduino<\/li><li><strong>Step 3:<\/strong> Connect Seeeduino to PC via a USB cable<\/li><\/ul>\n\n\n\n<p><strong>It should look something like this after completing the above steps:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/github.com\/SeeedDocument\/Grove-Optical_Rotary_Encoder-TCUT1600X01\/raw\/master\/img\/connect.jpg\" alt=\"\" width=\"399\" height=\"266\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Software Configurations and Arduino Library Code<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Step 1: <\/strong>Install the Encoder Library in the Arduino IDE by hovering your cursor to Sketch -&gt; Include Library -&gt; Manage Libraries<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/github.com\/SeeedDocument\/Grove-Optical_Rotary_Encoder-TCUT1600X01\/raw\/master\/img\/path.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Step 2: <\/strong>Search for the encoder in the pop-up window and find Encoder by Paul Stoffregen, choose the Version 1.4.1 and click install<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/github.com\/SeeedDocument\/Grove-Optical_Rotary_Encoder-TCUT1600X01\/raw\/master\/img\/path_1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>When the library is installed, you&#8217;ll see the status turn to &#8220;installed&#8221; and you can now click close<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/github.com\/SeeedDocument\/Grove-Optical_Rotary_Encoder-TCUT1600X01\/raw\/master\/img\/path_2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Step 3:<\/strong> Restart the Arduino IDE. Open the example in either the following three ways:<ul><li>Open it directly in Arduino IDE via the path: File -&gt; Examples -&gt; Encoder -&gt; Basic<\/li><li>Open it in your computer by clicking the&nbsp;<strong>Basic.pde<\/strong>&nbsp;which you can find in the&nbsp;<strong>xxxx\\Arduino\\libraries\\Encoder\\examples\\Basic<\/strong>,&nbsp;<strong>XXXX<\/strong>&nbsp;is the location you installed the Arduino IDE<\/li><li>Copy and paste the following code into a new sketch in Arduino IDE<\/li><\/ul><\/li><\/ul>\n\n\n\n<p><strong>Arduino Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Encoder Library - Basic Example\n * http:\/\/www.pjrc.com\/teensy\/td_libs_Encoder.html\n *\n * This example code is in the public domain.\n *\/\n\n#include &lt;Encoder.h>\n\n\/\/ Change these two numbers to the pins connected to your encoder.\n\/\/   Best Performance: both pins have interrupt capability\n\/\/   Good Performance: only the first pin has interrupt capability\n\/\/   Low Performance:  neither pin has interrupt capability\nEncoder myEnc(5, 6);\n\/\/   avoid using pins with LEDs attached\n\nvoid setup() {\n  Serial.begin(9600);\n  Serial.println(\"Basic Encoder Test:\");\n}\n\nlong oldPosition  = -999;\n\nvoid loop() {\n  long newPosition = myEnc.read();\n  if (newPosition != oldPosition) {\n    oldPosition = newPosition;\n    Serial.println(newPosition);\n  }\n}<\/code><\/pre>\n\n\n\n<p style=\"background-color:#f0fff2\" class=\"has-background\"><strong>Quick Tip:<\/strong> You can change two no. to the pins connected to your encoder. For the best performance; both pins have interrupt capability, so you can change the code line 13 into Encoder myEnc (2, 3). Meanwhile, you should connect the sensor to port D2 of the base shield.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Step 4:<\/strong> Upload the demo. If you&#8217;re unsure on how to do so, do check out <a href=\"http:\/\/wiki.seeedstudio.com\/Upload_Code\/\">our guide on how to upload a code<\/a><\/li><li><strong>Step 5:<\/strong> Open the Serial Monitor of Arduino IDE by clicking Tool -&gt; Serial Monitor or tap the Ctrl + Shift + M key on your keyboard. Set baud rate to 9600<\/li><\/ul>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If you&#8217;ve followed the above tutorial step by step, you should achieve success and getting the below result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Basic Encoder Test:\n0\n1\n2\n3\n4\n3\n2\n1\n0\n-1\n-2\n-3\n-4<\/code><\/pre>\n\n\n\n<p>Now when you move the obstacle from left to right, the count value will increase by 1, and when you move the obstacle from right to left, the count value will decrease by 1!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Summary<\/strong> <\/h2>\n\n\n\n<p>That\u2019s all for today\u2019s guide on Rotary encoders and how you can use it with Arduino. I hope with this, you get a basic understanding of what are rotary encoders, how it works as well!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A rotary encoder is a position sensor used to determine the angular position of a<\/p>\n","protected":false},"author":2850,"featured_media":29293,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_price":"","_stock":"","_tribe_ticket_header":"","_tribe_default_ticket_provider":"","_tribe_ticket_capacity":"0","_ticket_start_date":"","_ticket_end_date":"","_tribe_ticket_show_description":"","_tribe_ticket_show_not_going":false,"_tribe_ticket_use_global_stock":"","_tribe_ticket_global_stock_level":"","_global_stock_mode":"","_global_stock_cap":"","_tribe_rsvp_for_event":"","_tribe_ticket_going_count":"","_tribe_ticket_not_going_count":"","_tribe_tickets_list":"[]","_tribe_ticket_has_attendee_info_fields":false,"iawp_total_views":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-29275","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Rotary Encoders - How it works, How to use it with Arduino - Latest News from Seeed Studio<\/title>\n<meta name=\"description\" content=\"A rotary encoder is a position sensor used to determine the angular position of a rotating shaft. It can be used with an Arduino through modules to achieve such functionality. With two main types of rotary encoder available (Absolute encoder and Incremental encoder) that adopt different functional technologies, one would beg to wonder how does all of them work?\" \/>\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.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Rotary Encoders - How it works, How to use it with Arduino - Latest News from Seeed Studio\" \/>\n<meta property=\"og:description\" content=\"A rotary encoder is a position sensor used to determine the angular position of a rotating shaft. It can be used with an Arduino through modules to achieve such functionality. With two main types of rotary encoder available (Absolute encoder and Incremental encoder) that adopt different functional technologies, one would beg to wonder how does all of them work?\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/\" \/>\n<meta property=\"og:site_name\" content=\"Latest News from Seeed Studio\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-19T10:13:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-11-04T06:57:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png\" \/>\n\t<meta property=\"og:image:width\" content=\"623\" \/>\n\t<meta property=\"og:image:height\" content=\"374\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Shawn\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shawn\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/\",\"url\":\"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/\",\"name\":\"Rotary Encoders - How it works, How to use it with Arduino - Latest News from Seeed Studio\",\"isPartOf\":{\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png\",\"datePublished\":\"2020-01-19T10:13:46+00:00\",\"dateModified\":\"2020-11-04T06:57:54+00:00\",\"author\":{\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/#\/schema\/person\/c9b61b761840d544e2d830b0e3bc9fe2\"},\"description\":\"A rotary encoder is a position sensor used to determine the angular position of a rotating shaft. It can be used with an Arduino through modules to achieve such functionality. With two main types of rotary encoder available (Absolute encoder and Incremental encoder) that adopt different functional technologies, one would beg to wonder how does all of them work?\",\"breadcrumb\":{\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/#primaryimage\",\"url\":\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png\",\"contentUrl\":\"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png\",\"width\":623,\"height\":374,\"caption\":\"Rotary Encoders - How it works and How to use with Arduino\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.seeedstudio.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Rotary Encoders &#8211; How it works, How to use it with Arduino\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/#website\",\"url\":\"https:\/\/www.seeedstudio.com\/blog\/\",\"name\":\"Latest News from Seeed Studio\",\"description\":\"Emerging IoT, AI and Autonomous Applications on the Edge\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.seeedstudio.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/#\/schema\/person\/c9b61b761840d544e2d830b0e3bc9fe2\",\"name\":\"Shawn\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.seeedstudio.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5afdc71205d9d485de69bc4b192294e3?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5afdc71205d9d485de69bc4b192294e3?s=96&r=g\",\"caption\":\"Shawn\"},\"url\":\"https:\/\/www.seeedstudio.com\/blog\/author\/shawn\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Rotary Encoders - How it works, How to use it with Arduino - Latest News from Seeed Studio","description":"A rotary encoder is a position sensor used to determine the angular position of a rotating shaft. It can be used with an Arduino through modules to achieve such functionality. With two main types of rotary encoder available (Absolute encoder and Incremental encoder) that adopt different functional technologies, one would beg to wonder how does all of them work?","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.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/","og_locale":"en_US","og_type":"article","og_title":"Rotary Encoders - How it works, How to use it with Arduino - Latest News from Seeed Studio","og_description":"A rotary encoder is a position sensor used to determine the angular position of a rotating shaft. It can be used with an Arduino through modules to achieve such functionality. With two main types of rotary encoder available (Absolute encoder and Incremental encoder) that adopt different functional technologies, one would beg to wonder how does all of them work?","og_url":"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/","og_site_name":"Latest News from Seeed Studio","article_published_time":"2020-01-19T10:13:46+00:00","article_modified_time":"2020-11-04T06:57:54+00:00","og_image":[{"width":623,"height":374,"url":"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png","type":"image\/png"}],"author":"Shawn","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Shawn","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/","url":"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/","name":"Rotary Encoders - How it works, How to use it with Arduino - Latest News from Seeed Studio","isPartOf":{"@id":"https:\/\/www.seeedstudio.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/#primaryimage"},"image":{"@id":"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/#primaryimage"},"thumbnailUrl":"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png","datePublished":"2020-01-19T10:13:46+00:00","dateModified":"2020-11-04T06:57:54+00:00","author":{"@id":"https:\/\/www.seeedstudio.com\/blog\/#\/schema\/person\/c9b61b761840d544e2d830b0e3bc9fe2"},"description":"A rotary encoder is a position sensor used to determine the angular position of a rotating shaft. It can be used with an Arduino through modules to achieve such functionality. With two main types of rotary encoder available (Absolute encoder and Incremental encoder) that adopt different functional technologies, one would beg to wonder how does all of them work?","breadcrumb":{"@id":"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/#primaryimage","url":"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png","contentUrl":"https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png","width":623,"height":374,"caption":"Rotary Encoders - How it works and How to use with Arduino"},{"@type":"BreadcrumbList","@id":"https:\/\/www.seeedstudio.com\/blog\/2020\/01\/19\/rotary-encoders-how-it-works-how-to-use-with-arduino\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.seeedstudio.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Rotary Encoders &#8211; How it works, How to use it with Arduino"}]},{"@type":"WebSite","@id":"https:\/\/www.seeedstudio.com\/blog\/#website","url":"https:\/\/www.seeedstudio.com\/blog\/","name":"Latest News from Seeed Studio","description":"Emerging IoT, AI and Autonomous Applications on the Edge","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.seeedstudio.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.seeedstudio.com\/blog\/#\/schema\/person\/c9b61b761840d544e2d830b0e3bc9fe2","name":"Shawn","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.seeedstudio.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5afdc71205d9d485de69bc4b192294e3?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5afdc71205d9d485de69bc4b192294e3?s=96&r=g","caption":"Shawn"},"url":"https:\/\/www.seeedstudio.com\/blog\/author\/shawn\/"}]}},"modified_by":"Gigi","views":114117,"featured_image_urls":{"full":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png",623,374,false],"thumbnail":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders-80x80.png",80,80,true],"medium":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders-300x180.png",300,180,true],"medium_large":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png",623,374,false],"large":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png",623,374,false],"1536x1536":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png",623,374,false],"2048x2048":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png",623,374,false],"visody_icon":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png",32,19,false],"magazine-7-slider-full":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png",623,374,false],"magazine-7-slider-center":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png",623,374,false],"magazine-7-featured":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png",623,374,false],"magazine-7-medium":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png",623,374,false],"magazine-7-medium-square":["https:\/\/www.seeedstudio.com\/blog\/wp-content\/uploads\/2020\/01\/rotary-encoders.png",623,374,false]},"author_info":{"display_name":"Shawn","author_link":"https:\/\/www.seeedstudio.com\/blog\/author\/shawn\/"},"category_info":"<a href=\"https:\/\/www.seeedstudio.com\/blog\/category\/news\/\" rel=\"category tag\">News<\/a>","tag_info":"News","comment_count":"0","_links":{"self":[{"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/posts\/29275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/users\/2850"}],"replies":[{"embeddable":true,"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/comments?post=29275"}],"version-history":[{"count":5,"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/posts\/29275\/revisions"}],"predecessor-version":[{"id":37919,"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/posts\/29275\/revisions\/37919"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/media\/29293"}],"wp:attachment":[{"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/media?parent=29275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/categories?post=29275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.seeedstudio.com\/blog\/wp-json\/wp\/v2\/tags?post=29275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}