# Controlling Stepper Motors

### Overview: &#x20;

Stepper motors are used when a high degree of control and accuracy is needed and speed is not. 3D  printers, CNC machines,surveillance devices, medical controls, cameras, andmany other items are  operated, in part, by a stepper motor. Stepper motors could also be used to control a small vehicle if  you wanted its motion to be very precise! Check out&#x20;

<http://42bots.com/tutorials/28byj-48-stepper> motor-with-uln2003-driver-and-arduino-uno/

This lesson will show you the basics of how to drive a  stepper motor using an Arduino, a stepper motor driver, and a stepper motor. &#x20;

### Parts: &#x20;

1 – Arduino Uno

1 – 28BYJ – 48 Stepper Motor

1 – ULN2003 Stepper Motor Driver Board

Assorted Wiring

### Wiring: &#x20;

![](/files/QhI4DEZynSGR3dfFOGo7)

![](/files/nnSwfPnIevORJdZn8trw)

### Code: &#x20;

```
/*-----( Import needed libraries )-----*/
#include <Stepper.h>
/*-----( Declare Constants, Pin Numbers )-----*/
//---( Number of steps per revolution of INTERNAL motor in 4-step mode )---
#define STEPS_PER_MOTOR_REVOLUTION 32
//---( Steps per OUTPUT SHAFT of gear reduction )---
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 64 //2048  
/*-----( Declare objects )-----*/
// create an instance of the stepper class, specifying
// the number ofsteps of the motor and the pins it's
// attached to
//The pin connections need to be 4 pins connected
// to Motor Driver In1, In2, In3, In4 and then the pins entered
// here in the sequence 1-3-2-4 for proper sequencing
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 8, 10, 9, 11);

/*-----( Declare Variables )-----*/
int Steps2Take;

void setup() /*----( SETUP: RUNS ONCE )----*/
{
  // Nothing (Stepper Library sets pins as outputs)
}

void loop()
{
  // In slow motion, this is the 4 step sequence that
  //is required to make the motor take one step
  small_stepper.setSpeed(1);
  Steps2Take = 4; // Rotate CW
  small_stepper.step(Steps2Take);
  delay(2000);
  // now we will make the motor rotate half a revolution at a moderate speed
  Steps2Take = STEPS_PER_OUTPUT_REVOLUTION / 2;
  small_stepper.setSpeed(100);
  small_stepper.step(Steps2Take);
  delay(1000);
  //now we will have it rotate half a revolution backwards at a quicker pace
  Steps2Take = - STEPS_PER_OUTPUT_REVOLUTION / 2; //
  small_stepper.setSpeed(700);
  small_stepper.step(Steps2Take);
  delay(2000);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://guides.mccaskeyrobotics.org/arduino/controlling-stepper-motors.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
