Working with STM32 and Motors part 1 : Stepper Motor

Buy 28BYJ-48 Stepper Motor with ULN2003 Motor Driver Online

In this guide,, we will learn to interface a stepper motor with STM32F411RE Nucleo-64. There are many options available for stepper motors. But in this tutorial, we will use an inexpensive 28BYJ-48 stepper motor for interfacing with STM32F411. Because it comes with a UL2003 driver. Moreover, it is a low-cost solution to learn about stepper motors control and working. But the concepts learned with this type can be easily applied to other high torque industrial stepper motors also.

In this guide, we will cover the following:

  • 28BYJ-48 Stepper Motor
  • Connection diagram
  • Code
  • Demo

1.1 28BYJ-48 Stepper Motor pin configuration:

The following figure shows the pinout diagram of 28BYJ-48 stepper motor. It consists of 5 pins. Out of these 5 pins, four pins are used to provide sequence logic to the coils and one pin is +5 volts supply pin. 

28BYJ-48 stepper motor pinout diagram

1.2 Pin Configuration Details

1Coil 1OrangeThese are coils used to control the step sequence of the stepper motor. One end of each coil is connected with +5V and the other end will be connected with ULN2003 driver output. 
2Coil 2Pink
35VRedUsed to apply +5 volt supply to the stepper motor. This voltage appears across the coils when a specific coil is ground through a control sequence. 
4Coil 3Yellow
5Coil 4Blue

The specifications of the S8BYJ-48 stepper motor are:

  • It is a unipolar 5 pin coil with a rated DC voltage of 5V.
  • It has 4 phases with a stride angle of 5.625°/64.
  • The frequency of this stepper motor is 100Hz and insulated power is 600VAC/1mA/1s.
  • The half-step method is recommended for driving this stepper motor.
  • The value of pull in torque for a stepper motor is 300 gf.cm.

1.3 ULN2003 Stepper Motor Driver Module

Now the first question which comes to your mind is why do we need a ULN2003 driver to drive stepper motors? This is because the current consumption of 28BYJ-48 is around 240mA. That means the current required to drive coils by applying a sequence of control signals is also almost 200mA. GPIO pins of STM32F411 can not provide current of this magnitude. Therefore, we need a ULN2003 driver which translates low current output of TM4C123 GPIO pins into higher current that meets the requirement of stepper motor control signals. 

Stepper Motor Driver Module Pinout

ULN2003 driver IC consists of 7 darlington pair transistor outputs. Each output can drive 500mA and 50V load. The input to each 7 darlington pair transistor can be a signal from the microcontroller such as STM32F411 microcontroller. To drive a stepper motor, this driver board used only four outputs. The following diagram shows the ULN2003 motor driver board and details of their components: 

ESP32 Stepper Motor (28BYJ-48 and ULN2003 Driver) | Random Nerd Tutorials

2.1 Connection:

So far, we learnt about the stepper motor and it’s driver. Let’s continue with the actual connection to STM32F411 and how to code





2.2 Driving Modes

Full Drive Sequence Mode

In this mode, two coils are energized at a time that means two windings of stepper motor energized together. Therefore, motor runs at full torque. We apply this sequence to input pins of the stepper motor through a UNL2003 driver. 


Half Drive Sequence Mode

In this mode, we control the phases or coils with alternate one and two phase control as shown in the table below. But, in this mode, motor runs at low torque. 


Wave Drive Mode 

Like full-drive mode, only one coil is turned on at a time but with a drive sequence as shown below. The only advantage of this method is that a stepper motor consumes less power. 


We shall use Full drive sequence mode

3. Code:

#include "stm32f4xx.h"                  // Device header
//delay in millisecond
void Delay_ms(int delay)


int i;

for(; delay>0 ;delay--)


for(i =0; i<3195;i++);



int main(void)
	//Enable clock access to GPIOC
	//Set PC0, PC1, PC2 and PC3 to output
		for(int i=0; i<500; i++) // take 500 steps to complete one revolution 
		//apply full drive clockwise rotation sequence 
	  GPIOC->ODR = 0x08;
    GPIOC->ODR = 0x04;
    GPIOC->ODR = 0x02;
    GPIOC->ODR = 0x01;
for(int i=0; i<100; i++) // rotate 100 steps counter clockwise 
	  GPIOC->ODR = 0x01;
    GPIOC->ODR = 0x02;
    GPIOC->ODR = 0x04;
    GPIOC->ODR = 0x08;


4. Demo

Note: My stepper motor sometimes gets stuck for some reason.

Happy Coding 😀


  • chris Posted October 14, 2021 8:52 am

    try adding an external 5volt to the stepper driver , might solve your getting stuck problem..

    • Husamuldeen Posted October 18, 2021 3:40 am

      I figured out the issue, it is so old (7 years old) and left in dust for almost 5 years.
      I tried with brand new motor, no issue.
      No need to external power since the motor is not loaded

  • Rafal Posted October 15, 2021 10:25 am

    well done!

  • Shahid Posted October 18, 2021 2:23 am

    This experiment do with stm32H7 series.tell me sir

    • Husamuldeen Posted October 18, 2021 3:48 am

      I don’t have H7. However, I took a look at the register set and it is easy easy to port my code from F4 to H7
      follow the same steps and you should be ok

Add Comment

Your email address will not be published. Required fields are marked *