element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum If a project calls for servos, how would you change the code to use stepper motors instead?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 2 replies
  • Answers 1 answer
  • Subscribers 390 subscribers
  • Views 740 views
  • Users 0 members are here
  • arduino
Related

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

blood_hound
blood_hound over 5 years ago

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.

  • Sign in to reply
  • Cancel

Top Replies

  • airbornesurfer
    airbornesurfer over 5 years ago +5 verified
    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…
  • airbornesurfer
    0 airbornesurfer over 5 years ago

    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:

    unipolar stepper arduino

    Bipolar:

    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.

     

    Tally-ho!

    • Cancel
    • Vote Up +5 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • easyejl
    0 easyejl over 5 years ago in reply to airbornesurfer

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube