# Difference between revisions of "Stepper Motor Tutorial"

## Overview

A stepper motor is a DC motor that is used for making precise adjustments in position by taking discrete steps. There are many types of stepper motors that are used for different functions that vary in motor size, step count, gearing, shaft style, wiring, coils, and phases. This tutorial is for the 28BYJ-48 Stepper Motor and ULN2003A Motor Driver.

## Materials/Prerequisites

• 28BYJ-48 Stepper motor
• ULN2003A Stepper driver
• Jumper wires
• Arduino UNO hardware and software

## Process

### Functioning of Stepper Motors

Stepper motors consist of multiple coils, and by transferring energy from coil to coil, phases are triggered which translate directly to steps being taken by the motor. There are two main types of stepper motors: unipolar and bipolar. Unipolar motors are simple to connect since the positive and negative ends of the circuit are specifically assigned to test leads that will not change as energy flows; however, a disadvantage to these motors is that there is available torque since only half of the coils of the motor are energized at once. On the other hand, bipolar motors use H-bridge circuitry to utilize alternating current rather than direct current. By using alternating current, bipolar motors are more efficient by energizing all of the coils simultaneously.

### Finding a Compatible Stepper Driver for the Stepper Motor

The datasheets of the stepper motor and stepper driver can both be found online simply through a search engine. These datasheets will provide information necessary to take into consideration when choosing a stepper driver to be compatible with a stepper motor. First, find the amps per phase (the maximum current that the motor coils can handle before overheating) and the resistance per phase of the stepper motor. Using Ohm's Law, V = IR, calculate the voltage rating of the motor. Since a constant-voltage driver will be used, the voltage of the stepper motor should match the voltage of the driver. From the datasheet of the driver, the maximum voltage that can flow through the driver and the maximum current that the driver can supply to the motor can be found. It is crucial to choose a stepper driver that is compatible with the stepper motor so that enough current is supplied to energize the coils of the motor yet does not overheat the system.

### Connecting Stepper Motor to Arduino

1. Connect stepper motor and stepper driver by connecting the white plug of the motor into the white opening of the driver.

2. Connect wires from IN1, IN2,IN3, and IN4 of stepper driver, and connect to pins 8, 9, 10, and 11 respectively.

3. Connect negative wire of stepper driver to ground (GND) of Arduino and positive wire of stepper drive to 5V power source of Arduino.

Diagram of stepper motor and motor driver connected to Arduino Uno

### Code

1. Initialize the pins of the stepper motor to pins on the board. For this example code, we assigned the pins to 8, 9, 10, and 11, but they can be assigned to whichever pins are desired. Create variables that will record the position of the motor, the number of steps taken, and the direction in which the motor should turn.

int Pin0 = 8; int Pin1 = 9; int Pin2 = 10; int Pin3 = 11; int _step = 0; int current_steps=0; boolean dir = true;

2. In the setup of the code, use the following syntax to guide the pins to be set to provide an output.

void setup() {

pinMode(Pin0, OUTPUT);
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);


}

3. In the loop of the code, declare each possible case in which the motor can take steps. Since there are 4 pins and we are using the digitalWrite function that sets a pin to either high or low, there are 8 possible cases, as well as a default case which, in this sample code, sets each pin to low.

void loop() {
switch(_step) {
case 0:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
break;
case 1:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, HIGH);
break;
case 2:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
break;
case 3:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
break;
case 4:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 5:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 6:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 7:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
break;
default:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
}


4. This portion of the loop tells directs the stepper motor to either take a step in the counterclockwise direction or clockwise direction. The number of steps is recorded and resets if it reaches its boundary conditions.

if(dir){
_step++;
}else{
_step--;
}
if(_step>7){
_step=0;
}
if(_step<0){
_step=7;
}
delay(1);


}

## Authors

Elizabeth Saliba

Liza Kaniewski

Michael Williams (TA)