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
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 motor 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
|5V DC Motor||x 1|
|L293D Motor Driver||x 1|
|Power Supply Module||x 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 USB port, shutdown, and restart your computer to reset the USB port.
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:
- The frequency of PWM output single is 100Hz
- The range of the duty cycle for PWM could be from 0% to 100%
- 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.
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.
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.
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 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 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:
That means the value for CMP register must less than the LOAD value.
Set the initial value for the duty cycle to 0%.
Add the following definitions to the MyDefines.h file:
Sample Firmware Code
- Generate two PWM signals for motor and LED. Then configure the frequency of PWM signals to 100Hz.
- 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
Psueducode for main() function is shown in below:
create an integer array: cmpDuty
create integer variable: i
manually calculate the cmp value with duty cycle 0%, 30%, 60%, and 100%, and set them to the array cmpDuty
set PWM?->_?_CMP with cmpDytuy array with index i
increase i value
check the i value in the range of the array size. Otherwise, reset i value to 0
delay for 500 msec
- What is the truth table for the L293D chip?
- Observes the motor speed in experiments 1 and 2, what is the difference after changing the wire connection on the motor? Explain why?
Lab Duty Cycle 0% 30% 60% 100% #1: Motor Speed (H/M/L/0) #2: Motor Speed (H/M/L/0)
- How to produce a right-aligned PWM signal when you configure the GEN register? Explain it.
- 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
- 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)