Raspberry Pi Stepper Motor Control with L293D / ULN2003A

14. November 2015

Stepper motors are ideal for applications where it is necessary to know the angle of rotation, for example in robotics. Unlike a normal motor, stepper motors can control the steps individually and can therefore detect the exact position.
For easy controlling such a motor, a driver IC like the L293D or the ULN2003 are needed. The use of both drivers is shown in this tutorial.


The more common of those two is the ULN2003, this is also often included on driver boards with the 28BYJ48th There is a voltage of 5V and 12V possible.

The L293D however opens the possibility to connect an external power source, whereby for example a higher amperage (which in some motors is necessary) can be connected.



  • Stepper motor, e.g. the 28BYJ-48 with ULN2003 driver board (US / UK)
  • L293D Driver IC (US / UK)
  • alternatively, if no L293D is wanted: ULN2003 Darlington Array IC (US / UK)
  • Jumper wire (US / UK)
  • maybe an external power source, for example batteries

Of course, you can use any other stepper motor, but it is important to identify the possibly different wire colors when you connect them (see data sheet).

Those colors reflect in my case to the 28BYJ-48 (data sheet):

If you so choose a different motor, consider the case that the colors may be different. Important are the positions and steps.


Wiring L293D

If you have a motor with ULN2003 driver board or the ULN2003 driver IC, you can skip this step.

The data sheet shows that an external power source is possible. The structure is thus as follows:

It is important to say that if you use an external power source, the + 5V from Raspberry Pi must also be connected to the bottom left pin of the IC (pin 8), in addition to Pin 16.

If the motor amperage required more than the Raspberry can deliver, you must have an external power source (a fixed current source is better than batteries). The pre-resistor is also specified in the data sheet. Pay attention that you have enough voltage and amperage, because that is the most common cause, why the engine later is not working as it should. With the 28BYJ-48 however, this is not the case.


Wiring ULN2003

Unlike the L293D, here are only 5V or 12V input voltage allowed (data sheet). Since the Raspberry Pi only has 5V supplies, we can also only use stepper motors that require a maximum of 5V.




When using the 28BYJ48, the sequence is as follows. If you are using a different motor, you have to adjust the sequence corresponding to your data sheet.
So we create a script:

sudo nano stepper.py

and add the following script:

Then just save with CTRL+O and CTRL+X and quit. You can start it with:

sudo python stepper.py

With setStep(0,0,0,0) you can set the stepper to idle, because otherwise power is consumed all the time (by the electromagnets in the motor).

Regard that if you choose a high delay value (per step), the motor is moving very slowly (e.g. with a 100ms delay one full turn would already take 51.2 seconds, because the engine has a total of 512 steps. Too small delay values may, however, have the opposite effect, so that the signals are transmitted too quickly and causing the motor to just “stuttering”. If you want a faster engine, choose a model with a larger step angle).

Leave a Comment

Your email address will not be published.