# Arduino Lesson 16. Stepper Motors

## Overview

Stepper motors fall somewhere in between a regular DC motor and a servo motor. They have the advantage that they can be positioned accurately, moved forward or backwards one 'step' at a time, but they can also rotate continuously.

In this lesson you will learn how to control a stepper motor using your Arduino and the same L293D motor control chip that you used with the DC motor in lesson 15.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/489/medium800/learn_arduino_overview.jpg?1396783705)

# Arduino Lesson 16. Stepper Motors

## Parts

To build the project described in this lesson, you will need the following parts.

# Arduino Lesson 16. Stepper Motors

## Breadboard Layout

The stepper motor has five leads, and we will be using both halves of the L293D this time. This means that there are a lot of connections to make on the breadboard.

The motor has a 5-way socket on the end. Push jumper wires into the sockets to allow the motor to be connected to the breadboard.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/495/medium800/learn_arduino_fritzing.jpg?1396783754)

Note that the red lead of the Stepper motor is not connected to anything.

# Arduino Lesson 16. Stepper Motors

## Arduino Code

The following sketch uses the Serial Monitor, so once the sketch is installed and running, open the Serial Monitor and enter a number of 'steps'. Try a value of about 500, this should cause the motor to turn through about 360 degrees. Enter -500 and it will turn back in the reverse direction.

Info: 

```
/*
Adafruit Arduino - Lesson 16. Stepper
*/

#include &lt;Stepper.h&gt;

int in1Pin = 12;
int in2Pin = 11;
int in3Pin = 10;
int in4Pin = 9;

Stepper motor(512, in1Pin, in2Pin, in3Pin, in4Pin);  

void setup()
{
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(in3Pin, OUTPUT);
  pinMode(in4Pin, OUTPUT);

  // this line is for Leonardo's, it delays the serial interface
  // until the terminal window is opened
  while (!Serial);
  
  Serial.begin(9600);
  motor.setSpeed(20);
}

void loop()
{
  if (Serial.available())
  {
    int steps = Serial.parseInt();
    motor.step(steps);
  }
}
```

As you might expect, there is an Arduino library to support stepper motors. This makes the process of using a motor very easy.

After including the 'Stepper' library, the four control pins 'in1' to 'in4' are defined.

To tell the Arduino Stepper library which pins are connected to the motor controller, the following command is used:

```
Stepper motor(768, in1Pin, in2Pin, in3Pin, in4Pin);
```

The first parameter is the number of 'steps' that the motor will take to complete one revolution. The motor can be moved by one step at a time, for very fine positioning.

Serial communications is then started, so that the Arduino is ready to receive commands from the Serial Monitor.

Finally the following command sets the speed that we wish the stepper motor to move, when we subsequently tell it how many steps to rotate.

```
motor.setSpeed(10);
```

The 'loop' function is very simple. It waits for a command to come in from the Serial Monitor and converts the text of the number sent into an int using 'parseInt'. It then instructs the motor to turn that number of steps.&nbsp;

# Arduino Lesson 16. Stepper Motors

## Stepper Motors

Stepper motors use a cogged wheel and electro magnets to nudge the wheel round a 'step' at a time.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/496/medium800/learn_arduino_steppers.png?1396783806)

By energizing the coils in the right order, the motor is driven round. The number of steps that the stepper motor has in a 360 degree rotation is actually the number of teeth on the cog.

The motor we are using has 48 steps, but then the motor also incorporates a reduction gearbox of 1:16 that means that it needs 16 x 48 = 768 steps.

In this lesson, we do not use the common Red connection. This connection is only provided if you are using a different type of drive circuit that does not allow the current in each coil to be reversed. Having a center connection to each coil means that you can either energise the left or right side of the coil, and get the effect of reversing the current flow without having to use a circuit that can reverse the current.

Since we are using a L293D that is very good at reversing the current, we do not need this common connection, we can supply current in either direction to the whole of each of the coils.

# Arduino Lesson 16. Stepper Motors

## Other Things to Do

Try changing the command that sets the speed of the stepper motor:

```
  motor.setSpeed(20);
```

to a lower value (say 5) upload the sketch and notice that the stepper turns more slowly.

Now try and find the maximum speed for the stepper by increasing the speed above 20. After a certain point, you will find that the motor does not move at all. This is because it just cannot keep up with the stream of pulses asking it to step.

Try disconnecting the orange and pink leads of the stepper. It should still turn, but you will notice that it is weaker, as it does not have both coils working to push the motor around.

In the [next Lesson](http://learn.adafruit.com/arduino-lesson-17-email-sending-movement-detector) you will learn how to make an Arduino detect movement using a PIR sensor and then communicate with your computer to send an email.


## Featured Products

### Small Reduction Stepper Motor - 5VDC 32-Step 1/16 Gearing

[Small Reduction Stepper Motor - 5VDC 32-Step 1/16 Gearing](https://www.adafruit.com/product/858)
This is a great first stepper motor, good for small projects and experimenting with steppers. This uni-polar motor has a built-in mounting plate with two mounting holes. There are only 32 steps (11.25 degree) per revolution, and inside is a 1/16&nbsp;reduction gear set. (Actually it's...

In Stock
[Buy Now](https://www.adafruit.com/product/858)
[Related Guides to the Product](https://learn.adafruit.com/products/858/guides)
### Dual H-Bridge Motor Driver for DC or Steppers - 600mA - L293D

[Dual H-Bridge Motor Driver for DC or Steppers - 600mA - L293D](https://www.adafruit.com/product/807)
Run four solenoids, two DC motors, or one bi-polar or uni-polar stepper with up to 600mA per channel using the L293D. These are perhaps better known as "the drivers in our Adafruit Motorshield". If you accidentally damaged the drivers in a shield, you can use one of these puppies to...

In Stock
[Buy Now](https://www.adafruit.com/product/807)
[Related Guides to the Product](https://learn.adafruit.com/products/807/guides)
### Premium Male/Male Jumper Wires - 40 x 6" (150mm)

[Premium Male/Male Jumper Wires - 40 x 6" (150mm)](https://www.adafruit.com/product/758)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 6" (150mm) long and come in a 'strip' of 40 (4 pieces of each of ten rainbow colors). They have 0.1" male header contacts on either end and fit cleanly next to each other...

In Stock
[Buy Now](https://www.adafruit.com/product/758)
[Related Guides to the Product](https://learn.adafruit.com/products/758/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

Out of Stock
[Buy Now](https://www.adafruit.com/product/64)
[Related Guides to the Product](https://learn.adafruit.com/products/64/guides)
### Adafruit METRO 328 Fully Assembled - Arduino IDE compatible

[Adafruit METRO 328 Fully Assembled - Arduino IDE compatible](https://www.adafruit.com/product/50)
We sure love the ATmega328 here at Adafruit, and we use them&nbsp;_a lot_&nbsp;for our own projects. The processor has plenty of GPIO, Analog inputs, hardware UART SPI and I2C, timers and PWM galore - just enough for most simple projects. When we need to go small, we use a <a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/50)
[Related Guides to the Product](https://learn.adafruit.com/products/50/guides)

## Related Guides

- [Light Meter](https://learn.adafruit.com/light-meter.md)
- [Silicone Robo-Tentacle](https://learn.adafruit.com/silicone-robo-tentacle.md)
- [Arduino Lesson 14. Servo Motors](https://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors.md)
- [Trellis 3D Printed Enclosure](https://learn.adafruit.com/trellis-3d-printed-enclosure.md)
- [WiFi Candy Bowl Monitor](https://learn.adafruit.com/wifi-candy-bowl.md)
- [Adafruit Analog Accelerometer Breakouts](https://learn.adafruit.com/adafruit-analog-accelerometer-breakouts.md)
- [Arduino Lesson 12. LCD Displays - Part 2](https://learn.adafruit.com/adafruit-arduino-lesson-12-lcd-displays-part-2.md)
- [Adafruit CC3000 WiFi and Xively](https://learn.adafruit.com/adafruit-cc3000-wifi-and-xively.md)
- [Ladyada's Learn Arduino - Lesson #2](https://learn.adafruit.com/ladyadas-learn-arduino-lesson-number-2.md)
- [A REST API for Arduino & the CC3000 WiFi Chip](https://learn.adafruit.com/a-rest-api-for-arduino-and-the-cc3000-wifi-chip.md)
- [RePaper eInk Development Board](https://learn.adafruit.com/repaper-eink-development-board.md)
- [Memories of an Arduino](https://learn.adafruit.com/memories-of-an-arduino.md)
- [Adafruit VS1053 MP3/AAC/Ogg/MIDI/WAV Codec Breakout Tutorial](https://learn.adafruit.com/adafruit-vs1053-mp3-aac-ogg-midi-wav-play-and-record-codec-tutorial.md)
- [RGB LED Matrix Basics](https://learn.adafruit.com/32x16-32x32-rgb-led-matrix.md)
- [Arduino Lesson 0. Getting Started](https://learn.adafruit.com/lesson-0-getting-started.md)
