Standard Forward-Backwards Stepper Motor Movement With R3 Shield

Hello all I’m new to Arduino and am having a bit of trouble getting a simple task to preform correctly. I have a simple setup (Stepper motor with belt connected to pulley) and wish for the motor to go Forward for 5 seconds then Backwards for 5 seconds and repeat. Have been working on this for days collecting code, I am able to get the motor to go in both directions sure but with 2 different sketches once I combine them it ends up doing a quick saw like pattern I’m stuck can anyone be of help.?

Casun Stepping Motor Type 42SHD0440-21B,

New Text Document (2).txt (1.2 KB)

Why not use the Stepper library or, better still, AccelStepper?

Post a link to the datasheet for your stepper motor. A shield intended for DC motors is a poor choice for controlling stepper motors. Have a look at these links
Stepper Motor Basics
Simple Stepper Code

also look up the AccelStepper library

…R

The Arduino R3 shield is supposed to run either 1 or 2 DC Brushed motors or 1 Stepper motor at least thats what the documentation tells me? And it does run the motor in each direction just with two different sketches.

Miltonpickle:
The Arduino R3 shield is supposed to run either 1 or 2 DC Brushed motors or 1 Stepper motor at least thats what the documentation tells me?

Indeed.

You could also paint the outside of your house with a 1 inch (25mm) paint brush.

...R

Well then lol.. Thanks for the reply although a little disappointed in the R3 shield not being able to drive a stepper very well. It couldn't pull a Hen of her nest week.. Can someone tell me why these sketches work well when uploaded separately as in Forward works great Backwards also works fine but when I combine them its Sea Saw city jerky B/F motion. I figured with a (delay) between the sketches combined it would have separated them.

int delaylegnth = 30;

void setup() {

//establish motor direction toggle pins
pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???

//establish motor brake pins
pinMode(9, OUTPUT); //brake (disable) CH A
pinMode(8, OUTPUT); //brake (disable) CH B

}

void loop(){

digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B

digitalWrite(12, HIGH); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A

delay(delaylegnth);

digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B

digitalWrite(13, HIGH); //Sets direction of CH B
analogWrite(11, 255); //Moves CH B

delay(delaylegnth);

digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B

digitalWrite(12, LOW); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A

delay(delaylegnth);

digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B

digitalWrite(13, LOW); //Sets direction of CH B
analogWrite(11, 255); //Moves CH B

delay(1000);

digitalWrite(9, HIGH); //ENABLE CH A
digitalWrite(8, LOW); //DISABLE CH B

digitalWrite(12, HIGH); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A

delay(delaylegnth);

digitalWrite(9, LOW); //DISABLE CH A
digitalWrite(8, HIGH); //ENABLE CH B

digitalWrite(13, HIGH); //Sets direction of CH B
analogWrite(11, 255); //Moves CH B

delay(delaylegnth);

digitalWrite(9, HIGH); //ENABLE CH A
digitalWrite(8, LOW); //DISABLE CH B

digitalWrite(12, LOW); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A

delay(delaylegnth);

digitalWrite(9, LOW); //DISABLE CH A
digitalWrite(8, HIGH); //ENABLE CH B

digitalWrite(13, LOW); //Sets direction of CH B
analogWrite(11, 255); //Moves CH B

delay(1000);

}

Hi,
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Miltonpickle:
The Arduino R3 shield is supposed to run either 1 or 2 DC Brushed motors or 1 Stepper motor at least thats what the documentation tells me? And it does run the motor in each direction just with two different sketches.

But not a modern low-impedance bipolar stepper though - and most steppers these days are such.

For those you need a current-controlling chopper driver such as A4988, DRV8825, others exist.

Ok just to make things a little more clear, The R3 Shield does drive the Stepper motor it's the separation of the code from Forwards to Backwards I'm having troubles with. So I need the motor to run for (x) amount of seconds then change direction for (x) seconds and repeat. Ex. Forward for 5 seconds then Reverse for 10 seconds Forward for 10 Back 10 and so forth. I can only assume from the Sea Saw action the code is not separated properly because they both work fine individually.

int delaylegnth = 30;

void setup() {
  
  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B


  
  
}

void loop(){
 
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, HIGH);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, LOW);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  
  delay(1000);
 
  digitalWrite(9, HIGH);  //ENABLE CH A
  digitalWrite(8, LOW); //DISABLE CH B

  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //DISABLE CH A
  digitalWrite(8, HIGH); //ENABLE CH B

  digitalWrite(13, HIGH);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //ENABLE CH A
  digitalWrite(8, LOW); //DISABLE CH B

  digitalWrite(12, LOW);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  
  delay(delaylegnth);
    
  digitalWrite(9, LOW);  //DISABLE CH A
  digitalWrite(8, HIGH); //ENABLE CH B

  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  
  delay(1000);

}

Your code is making the individual steps. It's energising the coils in order to make each step occur. Your last code takes 4 steps 30ms apart then waits 1000ms before taking 4 more steps, then it repeats.

You can write functions to make steps and then your loop can call those functions 100 times or whatever.

But the functions are already written for you. The libraries do this. The basic stepper.h library will do this. AccelStepper.h has more abilities and is my first choice for tasks like "run forwards 10 seconds."

I think I found that motor online, it seems to be 30 ohms 0.4A, so is possible to drive from an H-bridge
motor driver at 12V.

Stepper motors have inertia - instructing them to jump from stationary to a constant speed can
easily stall the motor as the rotor simply cannot accelerate that fast. Hence why AccelStepper library
is the method of choice - you can experiment and find the range of accelerations it can cope with
(this is dependent on lots of factors, not really possible to calculate).

MarkT:
I think I found that motor online, it seems to be 30 ohms 0.4A, so is possible to drive from an H-bridge
motor driver at 12V.

But lots of things are much easier when you use an A4988 stepper driver.

...R