Stepper Motor Code

Hi everyone :slight_smile:

I am trying to Program the stepper motor 57HS22 using driver CWD556.
the motor has 1.8 degree for step

I want the stepper motor to move 120.6 degree (not a full revolution) that makes 67 steps
and to stop at each step and take some measurement using some sensor connecting to Arduino as well.

This measurement has to be taken 60 times for one minute (each second one reading) and then take the average of the 60 readings and record it.
Then the stepper motor move to the next step and do the same.

After finishing the 67 steps decide which step is associated with the highest voltage coming from the sensor and return back to it.

I will repeat the same process every 5 min.

my questions is
how can i return back with stepper motor using this code?
I want to know if each pulse means 1.8 degree/one step?
and if i want to make half step 0.9 degree how can i do that?

this is an example code that works very well with stepper
I am trying to modify

int PULSE_PIN = 9;
int DIR_PIN = 10;

void setup() {
  // put your setup code here, to run once:
  pinMode(PULSE_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
}

void loop() {

  // Change the direction...
  digitalWrite(DIR_PIN, HIGH);
  
  // Give some pulse...
  for(int i = 0; i < 100; i++)
  {
    digitalWrite(PULSE_PIN, HIGH);
    delay(10); // millisecond
    digitalWrite(PULSE_PIN, LOW);
    delay(90);
  }
}

The data sheet for the CWD556 lacks a lot In the current setting table it has a line marked ‘default’ but doesn’t say what the default current is if it’s the full 5.6 amps you could burn out your motor.
The table for the steps is even worse, what is the default, 1/2 step or full step? My opinion is that this is primarily a driver for doing multistep on which it seems you don’t need and also for HUGE stepper motors. If your stepper motor only requires less than about 2- 3 amps there are plenty of IC drivers available for a couple of dollars, even modules mounted on a small PC, and they can do microstepping if required. Look for A4988 based stepper motor driver modules. here is an example
http://www.ebay.co.uk/itm/A4988-StepStick-Stepper-Motor-Driver-Module-for-3D-Printer-Reprap-Heat-sink-UK-/131539401047?pt=LH_DefaultDomain_3&hash=item1ea05bf957

“I want to know if each pulse means 1.8 degree/one step?
and if i want to make half step 0.9 degree how can i do that?”

I can’t answer this exactly, 1.8 deg is the full step distance of the stepper. full step, 1/2 step or multistep is a function of the driver and Default doesn’t specify full, half or any other mode. assuming full step

int    measurementresult[67];  // use float or int depending on type of result

int PULSE_PIN = 9;
int DIR_PIN = 10;
int Fwd = HIGH;
int  Rev = LOW;
int Dir;


void setup() {
  // put your setup code here, to run once:
  pinMode(PULSE_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
 digitalWrite(DIR_PIN, Fwd);
  for(int i = 0; i < 67; i++)  // 67 steps 0...66 
  {
    digitalWrite(PULSE_PIN, HIGH);
    delay(10); // millisecond
    digitalWrite(PULSE_PIN, LOW);
    delay(90);
    measurementresult[i]  = takemeasurement();
  }

int stepperpos = 0;
int maxval = 0; //or float depending on result
  for(int i = 0; i < 67; i++){
    if  (measurementresult[i] >= maxval){
      stepperpos = i;
      maxval = measurementresult[i];
    }
  }
 digitalWrite(DIR_PIN, Rev);
  for(int i = (67- stepperpos); i < 67; i++){
        digitalWrite(PULSE_PIN, HIGH);
    delay(10); // 10 millisecond
    digitalWrite(PULSE_PIN, LOW);
    delay(90); // gives 10 millisecond pulse every 100 ms.
  }
  // should be at point of highest reading now
}
//__________________________________________
int takemeasurement(){
  // do something
  int measurement = 1; //or float
  return measurement;
}

the code for every 5 mins is up to you!

If you want advice about your motor and stepper driver please post links to their datasheets

You seem to have a few different questions - which is confusing.

On the one hand you seem to be asking a very basic question about how to make the motor run in the other direction

how can i return back with stepper motor using this code?

but you also seem to be asking how to store 67 values and then figure out which is the highest

This simple stepper code should work with your driver and should provide a basis for testing. The second example is better for a real application as it does not use delay()

Once you are able to make the motor move a single step forwards and backwards the business of taking samples should be simple like this pseudo code

for (byte n = 0; n < 67; n++) {
    moveMotor(1);
    readSample();
}

Assuming the samples are stored in an array you just need to traverse the array to find the biggest element.

Have you planned a system to move the stepper to its HOME position at startup? The Arduino cannot tell where it is without some external detector.

…R
Stepper Motor Basics

Davidl

I am not using default actually i am using the second choice in the driver which is 2.7A peak 1.9A RMS and for the Pulse/rev i am using the second as well which is 1600 pulse/rev

I wounder 1600 pulse/rev gives me 0.225 micro-step means 0.4 degree.

also I want to ask if i need to write int Fwd = HIGH; int Rev = LOW; do I need to write these variables or can I just write high and low at the digitalWrite(DIR_PIN,.....)

and why we need always to give high then delay then low pulses, what will happened if we give just high and if we play with the first delay or the second?

digitalWrite(PULSE_PIN, HIGH); delay(10); // millisecond digitalWrite(PULSE_PIN, LOW); delay(90);

sorry for these basic questions i am really new in the electronics and programming and thank you for the help

Robin2: Have you planned a system to move the stepper to its HOME position at startup? The Arduino cannot tell where it is without some external detector.

Stepper Motor Basics

thank you for reply

Yes i will connect a potentiometer at the shaft of the stepper motor from the back, so as the motor turns the potentiometer turns with it.

i have 10 revolution potentiometer

HumamARDUINO: digitalWrite(PULSE_PIN, HIGH); delay(10); // millisecond digitalWrite(PULSE_PIN, LOW); delay(90);

digitalWrite() is a relatively slow function so most stepper drivers whould work fine with

digitalWrite(PULSE_PIN, HIGH); 
digitalWrite(PULSE_PIN, LOW);
delay(90);

And, in any case, the duration of the HIGH is only required to be a number of microseconds - perhaps 10 µsecs.

Using delay() is fine for testing but don't use it in your "real" code. The second example in this simple stepper code uses millis() or micros() for timing.

...R