Tiva Lab 06: Stepper Motor Interface


  • Learn how to interface a stepper motor with a microcontroller
  • Learn how to drive a stepper motor
  • Learn how to calculate the number of steps for the stepper motor to rotate at a certain angular degree

Required Reading Materials

Background Information

In this Laboratory, you will write firmware for generating digital TTL signals that can be used to provide the stepping sequence for a four-phase unipolar stepper motor in full-step mode.

Stepper Motor: 28BYJ-48

stepper motor controller uln2003 s

The 28BYJ-48 is a 5-wire unipolar stepper motor that runs on 5 volts. The detailed specs of the 28BYJ-48 stepper motor are shown below:

Motor Type Unipolar stepper motor
Connection Type 5-wire connection (to the motor controller): Blue, Pink, Yellow, Orange, Red
Voltage 5 Volts DC
Number of Phases 4
Operating Frequency (PPS) 100Hz
Step Mode Half-Step Mode: 8-step control signal sequence (recommended)
Full-Step Mode: 4-step control signal sequence
Step Angle Half-Step Mode: 5.625º per step / 64 steps per revolution of the internal motor shaft.
Full-Step Mode: 11.25º per step / 32 steps per revolution of the internal motor shaft
Gear ratio The manufacturer specifies: 64:1
Experimental result: 63.68395:1
Full revolution = steps per motor rotation x gear ratio
For half-step: 64 steps per motor rotation x 63.68395 gear ratio ≒ 4076 steps per full revolution
Step Angle (1-2 phase) 5.625º / 64

Step Sequence

The 28BYJ-48 is a Unipolar Stepper Motor. It has 4 coils of wire that are powered sequentially to make the magnetic motor shaft spin. When using the full-step method, 2 of the 4 coils are powered at each step. For the half-step mode, the first step is to power coil 1,  then coil 1 and 2 together, then coil 2 only, and so on. With 4 coils, the half-step mode has 8 different signals. The following table shows the steps for half- and full-step modes.

WAVE DRIVE: 1-Phase at a Time (Full-Step)

Step 0  X
Step 1  X
Step 2  X
Step 3  X

Simplest, but least used.

HALF STEP: 1 or 2 Phases at a Time (Half-Step)

Step 0 X
Step 1 X X
Step 2 X
Step 3 X X
Step 4 X
Step 5 X X
Step 6 X
Step 7 X X

Smallest step angle. Medium torque.

FULL STEP: 2 Phases at a Time (Full-Step)

Step 0  X  X
Step 1 X X
Step 2 X  X
Step 3 X X

Strongest torque.


In this lab, you need to complete the firmware code to control a stepper motor.


Required Component List

Stepper Motor x 1
ULN2003 Stepper Motor Driver Module x 1
breadboard power s Breadboard Power Supply Module x 1
Power Adapter 9V/2A x 1
breadboard s Breadboard x 1

Circuit/Schematic Diagram

The stepper motor used in this lab is the 28BYJ-48, with gear reduction, hence providing relatively good torque but slow rotation. The ULN2003A is a Darlington high current transistor array used to drive the 5v stepper motor. The following diagrams are schematics of the internal coils of this stepper motor and how each coil connects to the ULN2003A for each Tiva LaunchPad board. These coils must be connected to the right output of the ULN2003A to ensure correct sequencing.


EK-TM4C123G LaunchPad - Circuit

StepperMotor 123G s

Device IN4 IN3 IN2 IN1
Port.Pin PC7 PC6 PC5 PC4

Pin Configurations

DevicePort.Pin Signal TypePCTLDirectionDrive Mode




  1. Create a new folder under the EE3450 folder and name it Lab06_StepperMotor.
  2. Launch the Keil μVisio and create a new project. Save the project to the project folder you just created in the previous step and set the project name to Lab06_StepperMotor.
  3. Add the Common folder to the include paths which is under the "Options for Target" setting.


Example Source Code

EK-TM4C123GXL LaunchPad - main.c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

#include "TM4C123GH6PM.h"
#include "MyDefines.h"
#include "ez123G.h"

void Setup_GPIO(void);

#define CW    0
#define CCW   1
void StepperMotor(int s, bool direction);
volatile uint32_t *Stepper = (uint32_t *)GPIO? + (_PIN? | .... );

int main()
    int i = 0;

    *Stepper = ____; // Step 0 value

        // If users press SW1, rotate CW for 5 steps

		// If users press SW2, rotate CCW for 5 steps

void Setup_GPIO(void)
    // GPIO Initialization and Configuration
    // 1. Enable Clock to the GPIO Modules (SYSCTL->RCGCGPIO |= (_PORTs);)
    SYSCTL->RCGCGPIO |= (__);
    // allow time for clock to stabilize (SYSCTL->PRGPIO)
    while ((SYSCTL->PRGPIO & (__) ) != (__) ){};
    // 2. Unlock GPIO only PD7, PF0 on TM4C123G; PD7, PE7 on TM4C1294 (GPIOx->LOCK = 0x4C4F434B; and GPIOx->CR = _PINs;)
    GPIOF->LOCK = 0x4C4F434B;   // Unlock for GPIOF
    GPIOF->CR |= _PIN0;         // Commit for PIN0
    GPIOF->LOCK = 0;
    // 3. Set Analog Mode Select bits for each Port (GPIOx->AMSEL = _PINs; 0=digital, 1=analog)
    // 4. Set Port Control Register for each Port (GPIOx->PCTL = PMCn << _PTCL_PINn, check the PCTL table)
    // 5. Set Alternate Function Select bits for each Port (GPIOx->AFSEL = _PINs; 0=regular I/O, 1=PCTL peripheral)
    // 6. Set Output pins for each Port (Direction of the Pins: GPIOx->DIR = _PINs; 0=input, 1=output)

    // 7. Set PUR bits for internal pull-up, PDR for pull-down reg, ODR for open drain (0: disable, 1=enable)

    // 8. Set Digital ENable register on all port.pins (GPIOx->DEN = _PINs; 0=disable, 1=enable)

void StepperMotor(int s, bool direction)
    int i;



Lab Experiments

Write a firmware code to control the stepper motor rotation:

  • When SW1 and SW2 are pressed simultaneously, the stepper motor rotates 90 degrees clockwise
  • When SW1 is pressed, the stepper motor rotates 5 steps clockwise (CW)
  • When SW2 is pressed, the stepper motor rotates 5 steps counterclockwise (CCW)

You will have to calculate the number of steps required to rotate 90 degrees from the stepper motor data sheet at the top of this lab.

There are three ways to implement the stepper motor control:



  1. A 10° per step stepper motor is given 60 steps clockwise (CW) and 15 steps counter-clockwise (CCW). Assuming it started at 0°, calculate the final position in degrees.