Tiva Lab 07: Controlling a DC Motor and LED Using PWM


  • Learn how to use the PWM signal to change the brightness of an LED, and the speed of a small DC motor.
  • Learn how to calculate the LOAD and CMP values for the PWM signal.

Required Reading Material

Background Information

DC motor is a rotating machine that converts direct current electrical energy into mechanical energy. It is widely used in electrical power tools, toys, and appliances. They could be powered by a small battery to the DC power adapter. The basic working principle of a DC motor is that whenever a current-carrying conductor is placed in a magnetic field, it experiences a mechanical force that has the tendency to move. If the direction of the current is reversed, the rotation of the motor will also be reversed.

The speed control of DC motors for various applications is very important. There are two methods to control the DC motor speed: current control and voltage control. Voltage control is not a good idea as on low voltage could lose torque, hence the best way to control the speed is by current control. A simple method of controlling the current is to add a variable resistor in series with the motor and adjust the resistance of the variable resistor to change the current flowing through the motor. But this method is not efficient, because the variable resistor also consumes energy.

The PWM (pulse width modulation) method is a very efficient method and is the most commonly used method. PWM uses digital signals to control the average power across analog devices. It is essentially a fixed-frequency square wave with adjustable pulse width. This method can obtain a smooth speed variation without reducing the starting torque of the motor.

Required Components List

DC Motor s 5V DC Motor × 1
R220Kohm 220ohm Resistor × 3
ic 16pin DipChip s L293D Motor Driver × 1
breadboard s Breadboard × 1
breadboard power s Power Supply Module × 1
PowerAdapter 64 Power Adapter × 1

Circuit / Schematic Diagram

The motor will typically draw more current than a microcontroller can support. Therefore, the L293D will be used to provide power to the motor, and its input pin connects to the PWM signal from the microcontroller. Plug the power supply module on the breadboard. The power supply module provides two power sources: +5V and +3.3V. Make sure the power source you connected to the circuit is +5V.

A power supply module must be used in this lab. Do not connect +5V from the Tiva board that directly connects to a USB port on the computer. Since the motor needs more current, it may cause USB over-current. If it happens, it will trigger the protection circuit to shut down the USB port, and you need to disconnect the Tiva board with the USB port, shut down, and restart your computer to reset the USB port.

breadboard power adapter s tiva ports 1 s

In this lab, the microcontroller needs to generate two PWM signals. One is connected to a DC motor through a motor controller, another one is connected to an onboard LED to control the brightness of the LED. The code needs to update the duty cycle on both PWM signals.


The requirements of the PWM output signal in this lab are shown as below:

  1. The frequency of PWM output single is 1000Hz
  2. The range of the duty cycle for PWM could be from 0% to 100%
  3. Right-aligned PWM signal

To calculate the PWM timer clock frequency, you have to know the default system clock frequency.

  • EK-TM4C123GXL LaunchPad: You need to uncheck the "Clock Configuration" in the Keil μVision. After you unchecked the setting, the default system clock is 16 MHz
  • EK-TM4C1294XL LaunchPad: By default, the system clock is 16 MHz.


lab01 PwmOutput
Figure 1: PWM Outputs for DC Motor and LED

You have to calculate the frequency of the PWM timer based on the system clock frequency and the PWM divisor.

{f_{PWMTimer}} = \frac{{SysClk}}{{PWMDivsor}}

Calculate the count value for the PWM signal. This value will be set to the LOAD register, which is 16-bit length only. If the count value you calculated is over 65535 (= 216-1), you need to reduce the frequency of PWM Timer by increasing the value of the PWM Divisor and then recalculate the count value again.

LOAD = Coun{t_{PWM}} = \frac{{{T_{PWM}}}}{{{T_{PWMTimer}}}} = \frac{{{f_{PWMTimer}}}}{{{f_{PWM}}}} \le 65535

Changing the CMP value in the PWM module will change the duty cycle of the PWM signal. To calculate the CMP value, you have to know the type of PWM signal that you used: Left-aligned or right-aligned PWM.

  • For Left-Aligned PWM:
    • $duty = {t \over T} \times 100\% = 1 - {{CMP} \over {LOAD}}$
    • If the CMP value is closing to the LOAD value, it will decrease the duty cycle of the PWM signal.
    • If the CMP value is closing to zero, it will increase the duty cycle of the PWM signal.
  • For Right-Aligned PWM:
    • $duty = {t \over T} \times 100\% = {{CMP} \over {LOAD}}$
    • If the CMP value is closing to the LOAD value, it will increase the duty cycle of the PWM signal.
    • If the CMP value is closing to zero, it will decrease the duty cycle of the PWM signal.

In this lab, the range of the duty cycle is from 0% to 100%, and the relationship between CMP and LOAD is:

0 \le CMP < LOAD

That means the value for the CMP register must less than the LOAD value. The detailed calculations for CMP values, you can find in this session.

Set the initial values of the duty cycle for both PWM outputs to 0%.


Add the following definitions to the MyDefines.h file:

MyDefines PWM 0
MyDefines PWM 1


Sample Firmware Code

Lab Experiments

  • Generate two PWM signals for motor and LED. Then configure the frequency of PWM signals to 1000Hz.
  • Set the initial duty cycle value to 0%. (Inside the Setup_PWM() function)
  • In the main() code, the PWM duty cycle will be updated from 0%, 30%, 60%, to 100%, then reset the duty cycle to 0% and continue to update the duty cyle.
  • The intervals between the updating are 2 second

for main() function is shown below:

// manually calculate the CMP value with duty cycle 0%, 40%, 70%, and 100%, and set them to the array cmpDuty

create an integer array with the initial values of the duty: cmpDuty[]
create an integer variable with an initial value 0: i


    increase i value by 1
    if i is greater than the maximum value of array index number, then set i value to 0
    set both PWMm->_g_CMP? to cmpDuty array with index i
    delay for 2000 msec (You can use software delay, or delay function in ezTivaLIB)



  1. What is the truth table for the L293D chip?
  2. Observes the motor speed in experiments 1 and 2, what is the difference after changing the wire connection on the motor? Explain why?
    ExpDuty Cycle
    #1: Motor Speed (Hi+/Hi/Med/Lo/0)
    #2: Motor Speed (Hi+/Hi/Med/Lo/0)
  3. How to produce a right-aligned PWM signal when you configure the GEN register? Explain it.
  4. If the System Clock is 50 MHz, and the frequency of the PWM outputs is 200 Hz. Calculate the following values:
    • PWM Divisor value:
    • LOAD value:
    • CMP value for 25% duty cycle in Left-Aligned PWM signal:
    • CMP value for 10% duty cycle in Right-Aligned PWM signal:
  5. What happens if the CMP value is set as the LOAD value? (In your code, set the CMP value to the same as the LOAD value, and observe the motor speed)