{"id":22,"date":"2015-10-29T19:22:04","date_gmt":"2015-10-29T19:22:04","guid":{"rendered":"https:\/\/zhutarduino.wordpress.com\/?page_id=22"},"modified":"2020-10-12T16:26:00","modified_gmt":"2020-10-12T16:26:00","slug":"bipolar-stepper","status":"publish","type":"page","link":"https:\/\/thezhut.com\/?page_id=22","title":{"rendered":"Bipolar stepper motor controlled with rotary encoder"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"ARDUINO Rotary Encoder Controlled Bipolar Stepper Motor with H-bridge tutorial\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/233LfyGHac0?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><strong>\u00a0<\/strong><\/p>\n<p>Stepper motor&#8217;s are very useful for lots of diy projects.\u00a0 In most cases the microcontroller can control all the movements of the stepper, But in some cases you may want to manually control the stepper motor to set a start point or fine tune something.\u00a0 To do this a rotary encoder is the perfect option.<\/p>\n<p>This tutorial will show you how to connect a rotary encoder to your ARDUINO to control a H-Bridge\u00a0that will\u00a0run the stepper motor. The video above shows the circuit in action.<\/p>\n<p><span style=\"text-decoration: underline;\"><em><strong>PARTS YOU WILL NEED:<\/strong><\/em><\/span><\/p>\n<p>#affiliate links#<\/p>\n<ol>\n<li>1 <a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=Rotary Encoder&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=6e57cc52a6252ce1eebd44b6a075de12\" target=\"_blank\" rel=\"noopener noreferrer\">Rotary Encoder<\/a> (one with a push button built in)<\/li>\n<li>1 <a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=Push Button&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=d459a4b974268c176c0b0dbba204afc0\" target=\"_blank\" rel=\"noopener noreferrer\">Push Button<\/a> (only if your rotary encoder does not have a push button)<\/li>\n<li>1 <a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=H-Bridge&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=fb2e72bcaca809a337a4ce3319775c23\" target=\"_blank\" rel=\"noopener noreferrer\">H-Bridge<\/a> chip (L293D or SN754410NE, you can also use a pre assembled stepper control board)<\/li>\n<li>1 <a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=arduino stepper motor&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=d3972d2fe7110a00b6c873ba8fdd4394\" target=\"_blank\" rel=\"noopener noreferrer\">Bipolar stepper motor<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=ARDUINO board&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=b544b1a7efcff4a67e9ab76b3144800a\" target=\"_blank\" rel=\"noopener noreferrer\">ARDUINO<\/a> board that supports interrupts<\/li>\n<li><a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=ARDUINO bread board&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=cba1e8622f70e6f56cae421f4b732634\" target=\"_blank\" rel=\"noopener noreferrer\">bread board<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=ARDUINO jumper wires&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=b7c3e9ebe79c927ae0404b0cc258f336\" target=\"_blank\" rel=\"noopener noreferrer\">jumper wires<\/a><\/li>\n<\/ol>\n<p><a href=\"https:\/\/thezhut.com\/wp-content\/uploads\/2015\/10\/rotaryencodersteppermotor_schem.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-130\" src=\"https:\/\/thezhut.com\/wp-content\/uploads\/2015\/10\/rotaryencodersteppermotor_schem.jpg?w=300\" alt=\"rotaryEncoderStepperMotor_schem\" width=\"816\" height=\"351\" srcset=\"https:\/\/thezhut.com\/wp-content\/uploads\/2015\/10\/rotaryencodersteppermotor_schem.jpg 1815w, https:\/\/thezhut.com\/wp-content\/uploads\/2015\/10\/rotaryencodersteppermotor_schem-300x129.jpg 300w, https:\/\/thezhut.com\/wp-content\/uploads\/2015\/10\/rotaryencodersteppermotor_schem-768x330.jpg 768w, https:\/\/thezhut.com\/wp-content\/uploads\/2015\/10\/rotaryencodersteppermotor_schem-1024x440.jpg 1024w\" sizes=\"auto, (max-width: 816px) 100vw, 816px\" \/><\/a><\/p>\n<p>The picture above shows the circuit you will need to build, make sure the rotary encoder is connected to pins that support interrupts (on the uno its pin 2 &amp; 3).\u00a0 If you click on the picture you can enlarge it.<\/p>\n<p>If you use a different H-Bridge chip or a pre built board you will need to modify the circuit accordingly.<\/p>\n<p>For more info on H-Bridges and how they work check out this link:\u00a0 <a href=\"https:\/\/en.wikipedia.org\/wiki\/H_bridge\" target=\"_blank\" rel=\"noopener noreferrer\">H-Bridge INFO<\/a><\/p>\n<p>Now that you have the circuit built copy the code below and past it into your ARDUINO ide<\/p>\n<p>#include &lt;Stepper.h&gt;<br \/>\nconst int encoderPinA = 2;\u00a0\u00a0 \/\/ right<br \/>\nconst int encoderPinB = 3;\u00a0\u00a0 \/\/ left<br \/>\nconst int encButton = 4;\u00a0\u00a0\u00a0\u00a0 \/\/ encoder push button<br \/>\nint encoderPos = 0;\u00a0\u00a0\u00a0 \/\/ counter<br \/>\nunsigned int lastReportedPos = 1;\u00a0\u00a0 \/\/ change<br \/>\nstatic boolean rotating = false;\u00a0\u00a0\u00a0 \/\/ debounce<br \/>\nboolean A_set = false;<br \/>\nboolean B_set = false;<br \/>\nconst int stepsPerRevolution = 200;<br \/>\nStepper myStepper(stepsPerRevolution, 8, 9, 10, 11);\u00a0 \/\/h-bridge pins<\/p>\n<p>void setup() {<\/p>\n<p>myStepper.setSpeed(60);<br \/>\npinMode(encoderPinA, INPUT_PULLUP); \/\/enabling pullups<br \/>\npinMode(encoderPinB, INPUT_PULLUP);<br \/>\npinMode(encButton, INPUT_PULLUP);<br \/>\nattachInterrupt(0, doEncoderA, CHANGE); \/\/pin 2<br \/>\nattachInterrupt(1, doEncoderB, CHANGE); \/\/pin 3<br \/>\nSerial.begin(9600);<\/p>\n<p>}<\/p>\n<p>void loop() {<br \/>\nrotating = true;\u00a0 \/\/ reset the debouncer<\/p>\n<p>if (lastReportedPos != encoderPos) {<br \/>\nSerial.println(encoderPos);<br \/>\nlastReportedPos = encoderPos;<br \/>\nif (digitalRead(encButton) == LOW )<br \/>\n{<br \/>\nencoderPos = (encoderPos * 100);\u00a0 \/\/change the 100 to how many steps<br \/>\n}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/you want the\u00a0stepper\u00a0to move when<br \/>\nmyStepper.step(encoderPos);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/the button is pushed down<br \/>\nencoderPos = 0;<br \/>\n}<br \/>\n}<\/p>\n<p>void doEncoderA() {<br \/>\n\/\/ debounce<br \/>\nif ( rotating ) delay (1);\u00a0 \/\/ wait a little until the bouncing is done<br \/>\n\/\/ Test transition<br \/>\nif ( digitalRead(encoderPinA) != A_set ) { \/\/ debounce once more<br \/>\nA_set = !A_set;<br \/>\n\/\/ adjust counter + if A leads B<br \/>\nif ( A_set &amp;&amp; !B_set )<br \/>\nencoderPos += 1;\u00a0\u00a0\/\/change the 1 to steps\u00a0to take when encoder turned<br \/>\nrotating = false;\u00a0 \/\/ no more debouncing until loop() hits again<br \/>\n}<br \/>\n}<br \/>\n\/\/ 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\/\/\u00a0 adjust counter &#8211; 1 if B leads A<br \/>\nif ( B_set &amp;&amp; !A_set )<br \/>\nencoderPos -= 1;\u00a0\/\/change the 1 to steps to take when encoder turned<\/p>\n<p>rotating = false;<br \/>\n}<br \/>\n}<\/p>\n<p>Now that you have every thing running turn the rotary encoder, If\u00a0the stepper\u00a0turns the opposite way than what you want just switch 2 wires on one side of the stepper motor (only one side not both).<\/p>\n<p>Next push the button down and turn the encoder, Is should turn 100 steps in the direction you turned the encoder.\u00a0 If you want to change how many steps the stepper moves when you turn the encoder just look at the code, its commented at the 3\u00a0places you need to change it.\u00a0 When you change the push button value remember it will be multiplied by the normal steps value.<\/p>\n<p>I hope this tutorial was helpful, if you have any questions or comments please leave them on our <a href=\"https:\/\/www.facebook.com\/zarduino\/\" target=\"_blank\" rel=\"noopener noreferrer\">Facebook<\/a> page. Also if you want to be notified when new tutorials are added to this site please follow us on <a href=\"https:\/\/www.facebook.com\/zarduino\/\" target=\"_blank\" rel=\"noopener noreferrer\">Facebook.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 Stepper motor&#8217;s are very useful for lots of diy projects.\u00a0 In most cases the microcontroller can control all the movements of the stepper, But in some cases you may want to manually control the stepper motor to set a &hellip; <a href=\"https:\/\/thezhut.com\/?page_id=22\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":10,"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-22","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/22","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thezhut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=22"}],"version-history":[{"count":3,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/22\/revisions"}],"predecessor-version":[{"id":1589,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/22\/revisions\/1589"}],"up":[{"embeddable":true,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/10"}],"wp:attachment":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}