Arduino Stepper Motor Control

I've had a number of stepper motors sitting in my shop for years. If we really want to do anything fun with our Arduino, we are eventually going to have to run a stepper motor. So, here we go!

A little background on stepper motors and how they are different compared to a typical electric motor. Unlike a conventional electric motor that spins when power is applied, stepper motors incrementally spin (step), allowing control over exactly how far the motor's shaft spins, even to the degree.
Stepper motors have a series of electromagnets that encircle an armature containing a magnet. Think of a compass with a shaft attached to the center of the needle. That needle becomes like the armature of the motor. To spin the armature, electromagnets are turned on and off around the outside of the compass, causing the needle to "step" (or point) from one electromagnet to the next. This picture may help illustrate the movement within a stepper motor.

There are two common types of stepper motors: Bipolar and Unipolar See this article to help explain the differences. I've chosen to use a bipolar stepper motor in this project. It can be somewhat more difficult to use because of the need to reverse polarity on the stepper motor electromagnets to provide the proper stepping. Here's a couple pictures of the motor I used:

A bipolar stepper motor has two electromagnetic winding sets. Because of this, it's necessary to use some circuit to reverse the polarity of the winding. A common way to do this is through the use of an H-Bridge circuit.

I used this circuit to create my H-Bridge controller:

I implemented the circuit on a perf board with a small 6 pin 90 degree header. I also added some LEDs to the circuit, just because everyone likes blinking lights, and I'm no exceptions. After all, that's what got me into this as a child ... blinking lights. Here's a picture of the H-Bridge:

I used the following diagram to pull the whole circuit together. There is an Inferred LED Interrupt sensor in this diagram. I'll talk about that in a subsequent post, so disregard for now.

I loaded the following very simple circuit onto the Arduino. It sets the speed of the motor to 10 RPM. Also, it turns the motor one revolution and then waits 2 seconds before looping.

#include

//create an instance of a stepper motor object
Stepper stepper(96, 8, 9, 10, 11);

void setup()
{
// set the speed of the motor to 10 RPMs
stepper.setSpeed(10);
}

void loop()
{
stepper.step(96); //on revolution
delay(2000); //wait 2 seconds
}

Lastly, here's a video to show this program in action with the whole circuit:





10 comments:

Bob said...

Hello,
I am trying to learn more about steppers and would like to incorporate the optical sensor that you had in your schematic.
If possible , i would like the stepper to return to "Home" with the sensor blocked. I then want to rotate the stepper a known number of puless . This will be connected to an air damper , so I only need 0 to 90 degrees of travel.
I am not much of a coder , so even small steps appear to be mountains at times.
Thanks again for your contribution , I used it to get my first stepper ....stepping!
Build_it_Bob ( Instructibles)

pscmpf said...

Hi Bob,

Here is some code that might help you. In this example, the for loop will run the stepper motor one step at a time, with the LED on until the optical sensor is interrupted - the home position.

Then, it will shut off the LED, wait a second and then rotate the stepper motor 15 steps, then wait 2 seconds, loop to the beginning of the program and reposition it to the home position again...

#include

Stepper stepper(96, 8, 9, 10, 11);

void setup()
{
stepper.setSpeed(150);
pinMode(13, OUTPUT);
}

void loop()
{
for(int i = 1; i <= 96; i++)
{
stepper.step(1);
if(analogRead(5) > 100)
digitalWrite(13, HIGH);
else
{
digitalWrite(13, LOW);
delay(1000);
i=96;
}
}

stepper.step(15);
delay(2000);
}

I hope this is helpful. Feel free to ask another question if you need anymore help!

Mark~

pscmpf said...

Bob, here's a link to a video using code similar to the example above. You'll notice the LED on the Arduino will light when the for loop is running, repositioning the stepper motor to it's home position. I hope this little video is helpful!

http://www.youtube.com/user/pscmpf#p/a/u/1/vnTyceukKio

Mark~

Shane.Evans36 said...

I am trying to build this circuit to activate a latching solenoid and allow the current to flow in either direction so that the solenoid can release when needed.

In your video it has 2 H bridges... Is that needed to make the motor(solenoid) move in either direction?

It may be due to using a 2N3906 transistor instead of the 2N2907 but I cannot seem to get this circuit to work. Any thoughts?

PS I am also using a 9v battery for power source.

pscmpf said...

Hi Shane.Evans36,

Good question. Since the stepper motor is a bipolar motor, we have to run TWO different coils within the motor.

Each of those needs to have its polarity changed to properly move through each step. So, for each of these coils, there's a dedicated H-Bridge to control the polarity.

The stepper motor library identifies four outputs to control the motor stepping. So, each H-Bridge gets two of those pins' output.

Shane - does this help answer your question?

Thanks,

Mark~

Anonymous said...
This comment has been removed by a blog administrator.
pscmpf said...
This comment has been removed by the author.
Anonymous said...
This comment has been removed by a blog administrator.
jj said...

Is it a problem if both inputs are high? Will there be current flow from M1 to M2? Will the transistors be damaged?

Anonymous said...

Hi, great tutorial.

I was wondering if you used the stepper library for this code? I couldn't tell. Also, do you know how to control a stepper motor if I wanted to rotate clockwise for 20 revolutions, pause for 2 seconds, then rotate counter clockwise for 15 revolutions with a RPM of 120.