# 2.14 Stepper Motor¶

## Overview¶

In this lesson, you will learn about Stepper Motor.

## Fritzing Circuit¶

Power Supply Module is used to power the stepper motor. Get the GND of Mega 2560 Board and GND of ULN2003 connected to the cathode of the breadboard, and connect the VCC of ULN2003 to 5V OUTPUT of Power Supply.

The wiring of ULN2003 and Mega2560 is shown as follows:

## Code¶

Note

• You can open the file `2.14_stepperMotor.ino` under the path of `sunfounder_vincent_kit_for_arduino\code\2.14_stepperMotor` directly.

• Or copy this code into Arduino IDE 1/2.

• Or click Open Code to open it in Web Editor.

• Then Upload the Code to the board.

After uploading the codes to the Mega2560 board, you will be able to see that the stepper motor rotates one circle with an interval of a second and each circle takes 3.75s.

## Code Analysis¶

By calling the library Stepper.h, you can easily drive the stepper motor.

```#include <Stepper.h>
```

Library Functions：

```Stepper(steps, pin1, pin2, pin3, pin4)
```

Creates a new instance of the Stepper class that represents a particular stepper motor attached to your Arduino board.

• `steps`: the number of steps in one revolution of your motor. If your motor gives the number of degrees per step, divide that number into 360 to get the number of steps (e.g. 360 / 3.6 gives 100 steps). (int)

Note

Every circle of the stepper motor takes 2048 steps.

```setSpeed(rpm)
```

Sets the motor speed in rotations per minute. This function doesn’t make the motor turn, just sets the speed at which it will when you call step().

• `rpm`: the speed at which the motor should turn in rotations per minute - a positive number. (long)

Note

The stepper motor we use here rotates 17 circles a minute at most.

```step(steps)
```

Turns the motor a specific number of steps, at a speed determined by the most recent call to setSpeed().

This function is blocking; that is, it will wait until the motor has finished moving to pass control to the next line in your sketch. For example, if you set the speed to, say, 1 RPM and called step(2048) on a 2048-step motor, this function would take a full minute to run. For better control, keep the speed high and only go a few steps with each call to step().

• `steps`: the number of steps to turn the motor - positive to turn one direction, negative to turn the other. (int)