stepper with motor shield help?

hey folks. working on a project, could use some help. I have the arduino motor shield and a 4 wire, 12v bipolar stepper. I modified an example from instructables, it's currently working. When you press a button, the motor moves continuously in one direction, until you release the button, then it sits still. an LED blinks, indicating the speed of the motor.

What i need it to do is:

  • if the button isn't pressed, do nothing
  • when the button is pressed, move forward a specified amount and stop
  • when the button is released, return to start

here's the code I have so far (working on getting a pot to set the speed of rotation, it's not working yet):

/*

For more information see:
https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/

*/

int delaylength = 0;
const int ledPin =  7;  
const int buttonPin = 2;
int buttonState = 0; 
int potPin = A5;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  
  //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(){
    // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  delaylength = analogRead(potPin/10);

   // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
  digitalWrite(ledPin, HIGH);
  
  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(delaylength);

  //digitalWrite(ledPin, HIGH);
  
  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(delaylength);
  
  digitalWrite(ledPin, LOW);
  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(delaylength);
  
  //digitalWrite(ledPin, LOW);  
  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(delaylength);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }

}

I don't understand why it works well enough to change to code to do what I need. anyone that can explain why it works so I can make it do what I need? I don't understand the channels or the brake etc.

The code in your Original Post is intended for a DC motor, not a stepper motor. It is not a suitable starting point.

To use a stepper motor you should use the Stepper library that is included with the Arduino IDE or the much better AccelStepper library. The documentation for both libraries includes examples to get you started.

...R
Stepper Motor Basics

sardonicmath:
I have the arduino motor shield and a 4 wire, 12v bipolar stepper.

Post a web link to the stepper.

An "Arduino motor shield" (with L298 and without heat sink) could be a poor choice for most steppers.
Leo..

Robin2:
The code in your Original Post is intended for a DC motor, not a stepper motor. It is not a suitable starting point.

To use a stepper motor you should use the Stepper library that is included with the Arduino IDE or the much better AccelStepper library. The documentation for both libraries includes examples to get you started.

...R
Stepper Motor Basics

no it isn't. if you go to the link, that's the example written for the stepper motor, and he has an explanation written about using it with a stepper. The code works, and makes the stepper work.

Wawa:
Post a web link to the stepper.

An “Arduino motor shield” (with L298 and without heat sink) could be a poor choice for most steppers.
Leo…

not sure why it matters, as I said that code above works and makes the stepper move. I’m not asking for electronics help, I’m asking for help understanding why the above code works so I can change it.

why is the motor shield a bad choice?

It is indeed driving a stepper motor. The instructable explains how it works:

To make a bipolar motor spin:

Power the first coil.
Next power the second coil with reverse polarity.
Then power the first coil with reverse polarity.
Finally, power the second coil.
To reverse the motor direction of a bipolar stepper, simply reverse the polarity of the second coil.

Which means you're doing manually what a stepper library would abstract away for you. It works, but it's a bit like pushing your car to the store to buy groceries rather than just driving there.

sardonicmath:
The code works, and makes the stepper work.

Then it is a very strange way to do it. analogWrite() is for DC motors. Stepper motors are digital machines. analogWrite(pin, 255) is the same as digitalWrite(pin, HIGH)

If you use a specialised stepper motor driver that takes step and direction signals then writing your own code is more or less as easy as using a library. However if you are using a pair of h-bridges to drive the motor it is much easier to use one of the stepper libraries.

...R

sardonicmath:
...why is the motor shield a bad choice?

A brushed DC motor shield has a 'fixed voltage' output (supply minus the losses).
The ancient/lossy L298 is such a chip.
Steppers need current controlled chips, like the A4988.

You are able to get away with the L298 with the small high impedance (35 ohm) stepper you have.
But most modern steppers are low impedance, and that would instantly overheat/fry the L298.
Leo..

Wawa:
You are able to get away with the L298 with the small high impedance (35 ohm) stepper you have.
But most modern steppers are low impedance, and that would instantly overheat/fry the L298.

Also, your motor (like all stepper motors) will perform better at speed if you use a much higher voltage to power it. However if you try that with the L298 you will likely fry the motor as well as the L298. Stepper drivers such as the A4988 are able to limit the motor current so that higher voltages don't damage the motor. IIRC the A4988 can operate at up to 35 volts.

...R

Robin2:
Also, your motor (like all stepper motors) will perform better at speed if you use a much higher voltage to power it. However if you try that with the L298 you will likely fry the motor as well as the L298. Stepper drivers such as the A4988 are able to limit the motor current so that higher voltages don't damage the motor. IIRC the A4988 can operate at up to 35 volts.

...R

Ok, do you have a stepper driver you suggest?

sardonicmath:
Ok, do you have a stepper driver you suggest?

The A4988 or the DRV8825 will work fine with the motor in the link in Reply #4. The Pololu website has a lot of useful information about both drivers. And they are widely available.

...R

Depends what you want to use that 35ohm motor for.
There might not be much difference between an L296 and A4988 if you're only going to use a 12volt supply.
Leo..