STM32F103 SPL Tutorial 8 – Interfacing Unipolar Stepper Motor

Stepper Motor

Stepper motor is an electromechanical device that converts electrical pulses into discrete mechanical movements. In this tutorial, I will explain how to control a unipolar stepper motor using STM32F103 microcontroller. If you don’t know the basic of the stepper motor, I suggest you to read this post. To interface a stepper motor from a microcontroller, we can’t directly drive it with GPIO pins because GPIO pins have maximum current that can sink or source from it. To overcome this problem, we can use driver circuit. The driver circuit for unipolar stepper motor can be built by using 4 transistors to drive large current to the 4 wires of a stepper motor. It also can be built with ULN2003 IC. This is the circuit for driving a unipolar stepper motor from STM32F103 by using ULN2003 IC.

Stepper driver

In this tutorial, I will use the 28BYJ-48 stepper motor. This motor is very cheap and it also comes with driver module based on ULN2003 IC. This motor runs with 5V supply and has gear inside. The gear reduction ratio is approximately 64:1. If you search from the internet, other people say that the gear reduction ratio is actually 63.68395:1.

28byj-48

There are 2 common modes that can be used for controlling stepper motor, full step and half step. Actually there is 1 more mode that can be used for controlling stepper motor in more advanced ways. The mode is called micro step, but in this tutorial, I will explain only the full step and half step.

Full Step

The stepper motor is controlled by giving a sequence of electrical pulses. That electrical pulses are applied to the 4 wires of stepper motor. Each electrical pulse is consist of 4-bits. We can applied these pulses to the 4 wires by using 4 GPIO pins. The full step mode has a sequence of electrical pulses that consist of 4 different pulses. Every 1 sequence is equivalent to 4 steps of movement, so 1 pulse is equivalent to 1 step movement. The following table is the sequence of full step mode.

Full step

One step movement of this motor in full step mode is 11.25°. By applying these sequence once, the internal shaft of stepper motor will move 4 x 11.25° = 45°. To rotate the internal shaft of stepper motor 1 revolution, we need 360° / 45° = 8 sequences (32 steps). The actual shaft of this motor is geared with 64:1 gear ratio. That means every actual shaft rotates 1 revolution, the internal shaft must rotate 64 revolutions, so to rotate the actual shaft 1 revolution, we need 32 x 64 = 2048 steps. To move stepper motor 2048 steps, we can applied the sequence 512 times (because 1 sequence is 4 step). The following code is for rotating the actual shaft of the stepper motor 1 clockwise revolution.

Between each pulse in a sequence, we need to add delay, because the stepper motor works much slower compared to the execution time of a microcontroller to execute 1 line of code. This delay allows stepper to move 1 step before we apply next pulse. This delay is directly related to rotation speed of the shaft. The smaller delay value will give faster rotation speed. We can reverse the direction of the shaft rotation by reversing the applied sequence.

This is the full code for rotating stepper motor one revolution clockwise and one revolution counter-clockwise. You can get the project files from here.

Half Step

Half step mode consists of 8 electrical pulse for every 1 sequence. The sequence for half step mode is shown in the following table. The different between full step and half step is the step resolution. When you use half step, you will get more smaller degree per step movement. In this motor, if we use half step, you will get 5.625° per 1 step, but the total number of degree per 1 sequence is the same (5.625° x 8 = 45°). The effect is the shaft rotates more smooth.

Half step

This is the code for rotating stepper motor one revolution clockwise and the one revolution counter-clockwise using half step mode. You can get the project files from here.

The result is shown in the following video.

Go to the part 9 of the tutorial.

Share this:

One thought on “STM32F103 SPL Tutorial 8 – Interfacing Unipolar Stepper Motor

Leave a Reply

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