Introduction: Controlling a Stepper Motor With an Arduino

This tutorial will show you how to operate a stepper motor that was salvaged from an old printer with an Arduino.

Step 1: What Is a Stepper Motor?

A stepper motor consists of two main parts, a rotor and a stator. The rotor is the part of the motor that actually spins and provides work. The stator is the stationary part of the motor that houses the rotor. In a stepper motor, the rotor is a permanent magnet. The stator consists of multiple coils that act as electromagnets when an electrical current is passed through them. The electromagnetic coil will cause the rotor to align with it when charged. The rotor is propelled by alternating which coil has a current running through it.

Stepper motors have a number of benefits. They are cheap and easy to use. When there is no current send to the motor, the steppers firmly hold their position. Stepper motors can also rotate without limits and change direction based on the polarity provided.

Step 2: Parts List

Needed Parts

  • Stepper Motor (This motor was salvaged from an old printer)
  • Arduino
  • Insulated Copper Wire
  • Wire Cutters/Strippers
  • Current Regulator
    • Transistor
    • H-Bridge (What will be used in this tutorial)
    • Motor Shield

Optional Parts

  • Soldering Iron
  • Solder
  • Soldering Fan
  • 3rd Hand Tool
  • Safety Glasses

Step 3: Attach the Wires

Most stepper motors have four leads so you will need to cut four pieces of copper wire (note the color does not correlate to anything specific. Different colors were only used to make it easier to see). These leads will be used to control which coil is currently active in the motor. This motor was salvaged from an old printer so soldering the wires on was the easiest option for this project. Anyway you can safely make a connection (solder, plug, clips) will work though.

Step 4: Arduino Sketch

Arduino already has a built in library for stepper motors. Simply go to File > Examples > Stepper > stepper_oneRevolution. Next you are going to want to change the stepsPerRevolution variable to fit your specific motor. After looking up the motors part number on the internet, this particular motor was designed for 48 steps to complete one revolution. What the Stepper library is actually doing is just alternating HIGH and LOW signals to each coil as shown in the GIF.

Step 5: What Is an H-Bridge?

An H-Bridge is a circuit comprised of 4 switches that can safely drive a DC motor or stepper motor. These switches can be relays or (most commonly) transistors. The transistor is a solid state switch that can be closed by sending a small current (signal) to one of its pins. Unlike a single transistor which only allow you to control the speed of a motor, H-bridges allow you to also control the direction in which the motor spins. It does this by opening different switches (the transistors) to allow the current to flow in different directions and thus changing the polarity on the motor. WARNING: Switches 1 and 2 or 3 and 4 should never be closed together. This will cause a short circuit and possible damage to the device.

H-Bridges can help prevent your Arduino from being fried by the motors you are using it drive. Motors are inductors, meaning that they store electrical energy in magnet fields. When current is no longer being sent to the motors, the magnetic energy turns back into electrical energy and can damage components. The H-Bridge helps isolate your Arduino better. You should never plug a motor directly into an Arduino.

Though H-Bridges can be fairly easily built, many opt to buy an H-Bridge (such as a L293NE/SN754410 chip) due to convenience. This is the chip that we will be using in this tutorial. The physical pin numbers and their purpose are listed below.

  • Pin 1 (1, 2EN) ---> Motor 1 Enable/Disable (HIGH/LOW)
  • Pin 2 (1A) ---> Motor 1 Logic Pin 1
  • Pin 3 (1Y) ---> Motor 1 Terminal 1
  • Pin 4 ---> Ground
  • Pin 5 ---> Ground
  • Pin 6 (2Y) ---> Motor 1 Terminal 2
  • Pin 7 (2A) ---> Motor 1 Logic Pin 2
  • Pin 8 (VCC2) ---> Power Supply for Motors
  • Pin 9 ---> Motor 2 Enable/Disable (HIGH/LOW)
  • Pin 10 ---> Motor 2 Logic Pin 1
  • Pin 11 ---> Motor 2 Terminal 1
  • Pin 12 ---> Ground
  • Pin 13 ---> Ground
  • Pin 14 ---> Motor 2 Terminal 2
  • Pin 15 ---> Motor 2 Logic Pin 2
  • Pin 16 (VCC1) ---> Power Supply for H Bridge (5V)

Step 6: Connect the Wires

For a stepper motor, the 4 terminal pins on the H-Bridge should connect to the 4 leads of the motor. The 4 logic pins will then connect to the Arduino (8, 9, 10, and 11 in this tutorial). As shown in the Fritzing diagram, an external power source can be connected to power the motors. The chip can handle an external power source from 4.5V to 36V (I just chose a 9V battery because I'm still new to Fritzing).

Step 7: Upload Code and Test

Upload your code onto your Arduino. If you run your code and everything works as expected then that is awesome! If the wires are put into the wrong pins then the motor will just vibrate instead of fully rotating. Play around with the speed and direction of the motor as you see fit.

You should now have a working stepper motor with your Arduino. What you do next with it is up to you.

Step 8: Referneces & Thanks

The full data sheet for the H-Bridge can be found here.

When I initially posted this I didn't think it would gain the attention that it did. For that reason, I just made a quick instructable that I was planning on editing once all my parts had arrived. I didn't mean to cause so much concern with my previous, sloppy methods. Thanks for all your comments and I have updated my instructable to reflect the more appropriate method of connecting stepper motors.

Comments

author
jacam9803 (author)2017-06-08

I'm new to electronics and I'm trying to hook up an Arduino and an Easy driver to a nema 23 stepper motor with a 12 volt battery as I am going to use this in the field. Can anyone help me as to hook up with a sketch. Some say that I need to boost my voltage. I need to run my motor at 104 RPMs

author
vbpinstructables (author)2017-04-30

Really informative even new febe for electronics..nice article with all details but more interesting are informative comments too.wow.lot I got.keep it up

author
Gogomain (author)2017-04-23

thank you so much for the basic knowledge

author
leeK68 (author)2017-03-31

How do you get the stepper motor in 123D Circuit? I try to find one but the component list only has DC Motor.

author
VinhT17 (author)2016-03-08

If I used a stepper motor to pull back a spring. Will it be able to completely release the spring, and will it also rotate as fast the spring travels?

author
DonovanG (author)VinhT172016-11-21

Only if you turn off the motor entirely. So in theory is should work, that means if you want a solid state system you would want to use MOSFETS and that will be a good system.

author
R Jordan Kreindler (author)2016-09-28

Thank you for taking the time to post this interesting and informative Instructable.

The very best.

author
pachytrance (author)2015-12-29

Thanks

author
SomePolishGuy (author)2015-12-18

Oh Cool!

author
tttapa (author)2015-06-19

You can't just connect a stepper (or any other motor for that matter) directly to an Arduino's outputs. You should always use transistors, mostfets or H-bridges. The output pins of the Arduino can only deliver 40mA, while a typical stepper can draw up to several hundreds of milliamps. This is especially the case at lower step speeds, or under load. It might work for some time, with such a small stepper, at high speeds and with no heavy load connected, but you will almost certainly fry your Arduino. Just use a 10kΩ resistor and a NPN-transistor like the BD139, with a normal rectifier diode between collector and emitter (negative side to collector) to protect the transistor from high voltage peaks caused by self-inductance in the coils of the stepper. You could also use an N-channel mosfet, with a 1MΩ pull down resistor from gate to ground, and a diode between the source and drain. Those few extra components are a better option than destroying your Arduino, I think ;)

author
kavish laxkar (author)tttapa2015-07-09

is it necessary to use protection diode even we r using ln293d ic.?

author
onlycparra (author)tttapa2015-06-24

Hi, tttapa,

How can I do what you say? I need to do something like this instructable, but certainly with that part you say. The problem is that I have no too much knowledge about electronic. If you could show me how to do (for dummies) that amplifying step, I would be very grateful. Thanks

author
tttapa (author)onlycparra2015-06-24

If you have a stepper with five or six wires, you'll need a circuit like this. Otherwise you'll need an H-bridge ic, or build an H-bridge yourself. If you search for 'h bridge bipolar stepper motor' on google images, you'll find plenty of examples. Assuming you have one with 5 or six wires, you need to determine which one(s) the 'common' connections are. Those are the wires in the middle (see schematic). You can look it up in the datasheet of your particular motor, or find out by measuring the resistance. To amplify the signal from the Arduino, you'll need a transistor or MOSFET, and resistors (to limit the current from the Arduino). The diodes are there to prevent the very high voltage peaks, caused by self inductance in the motor, from damaging the transistors. Keep in mind that steppers draw a lot of current, even when idle, so you'll probably need a heatsink for your transistors. Since they are more efficient, MOSFETs don't need this. You also need a decent power supply. The 100mA supplies by the Arduino's usb port is not enough.

temp_405343579.jpg
author
kavish laxkar (author)tttapa2015-07-01

well thnks for knowlege.....now here can i use any N-channel mosfet....or sometng different....I HAVE....P55NF06....it wiil work.?

author
Maximous (author)tttapa2015-06-19

You are absolutely correct. That would definitely be the best way to utilize a stepper motor with an Arduino. I did some quick calculations and with the simple task I was using it for, I didn't feel like it was necessary to add the extra safety precautions. Thanks for the great advice though.

author
dmwatkins (author)Maximous2015-06-19

I was going to point that out, but you already acknowledged it. Good intro to steppers though. And I have to say, this is a well written and very visually appealing Instructable! Well done!

author
Maximous (author)dmwatkins2015-06-21

Thank you so much. It's always good to hear that others are finding my tutorials helpful.

author
hind244 (author)tttapa2015-06-21

Thanks for an advice about safety coil for stepper. I felt, that a direct connection of stepper to Arduico can cause a problem with a ports burned uot become available of high current, dmanded by a motor. Can you draw a diagram of correct connectuon?

author
tttapa (author)tttapa2015-06-19

For this kind of stepper, you probably need an H-bridge. It's a bipolar stepper, and that relies on connecting the right wires to the ground or to the V+. With single transistors you can only control whether it's connected to the V+ or not. This does work for unipolar steppers though, they have one or two wires to connect directly to the ground. (5 or 6 in total). Just for the sake of completeness :)

author
kavish laxkar (author)2015-07-09

is it necessary to use protection diode even we r using ln293d ic.?

author
domints (author)2015-06-23

Please, NO! Do NOT connect things like motors, relays and so on directly to the Arduino. And even if it works for you, do NOT instruct others to do so. It is highly dangerous for your arduino. The most dangerous thing is back EMF when you disconnect inductive load, which can be even few kV high - pretty easily can damage any microcontroller.
Moreover such load draws current. Loads of current. Arduino can safely drive maximum 20mA per pin. Stepper motors have typical resistance about 50Ohms. but might be much more or much less. When you drive such motor from Arduino, which is powered by 5V, it can suck even 5/50 = 0.1 = 100mA of current! 5 times more than it is allowed!!! You're very lucky, that your Ino is still alive! ALWAYS use transistors, and for inductive load, clamping diodes!

author
kavish laxkar (author)domints2015-07-01

in this case wat i have to do....i have connected base to arduino ..and colletor to +6v and emitter to motor and then gnd..is it ok..? plz rply.

author
Maximous (author)domints2015-06-23

This was already discussed in the comments, but thank you for your advice. You are correct that this is not the best option for your Arduino's safety.

author
Antzy Carmasaic (author)2015-06-24

As everyone is saying, this is one of the best ways to burn your arduino. If the motor current draw doesn't kill it, the kickback on coil deactivation will. So instead of only providing criticism, I'll provide solutions for those confused on how to do it properly.

1. For unipolar stepper motor(with 5 or more wires), a cheap ULN2003A transistor array can be used. Tutorial: http://www.arduino.cc/en/Tutorial/StepperUnipolar

2. For bipolar stepper motor(with 4 wires) use the famous L293D H-bridge IC. Adafruit tutorial on controlling a stepper with L293D:

https://learn.adafruit.com/adafruit-arduino-lesson...

author
rjohnson65 (author)2015-06-24

Well Bootloaded ATMega328P-PU chips are cheap enough, the last I bought cost me $3.98 Ea.. from Amazon .

Mainly because I chose not to wait 3 weeks to get them from Shenzhen CN.

"Best Practice" means little to those who have no idea of "Practice"

Docedison

author
rjohnson65 (author)2015-06-24

The "Surge Current" of a stepper motor is WAY beyond the output capacity of an Arduino Uno..

Typically .5 - 2A

I do wonder what mechanical loading was used to verify the motors operational capacity..

I also have doubts about the Arduino Uno's stability, when attempting to use the stepper motor..

Docedison

author
diy_bloke (author)2015-06-24

sticking in the wires at random is making it difficult for yourself. Even without a manual, you can take out most of the guesswork by taking an ohm meter and measuring what coils belong together.
So then you know which pairs go where and at most you have to switch the wires from one pair that is better than having to try some 16 possibilities (or was it 4! =24?)

author
diy_bloke (author)2015-06-24

I have a stepper salvaged from a HP printer. Have not used it yet as I recall it needed quite some current. But most steppers need more current than can safely be sourced directly from your Arduini pins. Would definitely advise a ULN2003 or L293

author

I approve this assembly, think very interesting , just did not like the part connecting the direct Arduino engine , for reason burn digital output , as stated in the datasheet , the Atmega 328P microcontroller port does not support more than 40 milli- ampers delivery of power supply , the rest this work is very good , congratulations !

author
lewac (author)2015-06-23

can't make anything constructive with arduino stuff. for example, no debugger (and that's just for openers). check out the gantry of my PnP machine. driven by a Microchip MCU. You're looking at both the A & Z axis using 200 steps per revolution steppers. btw this ain't no toy (0.002" accuracy on all 4 axis). the machine also runs at a max speed of 1440 inches per minute. finally see those PCB's there in the left foreground? 88 components placed in under 3 minutes ready for the oven.

author
mduarte4 (author)2015-06-23

Never use an microcontroler directly to motor!!!!!

* This is meant to be a basic demonstration of a stepper motor. The best
practice would be to utilize an H-bridge, Transistor, or Motor Shield
when connecting any motor to an Arduino. This will help prevent the
motor from overloading and frying your Arduino.

author
dollarseed (author)2015-06-23

One thing you didn't mention, which should be said: "Always shut down the power to the Arduino Board, before disconnecting or connecting any wires". There is a good chance of burning out the board, if you make changes while it is powered up. Otherwise, a very good, very basic instructable that may help many :)

author
JaunS (author)2015-06-20

Hello, I have built all the hardware: physical structure, and have completed in entering the GRBLtoArduino and all the software. I have used the Universal GCodeSender and has successfully interpreted the software for is says " Grbl 0.8c ['$' for help] ". I've soldered the stepper motors (for I'm using steppers from dvd roms) to some salvages wire from a printer I took apart. I installed some female sockets in order to connect to the male pins on the CNC Shield ver. 2.01 . The orange light from the arduino flashes and in the Universal GCodeSender ver. 0.8 completes the operation b/c is says " ok " . I have no idea whats wrong. I know the steppers work b/c ive used a 9v battery to pulse the motors and they do indeed pulse. Ive used a multimeter to check its resistance and it works. I've checked the current on the motors once connected to the cnc shield and there's current. I have tested if the drivers are in fact getting power and they are. I've attempted the code " x=100 " , " $7=225 " but no motor movement. Can ANYONE help me ?

author
Maximous (author)JaunS2015-06-21

I'm afraid that I have not used the Universal GCodeSender before so I do not know all the details of your code. However, if the code seems to compile and load correctly and you've tested the motors, it may be a simple wiring problem. Do the motors vibrate or anything when you try to run it? Or are they completely motionless?

author
JaunS (author)Maximous2015-06-21

Motionless. Howecer I've used a 9v battery to check if it would pulse and they do. Could it be that my power supply doesn't have sufficient amps? It's 1 amp.

author
Maximous (author)JaunS2015-06-21

I would check the data sheet for the Arduino model you are using. The Arduino Uno for example can only have a max output of 40 mA per I/O pin. Otherwise you would have to have the stepper motor draw the current from an external power source. If your stepper motor has a model number on it then I would google that and check it's data sheet as well.

author
JaunS (author)Maximous2015-06-21

Yeah I have an external supply of 12 v 1amp. Many say that it's the drivers that don't have enough amps . Is that tru ?

author
Maximous (author)JaunS2015-06-21

Just to clarify, your external supply is connected directly to the stepper motor correct? Or it is attached to the arduino first and then the arduino is attached to the stepper motor? And I don't think I fully understand your question. The drivers of what?

author
JaunS (author)Maximous2015-06-21

http://www.google.com/aclk?sa=L&ai=CJsz-qXyHVaT4NI-BpgOz94LwAfGztc4F2bbRvMcBgan9z8cCCAkQCSCTsvocKAlgyQbIAQeqBCdP0H5Zn-8nCy2RGxssLtJDpd5Y6at1JbsN_jZke57RBi9VKVjH5ZTABQWgBibYBgKAB9uAjhWIBwGQBwKoB6a-G9gHAeASuJnqi9frloEe&sig=AOD64_2evuLIOGMoGPIUt-AH-xntmPFwwg&adurl=http://rover.ebay.com/rover/1/711-117182-37290-0/2%3Fmtid%3D1588%26kwid%3D1%26crlp%3D53601919689_324272%26itemid%3D251941874243%26targetid%3D87946122369%26rpc%3D0.20%26rpc_upld_id%3D51110%26rlsatarget%3D%26device%3Dm%26mpre%3Dhttp%253A%252F%252Fwww.ebay.com%252Fitm%252Flike%252F251941874243%253Flpid%253D82%2526chn%253Dps%26adtype%3Dpla&ctype=5&clui=17&rct=j&q=&ved=0CFUQpys&ei=qXyHVanhMoq3yQS4v76wDg

This is similar to what I have. You connect the stepper motors to their driver, the drivers to the CNC shield and then the shield to the arduino. The poser supply is connected to the terminals on the shield lanes " +\-" .

author
chuck767 (author)JaunS2015-06-23

JaunS, could you try a different lint to connecting the drivers to the CNC shield. The link you provided (below) doesn't want to work.

Thanks, Chuck

"http://www.google.com/aclk?sa=L&ai=CJsz-qXyHVaT4NI-BpgOz94LwAfGztc4F2bbRvMcBgan9z8cCCAkQCSCTsvocKAlgyQbIAQeqBCdP0H5Zn-8nCy2RGxssLtJDpd5Y6at1JbsN_jZke57RBi9VKVjH5ZTABQWgBibYBgKAB9uAjhWIBwGQBwKoB6a-G9gHAeASuJnqi9frloEe&sig=AOD64_2evuLIOGMoGPIUt-AH-xntmPFwwg&adurl=http://rover.ebay.com/rover/1/711-117182-37290-0/2%3Fmtid%3D1588%26kwid%3D1%26crlp%3D53601919689_324272%"

author
seamster (author)2015-06-19

Good info, thank you!

About This Instructable

190,707views

789favorites

License:

More by Maximous:Control a Camera with an Arduino and RS232 ShieldPrint Conductive Circuits With An Inkjet PrinterMeasure Capacitance with Arduino
Add instructable to: