SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By ekraft
#160786
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.
Code: Select all
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?
By jremington
#160790
Try lowering the speed to see if that helps. You may be driving the motor too fast and it is skipping most steps. If rapid response is important, you usually have to "ramp up" the speed from low to max.
By Mee_n_Mac
#160793
I don't see anything obviously wrong with your code so simplify the code and see if the motor acts like it should. Command a lower speed in the + direction and let that run w/o halt. See if the motor achieves the commanded speed. If it doesn't I might think the driver or stepper is somehow miswired or not configured correctly (?micro-stepping?).

If it does work try higher speeds and in both directions.
User avatar
By ds18s20
#160851
My 2 cents: are the 3 steps well pronounced? You know -- one can hear whether the steps just click right or it's all a mess in the background and the thing somehow manages to turn 3 steps but it just doesn't sound right? If you grab the shaft by hand the sensation is even more accurate.

The code is too simple to be wrong plus calls to the internal stepper functions are proven to work correctly. Moreover your choice of the EacyDriver in right on the money -- an awesome setup; I have those boards laying around since v2 and they just work.

Does the motor spin fine if you feed a square 50/50 duty cycle signal to the driver? Does it change RPM accordingly if you play with the frequency knob on the generator?
By ekraft
#160962
Just wanted to let you all know that my problem was that I was using the stepper.h library incorrectly. I had to swap it out for a different one that someone recommended to me, and it is working perfectly. Thank you all for your help!