5.
Interfacing Stepper Motor with the 8051 Microcontroller
A stepper motor is a widely used device that translates electrical pulses into mechanical
movement. Every stepper motor has a permanent magnet rotor (also known as shaft)
surrounded by a stator as shown in Figure 1.
Figure 1: Rotor Alignment
The most common stepper motors have four windings that are paired with a center-tapped
common as shown in Figure 2.
Figure 2: Stator Windings Configuration
This type of stepper motor is commonly referred to as a four-phase stepper motor. The center
tap allows a change of current direction in each of two coils when a winding is grounded,
thereby resulting in the polarity change of the stator. The stepper motor shaft moves in a
fixed repeatable increment. The direction of rotation is determined by the stator poles. As
the direction of current through the stator changes, the polarity also changes causing
the reverse motion of the rotor.
Normal 4-step sequence for clockwise movement
P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0 VALUE
0 0 0 0 1 0 0 1 0X09
0 0 0 0 0 0 1 1 0X03
0 0 0 0 0 1 1 0 0X06
0 0 0 0 1 1 0 0 0X0C
Normal 4-step sequence for anticlockwise movement
P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0 VALUE
0 0 0 0 1 1 0 0 0X0C
0 0 0 0 0 1 1 0 0X06
0 0 0 0 0 0 1 1 0X03
0 0 0 0 1 0 0 1 0X09
The stepper motor has 200 poles and is driven by ULN2003A driver IC which requires
a supply voltage of 5V
Number of poles = 200
Degrees per step = 360°/200 = 1.8°/ 𝑠𝑡𝑒𝑝
Number of steps required to rotate X° = X° / 1.8°
1. Example: Rotate stepper motor 𝟗𝟎° in clockwise direction.
Code:
#include <reg51.h>
#define MOTORPORT P2
unsigned char sequence[]={0x09, 0x03, 0x06, 0x0C};
void Delay(unsigned char ms)
{
unsigned char i;
unsigned int j;
for (i = 0; i < ms; i++)
for(j = 0; j < 1275; j++);
}
void RotateClockWise(unsigned int angle)
{
unsigned int steps = angle/1.8f;
unsigned int i;
for(i = 0; i < steps; i++)
{
MOTORPORT = sequence[i%4];
Delay(10);
}
}
void main()
{
RotateClockWise(90);
while(1)
{
}
}
2. Example: Rotate stepper motor 45° in anticlockwise direction
Code:
#include <reg51.h>
#define MOTORPORT P2
unsigned char sequence[]={0x09, 0x03, 0x06, 0x0C};
void Delay(unsigned char ms)
{
unsigned char i;
unsigned int j;
for (i = 0; i < ms; i++)
for(j = 0; j < 1275; j++);
}
void RotateAntiClockWise(unsigned char angle)
{
unsigned int steps = angle/1.8f;
unsigned int i;
for(i = 0; i < steps; i++)
{
MOTORPORT = sequence[3 - i%4];
Delay(10);
}
}
void main()
{
RotateAntiClockWise(45);
while(1)
{
}
}
3. Example: Rotate stepper motor 180° clockwise and anticlockwise
direction continuously
Code:
#include <reg51.h>
#define MOTORPORT P2
unsigned char sequence[]={0x09, 0x03, 0x06, 0x0C};
void Delay(unsigned char ms)
{
unsigned char i;
unsigned int j;
for (i = 0; i < ms; i++)
for(j = 0; j < 1275; j++);
}
void RotateClockWise(unsigned int angle)
{
unsigned int steps = angle/1.8f;
unsigned int i;
for(i = 0; i < steps; i++)
{
MOTORPORT = sequence[i%4];
Delay(10);
}
}
void RotateAntiClockWise(unsigned char angle)
{
unsigned int steps = angle/1.8f;
unsigned int i;
for(i = 0; i < steps; i++)
{
MOTORPORT = sequence[3 - i%4];
Delay(10);
}
}
void main()
{
while(1)
{
RotateClockWise(180);
RotateAntiClockWise(90);
}
}
Exercise 1: Rotate stepper motor 6𝟎° in anticlockwise direction and Rotate
stepper motor 5𝟎° in clockwise direction once.