# Adafruit's Raspberry Pi Lesson 10. Stepper Motors

## Overview

Stepper motors fall somewhere in between a regular DC motor ([Lesson 9](http://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-dc-motor)) and a servo motor ([Lesson 8](http://learn.adafruit.com/adafruits-raspberry-pi-lesson-8-using-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.

![](https://cdn-learn.adafruit.com/assets/assets/000/003/688/medium800/learn_raspberry_pi_photo_uln.jpg?1396800524)

In this lesson you will learn how to control a stepper motor using your Raspberry Pi and the same L293D motor control chip that you used with the DC motor in [Lesson 9](http://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-dc-motor).

The Lesson will also show you how to use an alternative driver chip, the ULN2803

For this project, it does not really matter if you use a L293D or a ULN2803. The lower cost of the ULN2803 and the four spare outputs, that you could use for something else, probably make it the best choice if you don't have either chip.

The motor is quite low power and suffers less from the surges in current than DC motors and servos (which use DC motors). This project will therefore work okay powered from the 5V line of the Raspberry Pi, as long as the Pi is powered from a good supply of at least 1A.

# Adafruit's Raspberry Pi Lesson 10. Stepper Motors

## Parts

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

# Adafruit's Raspberry Pi Lesson 10. Stepper Motors

## Hardware (L293D)

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.

This tutorial works with all versions of Raspberry Pi (rev 1, 2, A, B, B+ and Zero) except the compute module which has no headers.&nbsp;

Danger: 

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

## 40-Pin (A, B, B+ and Zero) Cobbler Plus Schematic
![](https://cdn-learn.adafruit.com/assets/assets/000/073/661/medium800/learn_raspberry_pi_Raspberry_Pi_Stepper_Motors-T-Cobbler-Plus-L293D.png?1553644678)

## 20-Pin (Rev 1 and Rev2) Cobbler Schematic
![](https://cdn-learn.adafruit.com/assets/assets/000/003/694/medium800/learn_raspberry_pi_breadboard_293.png?1396800609)

# Adafruit's Raspberry Pi Lesson 10. Stepper Motors

## Hardware (ULN2803)

If you are using a ULN2803, then all five of the stepper motor leads are used.

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.

This tutorial works with all versions of Raspberry Pi (rev 1, 2, A, B, B+ and Zero) except the compute module which has no headers.&nbsp;

Danger: 

Although the code below mentions pin 18 of the GPIO connector being used as an Enable pin, this is only required when using the L293D.

## 40-Pin (A, B, B+ and Zero) Cobbler Plus Schematic
![](https://cdn-learn.adafruit.com/assets/assets/000/073/662/medium800/learn_raspberry_pi_Raspberry_Pi_Stepper_Motors-Cobbler-Plus-ULN2803A.png?1553645046)

## 20-Pin (Rev 1 and Rev2) Cobbler Schematic
![](https://cdn-learn.adafruit.com/assets/assets/000/003/695/medium800/learn_raspberry_pi_breadboard_ULN.png?1396800640)

# Adafruit's Raspberry Pi Lesson 10. Stepper Motors

## Stepper Motors

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

![](https://cdn-learn.adafruit.com/assets/assets/000/003/696/medium800/learn_raspberry_pi_steppers.png?1396800693)

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 8 steps, but then the motor also incorporates a reduction gearbox of 1:64 that means that it needs 8 x 64 = 512 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.

# Adafruit's Raspberry Pi Lesson 10. Stepper Motors

## ULN2803

We looked at the L293D in Lesson 9. The ULN2803 is a very useful chip.

![](https://cdn-learn.adafruit.com/assets/assets/000/003/697/medium800/learn_raspberry_pi_uln2803.png?1396800715)

Whereas the L293D effectively has four outputs whose polarity can be reversed, the ULN2803 has eight outputs that amplify the weak signals from the raspberry Pi GPIO pins allowing them to switch much higher currents.

However, unlike the L293D an output from the ULN2803 can only sink current, so the common positive red lead of the stepper motor is used. So, rather than use the whole of the coil between say the Pink and Orange leads, just the half of the coil between the common Red connection and the Pink connection is energized.

![](https://cdn-learn.adafruit.com/assets/assets/000/003/698/medium800/learn_raspberry_pi_steppers_ULN.png?1396800764)

# Adafruit's Raspberry Pi Lesson 10. Stepper Motors

## Software

## The Setup
We are&nbsp;using the CircuitPython Libraries that are part of adafruit-blinka. See&nbsp;[CircuitPython Libraries on Raspberry Pi&nbsp;](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi)to get a fresh Raspberry Pi setup.

If you have a running Raspberry Pi with an up to date copy of Raspbian you can simply run the following command to install adafruit-blinka.&nbsp;

The software is exactly the same on all 40-pin and 20-pin Raspberry Pi models. You can use the L293D or ULN2803 chips without any modification the following code:

```auto
$ sudo pip3 install adafruit-blinka
```

## The Code
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Raspberry_Pi_Stepper_Motors/Raspberry_Pi_Stepper_Motors.py

When the steppers are not moving, they are still 'activated' and hold their position. This draws power. If you don't need the steppers to 'hold' their position, you can call **setStep(0,0,0,0)** to release the coils. The motor will spin freely and wont draw a lot of current.

# Adafruit's Raspberry Pi Lesson 10. Stepper Motors

## Configure and Test

## Get the Code
Let's put this file right in your home directory for simplicity. The wget command makes things easy as this can be run from an internet connected Raspberry Pi.

```auto
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Raspberry_Pi_Stepper_Motors/Raspberry_Pi_Stepper_Motors.py
```

## Running the Code
Confirm that the Pi connected to the Cobbler (or Cobbler Plus) and run the script.

The following command will start the program:

```auto
$ sudo python3 ./Raspberry_Pi_Stepper_Motors.py
```

Enter a delay (5 is a good value) and then a number of steps (512 is a full rotation).

Experiment reducing the delay to find the maximum speed of the motor.

![](https://cdn-learn.adafruit.com/assets/assets/000/073/751/medium800/learn_raspberry_pi_stepper.png?1553718543)


## 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...

Out of 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)
### ULN2803: 8 Channel Darlington Driver (Solenoid/Unipolar Stepper)

[ULN2803: 8 Channel Darlington Driver (Solenoid/Unipolar Stepper)](https://www.adafruit.com/product/970)
Bring in some muscle to your output pins with 8 mighty Darlingtons! This DIP chip contains 8 drivers that can sink 500mA from a 50V supply and has kickback diodes included inside for driving coils. This will let your little microcontroller or microcomputer power solenoids, DC motors (in one...

In Stock
[Buy Now](https://www.adafruit.com/product/970)
[Related Guides to the Product](https://learn.adafruit.com/products/970/guides)
### Assembled Pi Cobbler Plus - Breakout Cable

[Assembled Pi Cobbler Plus - Breakout Cable](https://www.adafruit.com/product/2029)
The Raspberry Pi B+ / Pi 2 / Pi 3 / Pi 4 / Pi 5 has landed on the Maker World like a 40-GPIO pinned, quad-USB ported, credit card sized bomb of DIY joy. And while you can use most of our great Model B accessories by hooking up our [downgrade...](https://www.adafruit.com/product/1986)

Out of Stock
[Buy Now](https://www.adafruit.com/product/2029)
[Related Guides to the Product](https://learn.adafruit.com/products/2029/guides)
### Assembled Pi T-Cobbler Plus - GPIO Breakout

[Assembled Pi T-Cobbler Plus - GPIO Breakout](https://www.adafruit.com/product/2028)
 **This is the assembled version of the Pi T-Cobbler Plus. &nbsp;It only works with the Raspberry Pi Model Zero, A+, B+, Pi 2, Pi 3, Pi 4, and Pi 5!** (Any Pi with 2x20 connector)  
  
The Raspberry Pi has landed on the Maker World like a 40-GPIO pinned, quad-USB ported,...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2028)
[Related Guides to the Product](https://learn.adafruit.com/products/2028/guides)
### Budget Pack for Raspberry Pi 1 Model B (Doesn't include RasPi)

[Budget Pack for Raspberry Pi 1 Model B (Doesn't include RasPi)](https://www.adafruit.com/product/965)
An optimized collection of parts and pieces to experiment with Raspberry Pi at home, school or work. Great for students and those that want to get their feet wet, no soldering required! **THIS PACK DOES NOT INCLUDE A RASPBERRY PI 1 MODEL B and is NOT compatible with Model B+ or Raspberry Pi...**

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/965)
[Related Guides to the Product](https://learn.adafruit.com/products/965/guides)
### Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi

[Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi](https://www.adafruit.com/product/914)
Now that you've finally got your hands on a [Raspberry Pi® Model B](http://www.raspberrypi.org/), you're probably itching to make some fun embedded computer projects with it. What you need is an add on prototyping Pi Cobbler from Adafruit, which can break out all those...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/914)
[Related Guides to the Product](https://learn.adafruit.com/products/914/guides)
### Breadboarding wire bundle

[Breadboarding wire bundle](https://www.adafruit.com/product/153)
75 flexible stranded core wires with stiff ends molded on in red, orange, yellow, green, blue, brown, black and white. These are a major improvement over the "box of bent wires" that are sometimes sold with breadboards, and faster than stripping your own solid core wires. Makes...

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

## Related Guides

- [Raspberry Pi Analog to Digital Converters](https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters.md)
- [Adafruit's Raspberry Pi Lesson 11. DS18B20 Temperature Sensing](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing.md)
- [Adafruit's Raspberry Pi Lesson 4. GPIO Setup](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup.md)
- [Speech Synthesis on the Raspberry Pi](https://learn.adafruit.com/speech-synthesis-on-the-raspberry-pi.md)
- [Adafruit AMG8833 8x8 Thermal Camera Sensor](https://learn.adafruit.com/adafruit-amg8833-8x8-thermal-camera-sensor.md)
- [Program an AVR or Arduino Using Raspberry Pi GPIO](https://learn.adafruit.com/program-an-avr-or-arduino-using-raspberry-pi-gpio-pins.md)
- [Adafruit's Raspberry Pi Lesson 12. Sensing Movement](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-12-sensing-movement.md)
- [Windows IoT Core Application Development: Headed Blinky](https://learn.adafruit.com/windows-iot-application-development-headed-blinky.md)
- [OpenClaw on Raspberry Pi](https://learn.adafruit.com/openclaw-on-raspberry-pi.md)
- [Playing sounds and using buttons with Raspberry Pi](https://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi.md)
- [Adding Basic Audio Ouput to Raspberry Pi Zero](https://learn.adafruit.com/adding-basic-audio-ouput-to-raspberry-pi-zero.md)
- [Drive a 16x2 LCD with the Raspberry Pi](https://learn.adafruit.com/drive-a-16x2-lcd-directly-with-a-raspberry-pi.md)
- [Analog Inputs for Raspberry Pi Using the MCP3008](https://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi.md)
- [Moonshine Voice Control on Raspberry Pi](https://learn.adafruit.com/moonshine-voice-control-on-raspberry-pi.md)
- [Capacitive Touch Sensors on the Raspberry Pi](https://learn.adafruit.com/capacitive-touch-sensors-on-the-raspberry-pi.md)
