2 Replies Latest reply on Feb 26, 2020 1:41 PM by easyejl

    If a project calls for servos, how would you change the code to use stepper motors instead?


      I am working on modifying a few projects that call for servos, but I need to change up the code to make it use a stepper motor. I need my projects to have more than a 180-degree spin.



      This is one project that I plan on changing up: https://www.instructables.com/id/Baby-MIT-Cheetah-Robot/


      Code to ^^project above that I want to modify^^: https://content.instructables.com/ORIG/FT5/W9EH/K24YHO6X/FT5W9EHK24YHO6X.unknown


      If anyone can help me figure out how to change the code, PLEASE help me.

        • Re: If a project calls for servos, how would you change the code to use stepper motors instead?

          So, first off, let me just say that steppers and servos are two WHOLLY different beasts that interact with microcontrollers in completely different ways--making translating code from one to another a little trickier than just swapping out a few lines. You really have to rethink how the movements will work and the goals for each. Could you not instead use a 360-degree servo?


          If you still decide that you would prefer a stepper, here's the basics to getting one going:


          First, you'll need to include the stepper library (just like you would with servo).

          #include <Stepper.h>


          You're also going to need to know what kind of motor you're using: unipolar motors have 4 wires and bipolar have 6. It's a minor difference in how you wire the circuit:



          unipolar stepper arduino


          bipolar stepper arduino

          The biggest difference comes from how you interact with the motor. When using a servo, you're defining a value based on the angle that you want the motor to move to. On a stepper, you're going to be defining the number of steps that you want the motor to turn. In other words, you're going to need to know either the number of steps the motor counts as 1 rotation or the number of degrees the motor counts per step.


          To apply this to your example, instead of an analog write to a particular angle for each servo, you're going to "write" (send a step command) for the particular number of steps for the stepper. I'm not exactly sure how this translates to your project, but hopefully that should be enough to get you started. For more information on using the step command, check out the Arduino reference guide https://www.arduino.cc/en/Reference/Stepper


          Heck and I built a Zen garden gantry using a handful of steppers, so there's some good example code and info on the element14 presents project page: Episode 344: Zen Garden Gantry with Matthew Eargle


          Of course, let me know if you're still having trouble.



          4 of 4 people found this helpful
            • Re: If a project calls for servos, how would you change the code to use stepper motors instead?

              Just to throw it in there, you'll have to track the current position of each servo, and initially do a few tests of rotating it 100 or 1000 times (with approximately the load you expect) to see if you are getting more or less than 360 degree rotation for x steps. cheap small consumer grade steppers are accurate enough over a small distance, but they seem to be less accurate than servos in terms of precise position from what i've seen. Part of it is that they have no real "knowledge" if they initially stall due to load then go slightly past the number of steps initially specified due to momentum, particularly if you use microstepping. If position isn't quite as critical, then the stepper has other advantages.

              1 of 1 people found this helpful