{"id":1447,"date":"2019-08-23T20:49:58","date_gmt":"2019-08-23T20:49:58","guid":{"rendered":"https:\/\/thezhut.com\/?page_id=1447"},"modified":"2019-08-23T21:11:26","modified_gmt":"2019-08-23T21:11:26","slug":"how-to-use-rotary-encoders-with-your-arduino","status":"publish","type":"page","link":"https:\/\/thezhut.com\/?page_id=1447","title":{"rendered":"How to use Rotary Encoders with your Arduino"},"content":{"rendered":"<h1><\/h1>\n<h1 style=\"text-align: center;\"><em><strong>This page is still under construction!!!<\/strong><\/em><\/h1>\n<h1><\/h1>\n<h1 style=\"text-align: center;\"><span style=\"color: #ff0000;\"><strong>In this Basic Arduino tutorial we will take a look at how to use rotary encoders with your Arduino board.<\/strong><\/span><\/h1>\n<p>&nbsp;<\/p>\n<p><iframe loading=\"lazy\" title=\"How to use Rotary Encoders with your Arduino\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/ZNqIA9W6UMA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<h1 style=\"text-align: center;\"><a href=\"https:\/\/www.facebook.com\/zarduino\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>check us out on Facebook: https:\/\/www.facebook.com\/zarduino\/<\/strong><\/a><\/h1>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1450\" src=\"https:\/\/thezhut.com\/wp-content\/uploads\/2019\/08\/encoder_wiring_6-300x190.jpg\" alt=\"\" width=\"626\" height=\"396\" srcset=\"https:\/\/thezhut.com\/wp-content\/uploads\/2019\/08\/encoder_wiring_6-300x190.jpg 300w, https:\/\/thezhut.com\/wp-content\/uploads\/2019\/08\/encoder_wiring_6-768x486.jpg 768w, https:\/\/thezhut.com\/wp-content\/uploads\/2019\/08\/encoder_wiring_6-1024x648.jpg 1024w, https:\/\/thezhut.com\/wp-content\/uploads\/2019\/08\/encoder_wiring_6.jpg 1251w\" sizes=\"auto, (max-width: 626px) 100vw, 626px\" \/><\/p>\n<p>#define encoderPinA 2 \/\/ right<br \/>\n#define encoderPinB 3 \/\/ left<br \/>\n#define clearButton 8 \/\/ switch<br \/>\nint encoderPos = 0; \/\/ a counter for the dial<br \/>\nunsigned int lastReportedPos = 1; \/\/ change management<br \/>\nstatic boolean rotating = false; \/\/ debounce management<br \/>\nboolean A_set = false;<br \/>\nboolean B_set = false;<\/p>\n<p>void setup() {<\/p>\n<p>pinMode(encoderPinA, INPUT_PULLUP); \/\/enabling pullups<br \/>\npinMode(encoderPinB, INPUT_PULLUP);<br \/>\npinMode(clearButton, INPUT_PULLUP);<br \/>\nattachInterrupt(0, doEncoderA, CHANGE); \/\/pin 2<br \/>\nattachInterrupt(1, doEncoderB, CHANGE); \/\/pin 3<br \/>\nSerial.begin(9600); \/\/ output<br \/>\n}<\/p>\n<p>void loop() {<br \/>\nrotating = true; \/\/ reset the debouncer<\/p>\n<p>if (lastReportedPos != encoderPos) {<br \/>\nSerial.print(&#8220;value = &#8220;);<br \/>\nSerial.println(encoderPos);<br \/>\nlastReportedPos = encoderPos;<br \/>\n}<br \/>\nif (digitalRead(clearButton) == LOW ) {<br \/>\nencoderPos = 0;<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ Interrupt on A changing state<br \/>\nvoid doEncoderA() {<br \/>\n\/\/ debounce<br \/>\nif ( rotating ) delay (1); \/\/ wait a little until the bouncing is done<\/p>\n<p>\/\/ Test transition, did things really change?<br \/>\nif ( digitalRead(encoderPinA) != A_set ) { \/\/ debounce once more<br \/>\nA_set = !A_set;<\/p>\n<p>\/\/ adjust counter + if A leads B<br \/>\nif ( A_set &amp;&amp; !B_set )<br \/>\nencoderPos += 1;<br \/>\nrotating = false; \/\/ no more debouncing until loop() hits again<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ Interrupt on B changing state<br \/>\nvoid doEncoderB() {<br \/>\nif ( rotating ) delay (1);<br \/>\nif ( digitalRead(encoderPinB) != B_set ) {<br \/>\nB_set = !B_set;<br \/>\n\/\/ adjust counter &#8211; 1 if B leads A<br \/>\nif ( B_set &amp;&amp; !A_set )<br \/>\nencoderPos -= 1;<br \/>\nrotating = false;<br \/>\n}<br \/>\n}<\/p>\n<h1><strong><span style=\"color: #ff0000;\">===============================<\/span><\/strong><\/h1>\n<hr \/>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1451\" src=\"https:\/\/thezhut.com\/wp-content\/uploads\/2019\/08\/encoder_wiring_8-300x140.jpg\" alt=\"\" width=\"619\" height=\"289\" srcset=\"https:\/\/thezhut.com\/wp-content\/uploads\/2019\/08\/encoder_wiring_8-300x140.jpg 300w, https:\/\/thezhut.com\/wp-content\/uploads\/2019\/08\/encoder_wiring_8-768x359.jpg 768w, https:\/\/thezhut.com\/wp-content\/uploads\/2019\/08\/encoder_wiring_8-1024x479.jpg 1024w, https:\/\/thezhut.com\/wp-content\/uploads\/2019\/08\/encoder_wiring_8.jpg 1899w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/p>\n<p>#include &lt;Wire.h&gt;<br \/>\n#include &lt;Adafruit_GFX.h&gt;<br \/>\n#include &lt;Adafruit_SSD1306.h&gt;<br \/>\n#define SCREEN_WIDTH 128 \/\/ OLED display width, in pixels<br \/>\n#define SCREEN_HEIGHT 64 \/\/ OLED display height, in pixels<br \/>\n#define OLED_RESET 4 \/\/ Reset pin # (or -1 if sharing Arduino reset pin)<br \/>\nAdafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, OLED_RESET);<\/p>\n<p>int encoderPinA = 2; \/\/ right<br \/>\nint encoderPinB = 3; \/\/ left<br \/>\nint clearButton = 8; \/\/ switch<br \/>\nint encoderPos = 0; \/\/ a counter for the dial<br \/>\nunsigned int lastReportedPos = 1; \/\/ change management<br \/>\nstatic boolean rotating = false; \/\/ debounce management<br \/>\nboolean A_set = false;<br \/>\nboolean B_set = false;<\/p>\n<p>void setup() {<\/p>\n<p>display.begin(SSD1306_SWITCHCAPVCC, 0x3c);<br \/>\ndisplay.setTextColor(WHITE);<br \/>\ndisplay.clearDisplay();<br \/>\ndisplay.setTextSize(4);<\/p>\n<p>pinMode(encoderPinA, INPUT_PULLUP); \/\/enabling pullups<br \/>\npinMode(encoderPinB, INPUT_PULLUP);<br \/>\npinMode(clearButton, INPUT_PULLUP);<br \/>\nattachInterrupt(0, doEncoderA, CHANGE); \/\/pin 2<br \/>\nattachInterrupt(1, doEncoderB, CHANGE); \/\/pin 3<\/p>\n<p>}<\/p>\n<p>void loop() {<br \/>\nrotating = true; \/\/ reset the debouncer<\/p>\n<p>if (lastReportedPos != encoderPos) {<br \/>\nlastReportedPos = encoderPos;<\/p>\n<p>}<br \/>\nif (digitalRead(clearButton) == LOW ) {<br \/>\nencoderPos = 0;<br \/>\n}<br \/>\ndisplay.clearDisplay();<br \/>\ndisplay.setCursor(40, 20); \/\/ col, row<br \/>\ndisplay.println(encoderPos);<br \/>\ndisplay.display();<br \/>\ndelay(100);<br \/>\n}<\/p>\n<p>\/\/ Interrupt on A changing state<br \/>\nvoid doEncoderA() {<br \/>\n\/\/ debounce<br \/>\nif ( rotating ) delay (1); \/\/ wait a little until the bouncing is done<\/p>\n<p>\/\/ Test transition, did things really change?<br \/>\nif ( digitalRead(encoderPinA) != A_set ) { \/\/ debounce once more<br \/>\nA_set = !A_set;<\/p>\n<p>\/\/ adjust counter + if A leads B<br \/>\nif ( A_set &amp;&amp; !B_set )<br \/>\nencoderPos += 1;<\/p>\n<p>rotating = false; \/\/ no more debouncing until loop() hits again<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ Interrupt on B changing state<br \/>\nvoid doEncoderB() {<br \/>\nif ( rotating ) delay (1);<br \/>\nif ( digitalRead(encoderPinB) != B_set ) {<br \/>\nB_set = !B_set;<br \/>\n\/\/ adjust counter &#8211; 1 if B leads A<br \/>\nif ( B_set &amp;&amp; !A_set )<br \/>\nencoderPos -= 1;<\/p>\n<p>rotating = false;<br \/>\n}<br \/>\n}<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This page is still under construction!!! In this Basic Arduino tutorial we will take a look at how to use rotary encoders with your Arduino board. &nbsp; &nbsp; check us out on Facebook: https:\/\/www.facebook.com\/zarduino\/ &nbsp; #define encoderPinA 2 \/\/ right &hellip; <a href=\"https:\/\/thezhut.com\/?page_id=1447\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1382,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-1447","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/1447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thezhut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1447"}],"version-history":[{"count":6,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/1447\/revisions"}],"predecessor-version":[{"id":1458,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/1447\/revisions\/1458"}],"up":[{"embeddable":true,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/1382"}],"wp:attachment":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}