Stepper Motor Code

Hi!
I bought a Arduino stepper motor from Jaycar in Australia and I can’t seem to make it turn anticlockwise. I am using it for a school project and need some answers by the 19th of June. It is supposed to once the ultrasonic sensor senses something turn clockwise and then anticlockwise. The stepper motor has rainbow wires and is connected to what I think is called a controller which you then use to connect it to the Arduino
but I have a breadboard in between first.

Here is my code:

#include <Stepper.h>

const int stepsPerRevolution = 120; //RPM
int x; // int
Stepper myStepper(stepsPerRevolution, 13, 12, 11, 10);

const int trigPin = 9;
const int echoPin = 8; //Where I connected all the pins on the arduino

// defines variables
long duration;
int distance;
int safetyDistance;
int previous = 0;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
myStepper.setSpeed(120);
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

safetyDistance = distance;

if (safetyDistance <= 20 ) {
Serial.println(“clockwise”);
myStepper.step(stepsPerRevolution);
myStepper.step(stepsPerRevolution);
myStepper.step(stepsPerRevolution);
myStepper.step(stepsPerRevolution);
delay(1000);
Serial.println(“anticlockwise”);
myStepper.step(stepsPerRevolution);
myStepper.step(stepsPerRevolution);
myStepper.step(stepsPerRevolution);
myStepper.step(stepsPerRevolution);
}
}

Please!!! Help
Thanks In Advance!

Hi ollie2005,

Please use code tags when posting code!

Here is a link that has some code, diagrams, etc that if followed carefully should work!
SmallSteppers - ArduinoInfo.

Let us know how you get on!

kind regards,

Zeb

Hi,
I don't know what code tags are and it still just turns only clockwise with that code.
Thanks for the help though and I hope you can help me further!

ollie2005:
I don’t know what code tags are

Code tags.jpg

Or roll your own [code] sketch goes here [/code]

This code from the link I sent you should work

/* YourDuino.com Example Software Sketch
   Small Stepper Motor and Driver V1.5 06/21/2017
  http://www.yourduino.com/sunshop/index.php?l=product_detail&p=126
   Steps one revolution of output shaft, then back
   terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <Stepper.h>

/*-----( Declare Constants, Pin Numbers )-----*/
//---( Number of steps per revolution of INTERNAL motor in 4-step mode )---
#define STEPS_PER_MOTOR_REVOLUTION 32

//---( Steps per OUTPUT SHAFT of gear reduction )---
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 64  //2048  


/*-----( Declare objects )-----*/
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
//-------------------------------------------------------------
//The pin connections need to be pins 8,9,10,11 connected
// to Motor Driver In1, In2, In3, In4 
//-------------------------------------------------------------

// Then the pins are entered here in the sequence 1-3-2-4 for proper sequencing
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 8, 10, 9, 11);

/*-----( Declare Variables )-----*/
int  Steps2Take;

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
// Nothing  (Stepper Library sets pins as outputs)
}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  Steps2Take  =  STEPS_PER_OUTPUT_REVOLUTION ;  // Rotate CW 1 turn
  small_stepper.setSpeed(500);
  small_stepper.step(Steps2Take);
  delay(1000);

  Steps2Take  =  - STEPS_PER_OUTPUT_REVOLUTION;  // Rotate CCW 1 turn  
  small_stepper.setSpeed(500);  // 700 a good max speed??
  small_stepper.step(Steps2Take);
  delay(2000);

}/* --(end main loop )-- */

/* ( THE END ) */

Let us know how you get on!

Zeb

#include <Stepper.h>Have a look inside this library, and you will see that it fires the pins in a particular order for one direction and uses the opposite order for the opposite direction.

I bought a Arduino stepper motor from Jaycar in Australia

can you post a link ?

The stepper motor has rainbow wires and is connected to what I think is called a controller which you then use to connect it to the Arduino

How many wires does it have ? can you post a picture and a diagram.

if (safetyDistance <= 20 ) {
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  myStepper.step(stepsPerRevolution);
  myStepper.step(stepsPerRevolution);
  myStepper.step(stepsPerRevolution);
  delay(1000);
  Serial.println("anticlockwise");
  myStepper.step(stepsPerRevolution);
  myStepper.step(stepsPerRevolution);
  myStepper.step(stepsPerRevolution);
  myStepper.step(stepsPerRevolution);
  }

You keep telling it to move in the positive direction. No wonder it doesn’t go in the other direction. If you want to go in the OTHER (negative) direction, use “myStepper.step(-stepsPerRevolution);”.

Hi,
Here is a link to the product:
https://www.jaycar.com.au/arduino-compatible-5v-stepper-motor-with-controller/p/XC4458

And I have put in the negative but it still won’t work. I have been trying multiple different ways which is why it doesn’t have the negative in that code.

Hi,
I have tried the code you suggested and it doesn't work. It still only goes clockwise.

Hi Everyone Who Helped,
Thanks for all the help but I figured it out. The motor must be wired wrong so I had to make it 13,11,12,10 instead of 13,12,11,10.

The motor must be wired wrong so I had to make it 13,11,12,10 instead of 13,12,11,10.

Nope: I use that motor and driver with AccelStepper and it’s right that the 2 pins in the middle are swopped:

#define motorPin1  17     // IN1 on the ULN2003 driver 1
#define motorPin2  16     // IN2 on the ULN2003 driver 1
#define motorPin3  15     // IN3 on the ULN2003 driver 1
#define motorPin4  14     // IN4 on the ULN2003 driver 1

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

I found this a good tutorial on that motor and driver.

yeah i think i have one of those or at least one that looks a lot like it. I had some issues with it too, mainly because it is such a slow moving motor. (or at least mine is) and because it needs a rather large time in between steps for it to go both directions. Since i was planning to anyway go beyond the stepper.h library (it really isn’t to good, being so blocking in nature, but with stepper motors what do you expect) i made a testing program (which initially was for 2 motors but this is the version for only one)

#define NR_MOTORS 1

uint8_t steppins[NR_MOTORS][4] = {2, 3, 4, 5};
uint32_t stepdelay[NR_MOTORS] = {2500};
uint32_t zerostep[NR_MOTORS] = {0};
uint32_t laststep[NR_MOTORS] = {0};
bool stepreverse[NR_MOTORS] = {false};

void setup() {
  SetupMotors();
}

void loop() {
  static uint32_t moment = millis();
  CheckSteps();
  if (millis() - moment > 5000) {
    for (uint8_t i = 0; i < NR_MOTORS; i++) {
      stepreverse[i] = !stepreverse[i];
    }
    moment = millis();
  }
}

void SetupMotors() {
  for (uint8_t j = 0; j < NR_MOTORS; j++) {
    for (uint8_t k = 0; k < 4; k++) {
      pinMode(steppins[j][k], OUTPUT);
    }
  }
}

void CheckSteps() {
  static bool inits = true;  
  static uint32_t lastmoment[NR_MOTORS];
  
  if (inits) {
    for (uint8_t j = 0; j < NR_MOTORS; j++) {
      zerostep[j] = micros();
      TakeStep(j, stepreverse[j]);
    }
    inits=false;
    return;
  }
  for (uint8_t j=0; j<NR_MOTORS; j++) {
    uint32_t moment=micros()-zerostep[j];
    uint32_t thisstep=moment / stepdelay[j];
    if (thisstep!=laststep[j]) {
      while (micros()-lastmoment[j]<1500) ; // prevent minimum step
      lastmoment[j]=micros();
      TakeStep(j, stepreverse[j]);
      if (thisstep<laststep[j]) laststep[j]=thisstep;
      else laststep[j]++;
    }
  }
}

void SetDirection(uint8_t motor, bool reverse) {
  if (motor>=NR_MOTORS) return;
  stepreverse[motor] = reverse;
}

void SetSpeed(uint8_t motor, uint8_t spd) {
  if (motor>=NR_MOTORS) return;
  spd=255-spd;
  uint16_t sqr=spd*spd;
  uint32_t moment = micros() - zerostep[motor];
  uint32_t modulo = moment % stepdelay[motor];
  zerostep[motor] = micros() - modulo;
  stepdelay[motor] = 1500 + sqr;
  laststep[motor] = 0;
}

void TakeStep(uint8_t motor, bool reverse) {
  if (motor>=NR_MOTORS) return;
  const uint8_t steps[4] = {0b1001, 0b1100, 0b0110, 0b0011};
  static int8_t currentstep[2] = {0, 0};
  if (reverse) {
    currentstep[motor] -= 1;
    if (currentstep[motor] < 0) currentstep[motor] = 3;
  }
  else {
    currentstep[motor] += 1;
    if (currentstep[motor] > 3) currentstep[motor] = 0;
  }
  for (uint8_t j = 0; j < 4; j++) {
    if (steps[currentstep[motor]] & 1 << j) digitalWrite(steppins[motor][j], HIGH);
    else digitalWrite(steppins[motor][j], LOW);
  }
}

I have the wires connected :
yellow → pin 2
orange → pin 3
red → pin 4
blue → pin 5
i can make it turn both ways when the minimum step length is at least 2500 uS (which is more that the 1500 which it is for most steppers) and since the reduction of the gear is really big it can only move very slow. (and there is quite a lot of play on the axle) As i said i don’t use stepper.h but if you do, you probably will have to set it at a lower speed (and in minus at a lower speed) to make it move both ways, because if the speed is to low it just won’t go both ways.

Actually i hadn't touched these steppers for quite a while, but i found that disconnecting some of the pins made it spin faster, which made me think i also don't have them connected properly as far now.