View Course Path

Stepper Motor Interfacing with 8051 – Simple tutorial

Let’s apply the theory that we have learned so fat in this 8051 course by interfacing a stepper motor with the 8051. Let’s talk in detail about the stepper motor and then discuss the actual interfacing with circuit diagrams and the code to control it all.

Components required for interfacing a Stepper motor with 8051 Microcontroller

Sr no. Components Required Description Nos.
1  8051 Microcontroller AT89S51/ AT89C51/Any other compatible variants 1
2 Stepper Motor 12V 1
3 Current Driver IC ULN2003A 1
4 DC Power Supply 12V 1
5 Oscillator Crystal 12Mhz 1
6 Capacitor (1) 22pF 2
7 Capacitor (2) 10uF 1
8 Resistor/ Pot 10kohm 1
9 Connecting wires As per the requirement

What is a stepper motor?

  • A motor, in general, is a device that converts electrical energy into mechanical energy.
  • As the name suggests, a stepper motor is a device that does the same task as above but, in steps.
  • It is a brushless, synchronous electric motor that can divide a complete rotation into a number of steps. Stepper motors generally have a permanent magnet shaft (rotor), and it is surrounded by a stator.
  • The best feature of this type of motor is that the motor’s angular position can be accurately controlled without any feedback mechanism, as long as the motor is not oversized. (From a designer’s point of view the sizing of the motor is essential, if the stepper motor is undersized then it will not be able to withstand load and if it is oversized then it becomes too expensive for its purpose.).
  • Therefore, it works in a simple accurate open-loop system, where the output is directly dependent on the input.
  • A stepper motor rotates at small angles to complete 360 degrees rotation, these small angles are called steps, hence the name Stepper Motor. Typically, a stepper motor consists of 200 steps.

200 Steps = 360 degrees
1 Step = ‘x’ degrees
x = 360 / 200 = 1.8 degree

  • Therefore, every step is 1.8 degrees.
  • The high and low pulses are provided to the stator coil of the stepper motor.
  • Here we’re using a 4-coil stator. The angle of each step is decided by the steps in the rotor and its alignment with the stator.
  • In the case of a 200-steps motor, the step angle is 360 / 200 = 1.8 degrees, and in the case of an 8-steps rotor, the step angle is 360 / 8 = 45 degrees. Here’s a simulation.

200 Steps Rotor: Step angle = 1.8 degrees

Stepper_simulation_1.8degree step angle

8 Steps Rotor: Step angle = 45 degrees

Stepper_simulation_45degree step angle

Important terminology related to a stepper motor

  1. Revolutions Per Minute (RPM): This term is often used when the number of rotations is to be found per minute. It determines the frequency with which the motor is rotating.
  2. Step Angle: Since a stepper motor rotates one step at a time, the angle it sweeps in one step is called the Step Angle. For a rotor having 200 steps/teeth, it is equal to 1.8 degrees and for a motor having 4 steps, it is equal to 90 degrees.
  3. Steps per revolution: This parameter decides the number of steps required to complete one revolution and is determined by 360 / (Step Angle).
  4. Steps per second: As suggested by the name it determines the steps covered in one second and is given by

Steps per second = RPM x steps per revolution / 60

Type of Stepper Motor

Stepper motors are broadly divided into two types

  1. Unipolar stepper motor
  2. Bipolar stepper motor

Unipolar stepper motor

The unipolar stepper motor has five or six wires out of which four wires are joined to one of the ends of each of the four stator coils. The connections at the center of the coils are joined together and are connected to the 12V supply. They are called unipolar steppers because power always comes in on this one pole.

Unipolar Stepper Motor

Bipolar stepper motor

The bipolar stepper motor usually has four wires coming out of it. Unlike unipolar steppers, bipolar steppers have no common center connection. They have two independent sets of coils instead.

Bipolar Stepper Motor

We’re going to use the unipolar stepper motor in our further discussions since it is easier to operate, widely available, and usually the cheapest means to get precise angular moments.

What are the components of a stepper motor

Components of Stepper

  1. Stator: The stator is made up of four coils, that are energized by the pulses from a microcontroller or a stepper controller.
  2. Rotor: The number of steps of the rotor and its alignment with the stator determines the step angle and steps per revolution.
  3. Permanent magnets: The rotor is mounted on a permanent magnet that attracts or repels the stator coils and hence the propulsion occurs.

Working of a stepper motor

A stepper motor works on the principle of magnetic attraction and repulsion. The coils of stators are placed on electromagnets which are energized and de-energized by the pulses of the microcontroller which creates magnetic north and south on the stator poles. The rotor is mounted on a permanent magnet having a permanent N and S marked.

Working of stepper motor

The sequence in which the coils are excited to form the poles causes the rotor to attract one pair of stator pole and repel the other causing motion in the shaft and the load connected to it. The sequence on which the coils are energized is discussed in detail in the next section of this article.

Gif describing the working of stepper motor

 

Interfacing stepper motor to 8051 Microcontroller

We are using Port 2 of 8051 microcontroller to generate high and low pulses and using a current amplifier IC i.e. ULN2003a to amplify the current to drive the stepper motor using the pulse of the microcontroller. On the basis of the way the coils are energized, a Unipolar Stepper motor can be classified into three categories:

  1. Wave Drive Mode
  2. Full Drive Mode
  3. Half Drive Mode

Wave drive mode

In this mode only one coil is energized at a time, all the four coils are energized one after the other in a sequence. In terms of power consumption, this mode is a power saver, but the torque produced is less compared to the full drive mode. In the following table, A-B-C-D refers to the stator coils, that are to be energized sequentially in the manner and ‘1’s and ‘0’s refers to ‘HIGH‘ and ‘LOW’ states.

Steps A B C D HEX
1 1 0 0 0 0x08
2 0 1 0 0 0x04
3 0 0 1 0 0x02
4 0 0 0 1 0x01

Full Drive mode

In this mode two coils are energized at a time, i.e. Logic 1 is given to 2 coils at the same time, this results in higher torque, but the power consumption also increases and precision of the stepper motor increases two folds.

Steps A B C D HEX
1 1 1 0 0 0x0C
2 0 1 1 0 0x06
3 0 0 1 1 0x03
4 1 0 0 1 0x09

Half Drive Mode

This mode works on the alternate energizing principle, i.e. at one moment only 1 coil is energized, but in the very next moment 2 coils are energized, then again back to 1. This sequence is repeated so as to make the motor more power-efficient while maintaining the high torque and increase the angular rotation of the motor.

Steps A B C D HEX
1 1 0 0 0 0x08
2 1 1 0 0 0x0C
3 0 1 0 0 0x04
4 0 1 1 0 0x06
5 0 0 1 0 0x02
6 0 0 1 1 0x03
7 0 0 0 1 0x01
8 1 0 0 1 0x09

Why are we using ULN2003A driver?

A Stepper motor consumes a current of 0.1 – 1 A during step rotation with the load. An AT89c51 produces a maximum current of 0.045A through the ports. Therefore, the pulses sent from Port 2 are not sufficient to run a stepper motor. Hence, we cannot directly interface stepper motors with microcontrollers like AT89C51 microcontroller.

There are two solutions to this problem:

  1. To use a motor driver like L239D
  2. Or, to use a current amplification IC like ULN2003A, here we choose the latter.

To overcome the stepper motor’s power constraint, we need an external power source that can drive heavy loads and supply enough power for the operation of the stepper motor. ULN2003 is an IC containing pairs of Darlington configuration of transistors. ULN2003 takes low power input and outputs a signal with greater power.

ULN2003a

The Darlington transistor is made up of two PNP or NPN BJTs by connecting together where the emitter of the PNP transistor is connected to the base of the other PNP transistor to create a sensitive transistor with high current gain used in many applications where switching or amplification is crucial.

Darlington transistor

Step by step connections

  • Step 1: If you’re using Proteus or and other simulation software or even hardware, select the AT89C51 or AT89S51 microcontroller or any other compatible variant. (The AT89C51 is an 8-bit microcontroller from the Atmel family which works with the 8051 architecture.)
  • Step 2: Connect a 12 MHz oscillator between pin 18 and 19.
  • Step 3: Connect two capacitors of 22pF, with one terminal on either side of the oscillator and the other terminal to ground, as shown below.
  • Step 4: Set Pin 31, i.e., EA pin to HIGH by connecting it to the +5V DC source.
  • Step 5: Now, to make the RESET circuit, connect Pin 9 (RST) to +5V through a capacitor of 10µF and connect the same pin to +0V (GND) through a 10kΩ resistor or a potentiometer.
  • Step 6: Connect the 4 pins of Port 2 to the input side of ULN2003a IC as follows,
    • P2.0 to 1B
    • P2.1 to 2B
    • P2.2 to 3B
    • P2.3 to 4B
  • Step 7: Connect the outputs of the ULN2003a to the 4 wires of the stepper motor as shown in the circuit diagram below. Note: Please note that while connecting the pins 1C, 2C, 3C, and 4C to the wires of the stepper, try all the possible connections. If the motor starts oscillating instead of rotating, then carefully look for the 4 coil wires and 2 common wires.
  • Step 8: Connect the two common wires of the stepper motor and Pin 9 of the IC to a 12V DC supply.

That’s it for the connections part, now you can move onto the code specific portion to interface it to 8051 Microcontrollers.

Circuit diagram to interface stepper motor to 8051

Circuit to interface stepper to 8051

Assembly language program to interface stepper motor with 8051

// Wave Drive Mode
ORG 00H
MOV TMOD, #01H
MAIN:
MOV P2, #08H
ACALL DELAY
MOV P2, #04H
ACALL DELAY      
MOV P2, #02H
ACALL DELAY
MOV P2, #01H
ACALL DELAY
SJMP MAIN 

// To generate a delay of 200 *1 ms
DELAY:MOV R0,#200 //change this value to required delay in ms       
BACK: MOV TH0,#0FCH  
      MOV TL0,#018H   
      SETB TR0             
wait: JNB TF0,wait       
      CLR TR0              
      CLR TF0             
      DJNZ R0,BACK
      RET
END
	   
// Full Drive Mode
ORG 00H
MOV TMOD,#01H
MAIN:
MOV P2, #0CH
ACALL DELAY
MOV P2, #06H
ACALL DELAY      
MOV P2, #03H
ACALL DELAY
MOV P2, #09H
ACALL DELAY
SJMP MAIN 

// To generate a delay of 200 * 1 ms = 200ms
DELAY:MOV R0,#200 //change this value to required delay in ms       
BACK: MOV TH0,#0FCH  
      MOV TL0,#018H   
      SETB TR0             
wait: JNB TF0,wait       
      CLR TR0              
      CLR TF0             
      DJNZ R0,BACK
      RET
END
   
// Half Drive Mode
ORG 00H
MOV TMOD,#01H
MAIN:
MOV P2, #08H
ACALL DELAY
MOV P2, #0CH
ACALL DELAY      
MOV P2, #04H
ACALL DELAY
MOV P2, #06H
ACALL DELAY
MOV P2, #02H
ACALL DELAY
MOV P2, #03H
ACALL DELAY
MOV P2, #01H
ACALL DELAY
MOV P2, #09H
ACALL DELAY
SJMP MAIN 

// To generate a delay of 200 *1ms
DELAY:MOV R0, #200 //change this value to required delay in ms       
BACK: MOV TH0, #0FCH  
      MOV TL0, #018H   
      SETB TR0             
wait: JNB TF0, wait       
      CLR TR0              
      CLR TF0             
      DJNZ R0, BACK
      RET
END

C language program to interface stepper motor with 8051

// Wave drive Mode
#include<reg51.h>
void ms_delay(unsigned int t) //To create a delay of 200 ms = 200 x 1ms
    {
    unsigned i,j ;
    for(i=0;i<t;i++)       //200 times 1 ms delay
    for(j=0;j<1275;j++);   //1ms delay
    }
void main()
{
    while(1) // To repeat infinitely
    {
        P2=0x08;           //P2 = 0000 1000 First Step
        ms_delay(200);
        P2=0x04;           //P2 = 0000 0100 Second Step
        ms_delay(200);
        P2=0x02;           //P2 = 0000 0010 Third Step
        ms_delay(200);
        P2=0x01;           //P2 = 0000 0001 Fourth Step
        ms_delay(200);
    }
}
// Full drive Mode
#include<reg51.h>
void ms_delay(unsigned int t) //To create a delay of 200 ms = 200 x 1ms
    {
    unsigned i,j ;
    for(i=0;i<t;i++)       //200 times 1 ms delay
    for(j=0;j<1275;j++);   //1ms delay
    }
void main()
{
    while(1) // To repeat infinitely
    {
        P2=0x0C;           //P2 = 0000 1000 First Step
        ms_delay(200);
        P2=0x06;           //P2 = 0000 0100 Second Step
        ms_delay(200);
        P2=0x03;           //P2 = 0000 0010 Third Step
        ms_delay(200);
        P2=0x09;           //P2 = 0000 0001 Fourth Step
        ms_delay(200);
    }
}

// Half Drive Mode
#include<reg51.h>
void ms_delay(unsigned int t)  //To create a delay of 200 ms = 200 x 1ms
    {
    unsigned i,j ;
    for(i=0;i<t;i++)    
    for(j=0;j<1275;j++);
    }
void main()
{
    while (1) 
    {
        P2 = 0x08;         //P2 = 0000 1000 First Step
        ms_delay(200)
        P2 = 0x0C;         //P2 = 0000 1100 Second Step
        ms_delay(200)
        P2 = 0x04;         //P2 = 0000 0100 Third Step
        ms_delay(200)
        P2 = 0x06;         //P2 = 0000 0110 Fourth Step
        ms_delay(200)
        P2 = 0x02;         //P2 = 0000 0010 Fifth Step
        ms_delay(200); 
        P2 = 0x03;         //P2 = 0000 0011 Sixth Step
        ms_delay(200); 
        P2 = 0x01;         //P2 = 0000 0001 Seventh Step
        ms_delay(200); 
        P2 = 0x09;         //P2 = 0000 1001 Eight Step
        ms_delay(200); 
    }
}

Applications of stepper motors

  • Used in Dot Matrix Printer.
  • Used in tape drives, floppy disc drives, printers, and electric watches.
  • Metal cutting & Metal forming machines.
  • Used in textile industries.
  • Used in integrated circuits fabrications.