Step Motor help

hi guys i got a stepmotor that i want to go to 120 steps foward and then go back 120 steps to do a 90 degree and 160 degree...
the problem is when power is turn off then depending where it stoped motor will start doing 120 steps foward and 120 backwords,and will not mantain route i wanted so i add a optical switch to tell the motor when it passes there to go 120 down up and then 120 steps down.
but so far not able to do it, so was hoping that someone can take a look in code i did so far

#include <Stepper.h>

#define motorSteps 48
#define motorPin1 8
#define motorPin2 9
#define OpticPin 10
#define LEDPin 13


int Opticstate;

Stepper myStepper(motorSteps, motorPin1, motorPin2);

void setup() {

  Serial.begin(9600);
  pinMode(LEDPin, OUTPUT);
  pinMode(OpticPin, INPUT);


}

void loop() {
  Opticstate = digitalRead(OpticPin);
  Serial.print("estadoinicial=");
  Serial.println(Opticstate);


  if (Opticstate == LOW) {
    Serial.print("right=");
    Serial.println(Opticstate);
    digitalWrite(LEDPin, HIGH);
    myStepper.step(-120);
    myStepper.setSpeed(10);
  }
  else if (Opticstate == HIGH);
  {
    Serial.print("left=");
    Serial.println(Opticstate);
    digitalWrite(LEDPin, LOW);
    myStepper.step(120);
    myStepper.setSpeed(80);
  }

}

If you want to use an optical detector (or any other device) to detect the position of a stepper your program must test the detector after every step.

Rather than using myStepper.step(-120); you may only use myStepper.step(-1); and you must use your own code to count the number of steps.

The usual way to manage a stepper motor is to move it to its Home position when the program starts and then keep count of where it is. It is not safe to assume it was not moved while the power was off.

...R
Stepper Motor Basics

thanks mate got it to work :slight_smile:

im using ULN2003 driver, from datasheet says 500 ma max current, i measure and peaks are arround 590ma, how can i limit max peak to 500ma, what values of resistor be better...
this is circuit shematic im using

There is no image. It's always a good idea to review your own posts.

I doubt if I have an answer even with an image.

...R

Pic is up now, i already burn 1 uln2003, the strange part is when i was using with arduino i had no troubles, now that i set all on breadbord i burn chip after motor work for awhile, sense uln2003 max peak per output current is 500 ma and from my mesures with multimeter is 590 ma per output, so that explain why driver burn, only solution i can see is use 9v instead of 12v to drop the mA, or change resitors values,im just not sure what resistor values would be apropriate.

That is not the symbol for a unipolar motor. Each winding should have a mid-point tap.

What is the winding resistance of your motor? Can you tell us what it actually is
(partnumber, datasheet, link, photo of nameplate....)

is a step motor from a old printer with 6 wires unfortunatly cant find no datasheet, all motor says is 8ZXC

Can you measure the wires in pairs and see which are connected?

is 6 wires , measures are if i measure 1 coil the wings 100 ohm and if is tap and 1 wire 50 ohm, if im correct

yes the center tap should have roughly half the resistance of the coil measured end to end

i actualy have a driver UDN2544B for step motor im just not sure how to wire, anyone in here have any schematic how to wire up,resitences etc to arduino, i have tryed fint info on it cant find nothing other then datasheet,and im not sure how to correct wire up

50 ohm resistance - use a ULN2803 with inputs outputs doubled up in pairs for extra
current handling. The ULN2803 has 8 channels so you can doulbe up and drive 4 phases.