Controlling a Stepper Motor With an Arduino

252,408

797

48

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.

Share

    Recommendations

    • Water Contest

      Water Contest
    • Tiny Home Contest

      Tiny Home Contest
    • Creative Misuse Contest

      Creative Misuse Contest

    48 Discussions

    Hi,

    I like your article, but I need to take it a stage further. I want it to control a model railway turntable.

    What actually starts the movement of the table? I need to be able to press a button, or similar, to move the TT to its' next position.

    I know nothing about the Arduino.

    Can a L293NE chip be used instead of a L293D chip

    Why does your program keep asking me to become a premium member when I already am one?

    I thought if you remove the current to the motor it would just stop. In order to hold you should maintain the last current change that put it there and that will lock it. If the motor is positioned to turn in a horizontal orientation I guess it won't matter much. I found that in a vertical orientation (i.e. elevation antenna) when the current is removed gravity lets it fall to the physical stop position. Any information would be greatly appreciated. dms8921@cableone.net

    Hi, thanks for the very helpful post!

    I have a fundamental question: Can we run 2 tasks simultaneously on arduino mega?

    I have a stepper motor that is moving and a rotary encoder that is encoding the rotations of that motor. The problem is that when I run the code, there is a jerking motion since the arduino stops the stepper motor to be able to read from the encoder. Is there any way to go about this and remove this jerking motion?

    Many thanks for any help/advice on the issue!

    Best,

    Daniel

    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

    1 reply

    it require 200 steps for 1 revolution , for a minute time you need 200*104 steps or

    345 steps per second and no need to boost voltage from 12v.

    https://plus.google.com/collection/o7OlEE

    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

    thank you so much for the basic knowledge

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

    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?

    1 reply

    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.

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

    The very best.

    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 ;)

    3 replies

    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

    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