Академический Документы
Профессиональный Документы
Культура Документы
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!
Required Components
1 PIC16F877A Add
1 BreadBoard Add
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)
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.
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.
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
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.
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:
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.
Lab Number 14
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
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.
3 TRISD1 = 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
2 CCP1M3 = 1;
3 CCP1M2 = 1;
6 PR2 = 124;
8 T2CKPS0 = 1;
9 T2CKPS1 = 0;
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
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
1 /*
2 * LAB Number: 14
6 *
7 */
9 #include <xc.h>
10 #include <stdint.h>
11 #include "config.h"
19 //================================
21 void PWM1_Set_Duty(uint16_t);
22
23 void main(void)
24 {
28 TRISD0 = 0;
29 TRISD1 = 0;
32 RD0 = 0;
33 RD1 = 1;
34
36 CCP1M3 = 1;
37 CCP1M2 = 1;
40 PR2 = 124;
42 T2CKPS0 = 1;
43 T2CKPS1 = 0;
46
48 while(1)
49 {
51 {
52 RD0 = ~RD0;
53 RD1 = ~RD1;
55 }
56 if(LV0) // 0% DC
57 {
58 PWM1_Set_Duty(0);
60 }
61 if(LV1) // 50% DC
62 {
63 PWM1_Set_Duty(250);
65 }
66 if(LV2) // 75% DC
67 {
68 PWM1_Set_Duty(375);
70 }
71 if (LV3) // 100% DC
72 {
73 PWM1_Set_Duty(500);
75 }
77 }
78 return;
79 }
80
83 {
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.