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


  • Learn how to use 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

PWM (Pulse Width Modulation) uses digital signals to control the average power across analog devices. PWM signal is essentially a fixed-frequency square wave with adjustable pulse width. A common way to use PWM is to control dimming of LED, to control the speed of a motor, or to control the angle of a servo.

Required Components List

5V DC Motor x 1
ic 16pin DipChip s L293D Motor Driver x 1
breadboard s Breadboard x 1
breadboard power s Power Supply Module x 1

Circuit / Schematic Diagram

Motor will typically draw more current that a micocontroller can support. Therefore, the L293D will be used to provider power to the motor, and its input pin connect 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 shutdown the USB port, and you need to disconnect the Tiva board with USB port, shutdown 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 motor controller, another one is connected to on-board LED to control the bright of LED. The code needs to update the duty cycle on both PWM signals.


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

  1. The frequency of PWM output single is 100Hz
  2. The range of duty cycle is 0% to 100%
  3. Left-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.


In this lab, the Tiva LaunchPad will generate two PWM signals. In the code, the PWM duty cycle will be updated from 0% to 100% in 5% increments, and then reset the duty cycle to 0%. The intervals between the updating are 100 ms.

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

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:
    • 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 the zero, it will increase the duty cycle of the PWM signal.
  • For Right-Aligned PWM:
    • 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 the 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%, so that means the value for CMP register must less than the LOAD value.

0 \le CMP < LOAD

Set the initial value for the duty cycle to 0%.


Sample Firmware Code

Lab Experiments

PWM frequency is 100Hz.



  1. What is the truth table for the L293D chip?
  2. Observes the motor speed in experiment 1 and 2, what is the difference after changing the wire connection on the motor? Explain why?
  3. How to produce a Right-aligned PWM signal when you configure the signal GEN? Explain it.
  4. If the System Clock is 50MHz, and the frequency of the PWM outputs is 200Hz. 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?