Turning off a stepper motor with code, not hardware

I wanted to turn off a stepper motor without having to do so with a hardware solution and it was very simple to do, but only if you know how. Finding the method was a little buried, so I'm going to post a link here just so other people can find the information. As clarification for newbies like myself, it's the bit about digitalWrite ((pin) LOW).

Most people who have asked were simply grilled about their reason for wanting to do such a thing and then told to use something else (not a stepper). For those of us who have reasons and would simply prefer an answer instead of a life lesson which does not apply, here it is, and thanks go out to the school and teacher for supplying the info:

The site I found:
http://www.me.umn.edu/courses/me2011/arduino/technotes/stepper/arduinoStepper.html

Site's Contents:

Arduino Code for Controlling a Stepper Motor

The Arduino programming environment comes with a function library for controlling a stepper motor.

To use the library, in the Arduino Editor from the top menu bar: Sketch > Import Library > Stepper.

Copy the example code below into an Arduino program.

Arduino code example
Example Code Notes

/*---------------------------------------------
Demo that turns stepper 360 deg in one dir then the other then a wave.
All coils turned off at end to avoid battery drain. 
----------------------------------------------*/

#include <Stepper.h> //include the function library
#define STEPS 48 // 48 steps per rev
Stepper stepper(STEPS, 5, 7, 6, 4); //create the stepper

void setup() 
{
  int i;
  stepper.setSpeed(30); //set speed to 30 rpm
  delay(1000); //pause for effect
  stepper.step(48); //move 360 deg one direction
  delay(1000); //pause for effect
  stepper.step(-48); //move 360 deg in the other direction
  delay(1000); //pause
  stepper.setSpeed(60); //speed up
  for (i=0;i<5;i++) {     
      stepper.step(10);   //wave the flag
      stepper.step(-10);
  }
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  digitalWrite(6,LOW);
  digitalWrite(7,LOW);   
}

void loop() 
{}

The example code assumes that the stepper is being controlled by Arduino pins 4, 5, 6 and 7, that control motor coil 1, 2, 3 and 4 (in that order) but you can use any set of four pins.
The "#define STEPS 96" line defines the number of steps per rev. A 3.75 deg motor has 96 steps/rev while a 7.2 deg motor has 48 steps/rev.
The "Stepper stepper(STEPS, 5, 7, 6, 4)" line is where you enter the four pins used to control the stepper. The order the pins are entered (coil 2, coil 4, coil 3, coil 1) is non-obvious, but can be understood if you want to dive deep into how the Arduino stepper library works.
The "stepper.setSpeed(x)" command sets the motor speed to x rpm.
The "stepper.step(x)" command turns the motor x steps at the speed last set in the stepper.setSpeed() command. The motor turns one direction for postive x and the reverse direction for negative x.
When the example program finishes, two of the coils will remain on, so all coils are turned off to prevent your battery from draining.
If you motor shudders but does not move, it is likely an error in the ordering of the coils.

Arduino Stepper Resources

Arduino tutorial for programming stepper motors:

Arduino stepper motor software library:

Example showing how to control a stepper with a potentiometer:

psyberjock:
When the example program finishes, two of the coils will remain on, so all coils are turned off to prevent your battery from draining.

The downside is that there is nothing to stop the motor moving and no means for the Arduino to know if it has moved.

The normal usage of stepper motors is to leave them powered all the time to hold position.

Specialized stepper drivers (such as a Pololu DRV8825) usually have an enable pin that can be used to turn off power to the motor - but the same problem of losing position exists.

This is a good example of why batteries are not really suitable for powering stepper motors.

If you want a system that holds position without power a DC motor with a worm drive is a good option - but obviously it does not have step-by-step position control.

...R
Stepper Motor Basics
Simple Stepper Code

Many industrial stepper controllers reduce the current if the motor is stationary for a while, to
reduce the heating/power wastage. Since the dynamic torque is usually significantly
less than static torque, you can reduce the stationary current quite a bit since the
dynamic pull-out torque is still the limiting factor for missing steps.

Hi, old thread by now but just wanted to say thanks! Saved me a lot of headache, I've been trying to accomplish the same thing with a power mosfet, complicating the circuit.

For my specific application theres no way the motor will move when powering down since i use a gearbox and there's no force applied to the object when stationary. The enclosure is very small and hence the heat generated by the motor became an issue when the power to the motor was always on ( active movement only some seconds per hour).

OK, now when you want the motor to turn again, you just send the step command or do you have to turn it on again, like setting the pins to HIGH?

rva1945:
OK, now when you want the motor to turn again, you just send the step command or do you have to turn it on again, like setting the pins to HIGH?

This seems to be a completely new question nearly 2 years after this Thread died.

You need to tell us what stepper motor driver you are using. If the driver has an enable pin then that needs to be attended to before any step command is givem.

...R

Robin2:
This seems to be a completely new question nearly 2 years after this Thread died.

You need to tell us what stepper motor driver you are using. If the driver has an enable pin then that needs to be attended to before any step command is givem.

...R

Yes I know I found an old post, thanks for answering.

I'm using the L298N double bridge, 2A per channel. I guess I has enable pins for each channel though they are jumped and I think the stepper library just set the pins to HIGH or LOW.

Is it related to this thread.