Вы находитесь на странице: 1из 16

DC Motor Control Tutorial – L293D Motor Driver | PWM | H-Bridge

BY KHALED MAGDY · PUBLISHED SEPTEMBER 7, 2018 · UPDATED JULY 7, 2019

Previous Tutorial Tutorial 16 Next Tutorial

DC Motor Speed Control With L293D IC | PWM

Introductory Level ★☆☆☆☆

In this tutorial, you’ll learn how DC motors work, How to control both speed & direction of a DC motor.
You’ll also get familiar with the required hardware driving circuitry. We’ll develop the required firmware
code in order to control a DC motor as a practical Lab at the end of this tutorial. We’ll finally draw in
some concluding remarks. So let’s get started!

Tutorial Contents [show]

Required Components

Qty. Component Name Buy On Amazon.com

1 PIC16F877A Add

1 BreadBoard Add

1 DC Motor 12v 1000RPM Add

5 Push Buttons Add

1 Resistors Kit Add Add

1 Capacitors Kit Add Add

1 Jumper Wires Pack Add Add

1 L293D DC Motor Driver Add Add

1 LM7805 Voltage Regulator (5v) Add

1 Crystal Oscillator Add


1 PICkit2 or 3 Programmer Add

2 9v Battery or DC Power Supply Add Add Add

The Prototyping Board Setup

How A DC Motor Works?

DC Motors are very simple rotary actuators that transform electrical energy into a mechanical rotation
at a specific torque. A typical DC motor will have the following features:
 Torque (in kg.cm)

 Rated Rotation Speed (RPM)

 Rated Full-Load current (e.x. 2A)

 Rated No-Load current (e.x. 0.2A)

 Rated voltage for operation (e.x. 12v)

image source (Renesas Electronics) @ renesas.com

Electrical current is passing through coils that are arranged within a fixed magnetic field (Stator). The
current generates magnetic fields in the coils. Which in turns causes the coil assembly (Rotor) to rotate,
as each coil is pushed away from the like-pole and attracted to the unlike-pole of the stator.

Reversing the direction of current flow in the coil translates to an inversion in the direction of the rotor’s
magnetic field. Which in turns applies an inversed torque of each side of the coil resulting in a reverse
direction in the rotation.
This is simply how a DC motor is actually working. Now, it’s our mission to control the behavior of such a
device as we’ll discuss in the section right below.

How To Control DC Motors?

Controlling a DC motor involves two different processes. The 1st one is controlling the direction of the
motor’s rotation CW & CCW (Clock-Wise & Counter-Clock-Wise). The 2nd one is controlling the speed of
rotation. Hereafter, we’ll discuss each of these processes in more detail.

Direction Control

To control the direction of a DC motor, you need to reverse the polarity of the input power source. In
fact, polarity-reversal circuitry has many implications and it can go really complex and tricky. This sort of
driver circuitry is called to be H-Bridge. it’s an H-shaped circuitry in which the DC motor is hooked
through Switches/Transistors between the power rails ( VM & Ground ). Altering the activated switches
reverses the polarity of the voltage (VM) applied to the DC Motor. Hence, the rotation direction is
reversed.
Actually, a real-world H-bridge will not have ON/OFF switches. Instead, there will be 4-Transistors which
are basically electronic switches doing the job of reversing the polarity of the motor supply voltage.
Designing and building a fully-functioning H-Bridge circuit is beyond the intent of this tutorial. However,
we’ll discuss a couple of ways to get polarity-reversal H-Bridge solutions on the cheap and so easily with
the least overhead.

I- L293D Motor Driver IC

This integrated circuit (IC) is a 2-channel full H-Bridge motor driver. That is capable of driving two
distinct DC motors. It’s the easiest way to go around building a whole H-Bridge driver from scratch. It
comes in a DIP package which fits nicely on a typical breadboard. The L293D provides an easy wire-up
interface with some decent features as follows:

 Motor Channels: 2

 Output Current Per Channel: 0.6A

 Output Peak Current (Non-Continuous): 1.2A

 Supply Voltage: up to 36v

 Switching Frequency: up to 5kHz

 Thermal Shutdown (Overheat Protection)


Here is the pinout diagram for an L293D motor driver ic

This is actually the driver circuitry we’ll be using in the following practical lab hereafter in this tutorial.
However, I’ll also show you another simple way of driving DC motors in case you’re interested or don’t
have an available L293D chip.

II- 2-Channel Relay Module

The rated values of voltage, current for each relay is printed/labeled on the electronic device package
itself. Which you should be choosing carefully. Especially when driving large DC motors, but for now a
small DC motor will cause no problem to any given relay. A couple of relays are basically capable of
driving a single DC motor as shown in the diagram right below.
image source http://healthygets.info

Activating (energizing) the coil of each relay will connect the COM pin to the NO pin. Hence the DC
motor pins are connected to (the VM+ or to the V-). Which in turns makes the DC motor rotate clockwise
(control signal 1,0), counter-clockwise (control signal 0,1) or stop! (when signal is 1,1 or 0,0).

Speed Control

Controlling the speed of a DC motor’s rotation (Permanent Magnet Motors) is basically achieved by a
couple of ways as shown below:

 Variable Supply Voltage

 Using PWM-Controlled Gate

I- Variable Supply Voltage: can be achieved using voltage regulation circuitries. However. there are too
many limitations and drawbacks to such a method. But theoretically, it just works and does the job!
Varying the supply voltage will definitely control the motor’s speed accordingly.

II- Using PWM-Controlled Gate: is the most common technique for digitally-controlled systems. It’s
basically done be isolating one of the power source rails from the H-Bridge circuitry using a transistor.
Hence, creating an open circuit with the (Ground or VM+). In this way, activating the Control Gate (the
transistor) will cause the H-Bridge to be powered-up then the motor will start rotating. Regardless of the
direction of rotations, activating the control gate with a PWM signal will somehow control the average
voltage being delivered from the supply to the motor through the control transistor. A simplified
diagram is shown right below
The gate transistor in this setting is controlling whether the negative power rail is connected to the H-
bridge circuit or not. By turning this transistor ON/OFF using a PWM signal, we’ll be seemingly
controlling the average voltage being delivered to the DC Motor.

DC Motor Speed Control – LAB

Lab Name DC Motor Speed Control (PWM)

Lab Number 14

Lab Level Beginner

Learn how to use PWM signal to control the speed of a DC Motor. Also,
you should be using an L293D Motor Driver IC in order to control the
Lab Objectives direction of motor’s rotation. Push buttons should be used to specify
the speed 0%, 50%, 75%, 100% and an extra button for reversing the
direction of rotation.

1. Coding

Open the MPLAB IDE and create a new project name it “DC_Motor_PWM”. If you have some issues
doing so, you can always refer to the previous tutorial using the link below.
Set the configuration bits to match the generic setting which we’ve stated earlier. And if you also find
troubles creating this file, you can always refer to the previous tutorial using the link below.

Now, open the main.c file and let’s start developing the firmware for our project.

Our first task is to define the IO pins used for controlling (choosing) the desired speed levels and
direction reversal. Configuring these input pins can be done as shown below

1 #define Rev RB0 // Reverse Direction Button

2 #define LV0 RB1 // 0% Speed Button

3 #define LV1 RB2 // 50% Speed Button

4 #define LV2 RB3 // 75% Speed Button

5 #define LV3 RB4 // 100% Speed Button

7 TRISB = 0x1F; // Low 5-pins are input pins

There should also be a couple of output pins in order to send the direction control logic signal to the
motor driver. Let’s use a couple of PORTD pins for this function.

1 // Set The Direction To Be Output Pins


2 TRISD0 = 0;

3 TRISD1 = 0;

4 // Initially (0, 1) Say it's ClockWise rotation!

5 // For Reversing The Direction Write (1, 0)

6 RD0 = 0;

7 RD1 = 1;

Then we should configure the CCP module to operate in PWM mode. And to output a PWM signal with a
2kHz signal (any frequency is acceptable as long as it’s inside the 5kHz window). For this, we’ll be using
the same lines of code in the previous lab

1 //--[ Configure The CCP Module For PWM Mode ]--

2 CCP1M3 = 1;

3 CCP1M2 = 1;

4 TRISC2 = 0; // The CCP1 Output Pin (PWM)

5 // Set The PWM Frequency (2kHz)

6 PR2 = 124;

7 // Set The PS For Timer2 (1:4 Ratio)

8 T2CKPS0 = 1;

9 T2CKPS1 = 0;

10 // Start CCP1 PWM !

11 TMR2ON = 1;

Now, it’s time to write the main loop (routine) of the system. Which is basically polling the Buttons and
apply the respective action of varying the DC (duty cycle) or reversing the direction of rotation. Let’s first
calculate the DC values to be written in each case for (0%, 50%, 75%, 100%) speed levels.

Using the exact resolution equation. And by substituting for Fosc, FPWM, and TMR2Presclaer in the
following equation, we get the exact resolution for our PWM output
Resolution = 8.96578 bits

Hence, the maximum duty cycle 100% is given by writing 2Resolution = 28.96578 = 500

Now, if 100% DC (duty cycle) is produced by writing 500 to the 10-Bit DC buffer register

Then, 50% DC is given by writing 250

And, 75% DC is given by writing 375

And of course, 0% DC is obtained by moving a 0 to the DC buffer register.

Obviously, we’re now done with writing the firmware. However, here is the full code listing for this LAB
for you to use as a fully-functioning starting point for your projects. Which you can play around and
make some changes to meet your application’s needs

The Full Code Listing

1 /*

2 * LAB Number: 14

3 * LAB Name: PWM - DC Motor Speed Control

4 * Author: Khaled Magdy

5 * For More Information Visit My Website @ DeepBlueMbedded.com

6 *

7 */

9 #include <xc.h>

10 #include <stdint.h>

11 #include "config.h"

12 #define _XTAL_FREQ 4000000

13 //--[ Pin Definitions ]--

14 #define Rev RB0 // Reverse Direction Button

15 #define LV0 RB1 // 0% Speed Button

16 #define LV1 RB2 // 50% Speed Button


17 #define LV2 RB3 // 75% Speed Button

18 #define LV3 RB4 // 100% Speed Button

19 //================================

20 //--[ Function Declarations ]--

21 void PWM1_Set_Duty(uint16_t);

22

23 void main(void)

24 {

25 //--[ Configuration For IO Pins ]--

26 TRISB = 0x1F; // Low 5-pins are input pins

27 // Set The Direction To Be Output Pins

28 TRISD0 = 0;

29 TRISD1 = 0;

30 // Initially (0, 1) Say it's ClockWise rotation!

31 // For Reversing The Direction Write (1, 0)

32 RD0 = 0;

33 RD1 = 1;

34

35 //--[ Configure The CCP Module For PWM Mode ]--

36 CCP1M3 = 1;

37 CCP1M2 = 1;

38 TRISC2 = 0; // The CCP1 Output Pin (PWM)

39 // Set The PWM Frequency (2kHz)

40 PR2 = 124;

41 // Set The PS For Timer2 (1:4 Ratio)

42 T2CKPS0 = 1;

43 T2CKPS1 = 0;

44 // Start CCP1 PWM !


45 TMR2ON = 1;

46

47 // The Main Loop (Routine)

48 while(1)

49 {

50 if(Rev) // Reverse The Direction

51 {

52 RD0 = ~RD0;

53 RD1 = ~RD1;

54 __delay_ms(500); // Wait For The Button To Be Released

55 }

56 if(LV0) // 0% DC

57 {

58 PWM1_Set_Duty(0);

59 __delay_ms(100); // Wait For The Button To Be Released

60 }

61 if(LV1) // 50% DC

62 {

63 PWM1_Set_Duty(250);

64 __delay_ms(100); // Wait For The Button To Be Released

65 }

66 if(LV2) // 75% DC

67 {

68 PWM1_Set_Duty(375);

69 __delay_ms(100); // Wait For The Button To Be Released

70 }

71 if (LV3) // 100% DC

72 {
73 PWM1_Set_Duty(500);

74 __delay_ms(100); // Wait For The Button To Be Released

75 }

76 __delay_ms(10); // Wait To Reduce The Simulation Overloading

77 }

78 return;

79 }

80

81 // Definition For PWM1_Set_Duty Function

82 void PWM1_Set_Duty(uint16_t DC)

83 {

84 // Check The DC Value To Make Sure it's Within 10-Bit Range

85 if(DC<1024)

86 {

87 CCP1Y = DC & 1;

88 CCP1X = DC & 2;

89 CCPR1L = DC >> 2;

90 }

91 }

2. Simulation

To simulate the project, just hook an 5 Pull-Down Push Buttons to the pins RB0-RB4. Connect the L293D
IC referring to the following connection diagram
Add the hex file and run the simulator. Here is the schematic for the full circuit diagram.

Note

please, be advised that there are some inconsistencies in pinouts of the L293D
simulation packages. So you should always refer to the datasheet to make sure you’re
getting everything correctly. Or you can just follow the hand-drawn diagrams shown
earlier.

It’s highly recommended to hook an oscilloscope to the PWM output line in order to have a clear vision
of what is exactly going on. Due to many factors, the motor speed of rotation may not be running in
real-time on simulators. And here is the typical simulation results!

3. Prototyping

Prototyping this project involves connecting a relatively larger power supply (12v). You should be careful
about the motor driver connections. Just follow the schematic diagrams carefully and run your project!
And here is the final running project in case you’re curious.

Вам также может понравиться