Raspberry Pi‎ > ‎

How to connect Stepper motors to a Raspberry Pi

The page demos how to connect 28BYJ-48 stepper motors to a Raspberry Pi.
These small stepper motors are available on ebay for £3.99 + p+p.
They're not very powerful, but they make for convenient testing
with a Raspberry Pi, as they will run straight off the Raspberry Pi.

These steppers come with a ULN2003A Driver board, the ULN2003
contains 7 darlington transistors. These boost the current capabilities
of the RPI so that it can supply enough power for the stepper motor.


A stepper motor can be controlled to move in very precise "steps", each
step turns the shaft between 0.36 and 5.6 degrees, depending on the
make of the motor.Because of it precision, the stepper motor is perfect
for controlling of machines.

Please be very careful when hooking this up, as any mistake in
the wiring could permanently damage your Raspberry Pi!


Step by Step:

 
The following Steps reference the pin numbers on the Raspberry Pi's P1 Header. As you can see from the diagram to the right P1 PIN1 is in the top left, to it's right P1 PIN2. The pins continue in order to the bottom right of the connector P1 PIN 26.

As well as pin numbers, the pins also have names such as GPIO 0, 2, 10 etc.
 
RPI GPIO Pinout


The driver board

The driver board contains a ULN2003A darlington driver board. This little chip contains 7 darlington transistors,
these transistors allow the small current output from the Raspberry Pi to control a bigger current (the stepper motor)
The diodes in the IC are there to deal with the back emf from the stepper motors coils. When the current is switched off to one of the coils, the magnetic field around the coil collapses rapidly, this creates a high voltage spike across the coil wire. This spike could damage the PI, so the diodes job is to dissipate this voltage safely.

Here is a circuit diagram that shows the driver board and the internal workings of the ULN2003A IC.




Connect "IN1" to P1 PIN 11  (Named GPIO 17)



Connect Stepper to Raspberry PI
Connect "IN2" to P1 PIN 12 (Named GPIO 18)



Connect Stepper To Raspberry Pi
Connect "IN3" to P1 PIN 13 (Named GPIO 21)



Connect Stepper To Raspberry Pi
Connect "IN4" to P1 PIN 15 (Named GPIO 22)



Power Connections


These two connections provide power to the ULN2003A from the Raspberry Pi's GPIO


Connect "  - (NEGATIVE) " to P1 PIN 6 (Named GND)
Connect " + (POSITIVE)" to P1 PIN 2 (Named +5v)

Connect Stepper To Raspberry Pi


Raspberry Pi is a trademark of the Raspberry Pi Foundation


Code :

To drive the stepper, I used the code from. Please remeber to change the code to suite the GPIO pins that you used abover, as the author of the code below used different GPIO pins.

 http://www.raspberrypi-spy.co.uk/2012/07/stepper-motor-control-in-python/