{"id":1116,"date":"2014-05-04T15:22:45","date_gmt":"2014-05-04T15:22:45","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=1116"},"modified":"2014-05-04T15:22:45","modified_gmt":"2014-05-04T15:22:45","slug":"seeedstudio-motorshield-v2-with-stp-42d206-stepper","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2014\/05\/seeedstudio-motorshield-v2-with-stp-42d206-stepper\/","title":{"rendered":"SeeedStudio Motorshield V2 with STP 42D206 Stepper"},"content":{"rendered":"<p>I was trying to use a STP42D306 stepper with a SeeedStudio Motor Shield V2 on an Arduino. This is a bipolar 2 phase stepper motor. I needed to modify the code to get it working as the SeeedStudio example is for a Unipolar motor. The code used is below. And here&#8217;s a pic of the setup:<\/p>\n<p><a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/05\/image.jpeg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/05\/image-300x225.jpeg\" alt=\"image\" width=\"300\" height=\"225\" class=\"aligncenter size-medium wp-image-1117\" srcset=\"https:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/05\/image-300x225.jpeg 300w, https:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/05\/image-1024x768.jpeg 1024w, https:\/\/41j.com\/blog\/wp-content\/uploads\/2014\/05\/image.jpeg 1632w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>No guarantee that I&#8217;ve done it correctly, but the motor spins clockwise\/anticlockwise. There are some nice diagrams of the stepping process <a href=\"http:\/\/www.haydonkerk.com\/Resources\/StepperMotorTheory\/tabid\/192\/Default.aspx\">here<\/a>.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/  Demo function:The application method to drive the stepper motor.\r\n\/\/  Hareware:Stepper motor - 24BYJ48,Seeed's Motor Shield v2.0\r\n\/\/  Author:Frankie.Chu\r\n\/\/  Date:20 November, 2012\r\n#define MOTOR_CLOCKWISE      0\r\n#define MOTOR_ANTICLOCKWISE  1\r\n\/******Pins definitions*************\/\r\n#define MOTORSHIELD_IN1\t8\/\/8\r\n#define MOTORSHIELD_IN2\t11\/\/11\r\n#define MOTORSHIELD_IN3\t12\/\/12\r\n#define MOTORSHIELD_IN4\t13\/\/13\r\n#define CTRLPIN_A\t\t9\/\/9\r\n#define CTRLPIN_B\t\t10\/\/10\r\n\r\nconst unsigned char stepper_ctrl&#x5B;]={0x27,0x36,0x1e,0x0f};\r\n\r\n\r\n\r\n\r\nstruct MotorStruct\r\n{\r\n\tint8_t speed;\r\n\tuint8_t direction;\r\n};\r\nMotorStruct stepperMotor;\r\nunsigned int number_of_steps = 200;\r\n\/**********************************************************************\/\r\n\/*Function: Get the stepper motor rotate                               *\/\r\n\/*Parameter:-int steps,the total steps and the direction the motor rotates.*\/\r\n\/*\t\t\tif steps &gt; 0,rotates anticlockwise,\t\t\t   \t\t\t   *\/\r\n\/*\t\t\tif steps &lt; 0,rotates clockwise.           \t\t\t\t   *\/\r\n\/*Return:\tvoid                      \t\t\t\t\t\t\t      *\/\r\nvoid step(int steps)\r\n{\r\n\tint steps_left = abs(steps)*4;\r\n\tint step_number;\r\n\tint millis_delay = 60L * 1000L \/number_of_steps\/(stepperMotor.speed + 50);\r\ndelay(millis_delay);\r\n\r\n\r\n\tif (steps &gt; 0) \r\n\t{\r\n\t\tstepperMotor.direction= MOTOR_ANTICLOCKWISE;\r\n\t\tstep_number = 0; \r\n\t}\r\n    else if (steps &lt; 0) \r\n\t{\r\n\t\tstepperMotor.direction= MOTOR_CLOCKWISE;\r\n\t\tstep_number = number_of_steps;\r\n\t}\r\n\telse return;\r\n\r\n\r\n\twhile(steps_left &gt; 0) \r\n\t{\r\n  \r\n               if(step_number%4 == 0) {\r\n                 digitalWrite(MOTORSHIELD_IN1,1);\r\n                 digitalWrite(MOTORSHIELD_IN2,0);\r\n                 digitalWrite(MOTORSHIELD_IN3,0);\r\n                 digitalWrite(MOTORSHIELD_IN4,0);\r\n                 digitalWrite(CTRLPIN_A,1);\r\n                 digitalWrite(CTRLPIN_B,0);                                \r\n               }\r\n               if(step_number%4 == 1) {\r\n                 digitalWrite(MOTORSHIELD_IN1,0);\r\n                 digitalWrite(MOTORSHIELD_IN2,0);\r\n                 digitalWrite(MOTORSHIELD_IN3,1);\r\n                 digitalWrite(MOTORSHIELD_IN4,0);\r\n                 digitalWrite(CTRLPIN_A,0);\r\n                 digitalWrite(CTRLPIN_B,1);                                \r\n               }\r\n               if(step_number%4 == 2) {\r\n                 digitalWrite(MOTORSHIELD_IN1,0);\r\n                 digitalWrite(MOTORSHIELD_IN2,1);\r\n                 digitalWrite(MOTORSHIELD_IN3,0);\r\n                 digitalWrite(MOTORSHIELD_IN4,0);\r\n                 digitalWrite(CTRLPIN_A,1);\r\n                 digitalWrite(CTRLPIN_B,0);                                \r\n               }\r\n               if(step_number%4 == 3) {\r\n                 digitalWrite(MOTORSHIELD_IN1,0);\r\n                 digitalWrite(MOTORSHIELD_IN2,0);\r\n                 digitalWrite(MOTORSHIELD_IN3,0);\r\n                 digitalWrite(MOTORSHIELD_IN4,1);\r\n                 digitalWrite(CTRLPIN_A,0);\r\n                 digitalWrite(CTRLPIN_B,1);                                \r\n               }               \r\n  \r\n\t\t\/\/PORTB = stepper_ctrl&#x5B;step_number%4];\r\n\t\tdelay(millis_delay);\r\n\t\tif(stepperMotor.direction== MOTOR_ANTICLOCKWISE)\r\n\t\t{\r\n\t\t\tstep_number++;\r\n\t\t    if (step_number == number_of_steps)\r\n\t\t    \tstep_number = 0;\r\n\t\t}\r\n\t\telse \r\n\t\t{\r\n\t\t\tstep_number--;\r\n\t\t    if (step_number == 0)\r\n\t\t    \tstep_number = number_of_steps;\r\n\t\t}\r\n\t\tsteps_left --;\r\n\t\t\r\n\t}\r\n}\r\nvoid initialize()\r\n{\r\n\tpinMode(MOTORSHIELD_IN1,OUTPUT);\r\n\tpinMode(MOTORSHIELD_IN2,OUTPUT);\r\n\tpinMode(MOTORSHIELD_IN3,OUTPUT);\r\n\tpinMode(MOTORSHIELD_IN4,OUTPUT);\r\n\tpinMode(CTRLPIN_A,OUTPUT);\r\n\tpinMode(CTRLPIN_B,OUTPUT);\r\n\tstop();\r\n\tstepperMotor.speed = 1;\r\n\tstepperMotor.direction = MOTOR_CLOCKWISE;\r\n}\r\n\/*******************************************\/\r\nvoid stop()\r\n{\r\n\t\/*Unenble the pin, to stop the motor. *\/\r\n\tdigitalWrite(CTRLPIN_A,LOW);\r\n    digitalWrite(CTRLPIN_B,LOW);\r\n}\r\n\r\nvoid setup()\r\n{\r\n\tinitialize();\/\/Initialization for the stepper motor.\r\n}\r\n\r\nvoid loop()\r\n{\r\n\tstep(200);\/\/Stepper motors rotate anticlockwise 200 steps.\r\n\tdelay(1000);\r\n\tstep(-200);\/\/Stepper motors rotate clockwise 200 steps.\r\n\tdelay(1000);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was trying to use a STP42D306 stepper with a SeeedStudio Motor Shield V2 on an Arduino. This is a bipolar 2 phase stepper motor. I needed to modify the code to get it working as the SeeedStudio example is for a Unipolar motor. The code used is below. And here&#8217;s a pic of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1116","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-i0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=1116"}],"version-history":[{"count":2,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1116\/revisions"}],"predecessor-version":[{"id":1119,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1116\/revisions\/1119"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=1116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=1116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=1116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}