Stepper Motor Interfacing with TM4C123 Tiva Launchpad

In this tutorial, we will learn to interface a stepper motor with TM4C123 Tiva Launchpad. There are many options available for stepper motors. But in this tutorial, we will use an inexpensive 28BYJ-48 stepper motor for interfacing with TM4C123. 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. For programming of TM4C123 Tiva Launchpad, we will use Keil MDK for ARM

28BYJ-48 stepper motor interfacing with TM4C123 Tiva Launchpad hardware demo

Stepper motors Introduction 

As its name suggests, stepper motor rotates in discrete steps of predefined values. These are DC brushless and synchronous motors. Unlike other DC motors, they provide a precise position control according to the number of steps per revolution for which motor is designed. That means a complete one revolution of a stepper motor is divided into a discrete number of steps. You will find their enormous use in CNC machines, Robotics, 2D and 3D printers

As discussed earlier, in this TM4C123 stepper motor tutorial, we will use a 28BYJ-48 stepper motor and control it through ULN2003 motor driver.

28BYJ-48 stepper motor

This is the most commonly used stepper motor in low power industrial and mostly famously in hobbyist projects. 28BYJ-48 is a unipolar 5 coil stepper motor with operating voltage of 5V. That means 28BYJ-48 has four sequences of coils arrangement and these coils can be powered with 5 volts.  Because of 5 volts operating voltage, we can easily drive this motor from any microcontroller such as TM4C123 Tiva Launchpad.

This stepper motor has a stride angle of 5.625 degree. That means 28BYJ-48 will complete one revolution in (360/5.625) 64 steps by taking one step at a time and in one step it covers 5.625 degree distance. But this stepper motor can also be used in full step mode. In full step mode, the angle of each step is 11.25 degrees. That means the motor completes its one revolution in 32 steps ( 360/11.25). 

Therefore, In order to move one step forward or backward, the coils of the motor engerizes with a particular sequence. 

We will discuss sequences required to control steps of the stepper motor in coming sections. 

28BYJ-48 stepper motor Pinout 

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

Pin Configuration Details

PinNameColorFunctionality
1Coil1OrangeThese 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. 
2Coil2Pink
3Coil3Yellow
4Coil4Blue
5VccRedUsed 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. 

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 TM4C123 Tiva Launchpad 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 TM4C123GH6PM 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: 

ULN2003 Stepper Motor driver board

1N1 to IN4: These are input pins used to provide control signals to stepper motor such as control sequences. We will connect these pins with GPIO pins of TM4C123 Tiva Launchpad.

Vcc and GND: Vcc is a power supply pin and it is used to provide 5 volts power to the stepper motor from an external power source. Furthermore, TM4C123 microcontroller does not have an onboard 5 volts signal. Therefore, we will need to provide power from an external power source. Additionally, the 28BYJ-48 stepper motor requires 240mA current to operate and it also consumes power at an idle condition. Therefore, it is recommended not to power the 28BYJ-48 stepper motor directly from any microcontroller instead use an external 5 volts power supply.

Motor Connector Header: This connector is used to connect a stepper motor. It provides output from four Darlington pair transistors. 

Connection Diagram 28BYJ-48 stepper motor and TM4C123G Tiva Launchpad

So far in this tutorial, we learned everything about stepper motor and its driver board. Now make connections with 28BYJ-48,  ULN2003 and TM4C123 Launchpad according to this schematic diagram. 

28BYJ-48 stepper motor interfacing with TM4C123 Tiva Launchpad

Note: It is recommended to power 28BYJ-48 stepper motor with an external 5-volt power source having at least 300mA current output. Furthermore, TM4C123 does not have a 5-volt signal. Therefore, we should use an external power source.

Stepper Motor / TM4C123

  • (IN1) -> PF0
  • (IN2) -> PF1
  • (IN3) -> PF2
  • (IN4) -> PF3

Stepper Motor Sequence Control Logic TM4C123

There are three ways to provide control sequence to coils of unipolar stepper motor such as full-drive, half drive and wave-drive. It is beyond the scope of this tutorial to provide in-depth details of these types. However, we will discuss the control sequence of all these three types. 

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. 

StepBluePinkYellowOrange
11100
20110
30011
41001

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. 

StepBluePinkYellowOrange
11000
21100
30100
40110
50011
60001
71001
81000

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. 

StepBluePinkYellowOrange
11 000
20100
30010
40001

Input sequences as shown in the above tables are provided through GPIO pins of microcontrollers such as TM4C123 Tiva Launchpad.

Stepper Motor Control Code TM4C123G

This code uses the wave drive mode to drive the stepper motor. This code is written using Keil uvision. If you don’t know how to use Keil MDK for ARM, you can check these articles: 

/* This program drives the stepper motor in full rotation using wave drive mode*/
/*include register definition file of TM4C123GN6PM microcontroller */
#include "TM4C123GH6PM.h"

/* function prototype of milisecond delay routine */
void Delay_ms(int n);

int speed = 5; //* Vary the speed between 1-10 ms delay 
int i = 0;

/* Main function to drive motor */
int main(void)
{

    SYSCTL->RCGCGPIO |= 0x20;   /*Enable clock to PORTF */
    
    /* GPIOF->DATA PF3,PF2, PF1, PF0 pin initialization*/
    GPIOF->DIR |= 0x0F;         /* GPIOF->DATA  PF3,PF2, PF1, PF0as output */
    GPIOF->DEN |= 0x0F;         /* GPIOF->DATA  PF3,PF2, PF1, PF0 as digital pins */

	while(1)
{

	 for(i=0; i<250; i++) // take 250 steps to complete one revolution 
{
		// apply full drive clockwise rotation sequence 
	  GPIOF->DATA = 0x08;
    Delay_ms(4);
    GPIOF->DATA = 0x04;
    Delay_ms(4);
    GPIOF->DATA = 0x02;
    Delay_ms(4);
    GPIOF->DATA = 0x01;
    Delay_ms(4);
}
}	
		
}
/* Generates a delay in number of miliseocnds wit system clock of 16MHz */
void Delay_ms(int n)
{
    int a, b;
    for(a = 0 ; a < n; a++)
        for(b = 0; b < 3180; b++)
            {} /* execute NOP for one milisecond */
}

/* This function is called by the startup assembly code to perform system specific initialization tasks. */
void SystemInit(void)
{
    /* Grant coprocessor access */
    /* This is required since TM4C123G has a floating point coprocessor */
    SCB->CPACR |= 0x00f00000;
}

In this code, PORTF pins PF0, PF1, PF2 and PF3 are used to provide input sequence to stepper motor driver board. These pins are configured as digital output pins. If you don’t know how to configure GPIO pins of TM4C123, you can read these tutorials:

Hardware Demo

This video shows that stepper motor rotates in both clockwise and ant-clockwise direction. In order to change the above code of TM4C123 for anti-lclock wise rotation, just apply the sequence in opposite order.

Related Tutorials:

Leave a Comment