Stepper motor

Hi,

I am currently working on a project that require the use of stepper motor. The stepper motor i am using 5V Stepper Motor w/ULN2003 Driver , and a limit switch Micro Limit Switch – Future Electronics Egypt , is connected to the arduino uno. The stepper motor will rotate when the limit switch is off and stop when the limit switch is on. This is to set the home position. But there is something wrong with the coding i wrote and require some guidance. Really appreciate the help! Thanks!!

This is the code i use:

#include <Stepper.h>

#define in1Pin 8
#define in2Pin 9
#define in3Pin 10
#define in4Pin 11

Stepper Vstepper(512, in1Pin, in2Pin, in3Pin, in4Pin);

#define SWITCH 2
char button; // declare a variable button to store the state of the switch

void setup(){

Serial.begin(9600);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);

pinMode(SWITCH, INPUT_PULLUP);

}

void loop(){

button = digitalRead(2);

if (button == HIGH) { // switch not press

digitalWrite(in1Pin, LOW); // +ve coil of 1st magnet
digitalWrite(in2Pin, HIGH); // -ve coil of 1st magnet
digitalWrite(in3Pin, LOW); // +ve coil of 2nd magnet
digitalWrite(in4Pin, HIGH); // -ve coil of 2nd magnet

}

else {

digitalWrite(in1Pin, LOW); // +ve coil of 1st magnet
digitalWrite(in2Pin, LOW); // -ve coil of 1st magnet
digitalWrite(in3Pin, LOW); // +ve coil of 2nd magnet
digitalWrite(in4Pin, LOW); // -ve coil of 2nd magnet

}

}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

"Something is wrong" is not very informative.

Tell us what actually happens and what you want to happen.

If you use the Stepper library it will take care of sequencing the power for the coils.

If you want the motor to move to a limit your code should

  • move 1 step
  • check the switch
  • repeat as necessary

...R

You need to cycle through 4 or 8 states to turn a stepper, not just 2.

Use the Stepper library perhaps?

Hi,

Sorry for the late reply due to my exam period. When i run the code into the arduino, pin 2 and pin 4 LED were lighted up on the driver but the stepper motor did not rotate at all. when the limit switch is pressed, pin 2 and pin 4 lights went off which went according to plan.

The stepper motor suppose to rotate when the pin 2 and pin 4 are lighted up on the driver and stop rotating once the limit switch is pressed where the pin 2 and pin 4 lights will go off.

May i know what is the problem with my code? Is there an alternative to it?

It does not seem as if you read Replies #1 and #2 very carefully.

...R

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks... Tom.... :slight_smile: