element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • 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
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • 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 Need Help with Stepper Motor Coding
  • 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 5 replies
  • Subscribers 403 subscribers
  • Views 903 views
  • Users 0 members are here
  • stepper_motor
  • arduino_uno
Related

Need Help with Stepper Motor Coding

Former Member
Former Member over 12 years ago

Hello, I have a NEMA 17 stepper motor with an EasyDriver hooked up to an Arduino Uno. I have a -15 to 15V power supply with 12V powering the EasyDriver. Here is my code and then after the code I will ask my question.

 

int sensorPin = 0;

 

#include <Stepper.h>

 

const int stepsPerRevolution = 200;

 

int dirPin = 8;

 

int stepPin = 9;

 

Stepper myStepper(stepsPerRevolution, dirPin, stepPin);

 

void setup()

{

 

  myStepper.setSpeed(100);

 

  pinMode(dirPin, OUTPUT);

 

  pinMode(stepPin, OUTPUT);

 

  Serial.begin(9600);

}

 

 

void loop()

{

 

  int sensorValue = analogRead(sensorPin);

 

  float voltage = sensorValue * (5.0 / 1023.0);

 

  Serial.println(voltage);

 

  if (voltage > 2.60)

  {

    myStepper.step(100);

   

    delay(1000);

  }

  else if (voltage < 2.40)

  {

    myStepper.step(-100);

 

    delay(1000);

  }

}

 

I have it set at 200 steps per revolution with a speed of 100 RPM. What I want the motor to do is to read the voltage (which it does), turn 180 degrees depending on the voltage, wait one second, and then repeat. With the code I have, it takes three steps, pauses a second, and then repeats. It doesn't go 180 degrees like I want it to do. What can I do to in my code to make it do that?

  • Sign in to reply
  • Cancel

Top Replies

  • colecago
    colecago over 12 years ago +2 verified
    I don't know if you are using library stepper.h correctly. I think the standard Arduino stepper library is for direct driving steppers. Thus Stepper myStepper(stepsPerRevolution, dirPin, stepPin); is meant…
  • colecago
    0 colecago over 12 years ago

    I don't know if you are using library stepper.h correctly. 

    I think the standard Arduino stepper library is for direct driving steppers.  Thus Stepper myStepper(stepsPerRevolution, dirPin, stepPin); is meant to drive the coils, not controls for step and direction.

     

    Look at this link, it contains an example of something with more of a driver library

    http://www.maxuino.org/stepperdriver

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to colecago

    Colecago, just wanted to let you know that you were indeed correct. I went to that link, downloaded that library, and everything is working great! I greatly appreciate your help. Thank you!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • billabott
    0 billabott over 12 years ago

    Did you have to change any of your code to make it work "perfectly".  I think you should post the new sketch here.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 12 years ago in reply to billabott

    This is my new sketch with more if statements than before:

     

    #include <StepperDriver.h>

     

    int sensorPin = 0;

     

    int dirPin = 8;

     

    int stepPin = 9;

     

    const int stepsPerRevolution = 200;

     

    StepperDriver myStepper;

     

    void setup()

    { 

      myStepper.setStep(stepsPerRevolution, dirPin, stepPin);

     

      myStepper.setSpeed(50);

     

      myStepper.step(stepsPerRevolution*8);

     

      Serial.begin(9600);

    }

     

     

    void loop()

    {

     

      int sensorValue = analogRead(sensorPin);

     

      float voltage = sensorValue * (5.0 / 1023.0);

     

      Serial.println(voltage);

     

      if (voltage >= 4.5)

      {

        if (myStepper.update() == 1)

        {

        delay(1000);

       

        myStepper.step(stepsPerRevolution);

        }

      }

      else if (voltage > 4.0 && voltage < 4.5)

      {

        if (myStepper.update() == 1)

        {

        delay(1000);

       

        myStepper.step(stepsPerRevolution/2);

        }

      }

      else if (voltage > 3.5 && voltage <= 4.0)

      {

        if (myStepper.update() == 1)

        {

        delay(1000);

       

        myStepper.step(stepsPerRevolution/4);

        }

      }

      else if (voltage > 3.0 && voltage <= 3.5)

      {

        if (myStepper.update() == 1)

        {

        delay(1000);

       

        myStepper.step(25);

        }

      }

      else if (voltage > 2.6 && voltage <= 3.0)

      {

        if (myStepper.update() == 1)

        {

        delay(1000);

       

        myStepper.step(1);

        }

      }

      else if (voltage > 2.0 && voltage <= 2.4)

      {

        if (myStepper.update() == 1)

        {

        delay(1000);

       

        myStepper.step(-1);

        }

      }

      else if (voltage > 1.5 && voltage <= 2.0)

      {

        if (myStepper.update() == 1)

        {

        delay(1000);

       

        myStepper.step(-25);

        }

      }

      else if (voltage > 1.0 && voltage <= 1.5)

      {

        if (myStepper.update() == 1)

        {

        delay(1000);

       

        myStepper.step(-stepsPerRevolution/4);

        }

      }

      else if (voltage > 0.5 && voltage <= 1.0)

      {

        if (myStepper.update() == 1)

        {

        delay(1000);

       

        myStepper.step(-stepsPerRevolution/2);

        }

      }

      else if (voltage >=0 && voltage <= 0.5)

      {

       if (myStepper.update() == 1)

        {

        delay(1000);

       

        myStepper.step(-stepsPerRevolution);

        }

      }

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • billabott
    0 billabott over 12 years ago in reply to Former Member

    On behalf of the Open Source Community, I thank you.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify 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