Interfacing DC Motor with PIC Microcontroller – FlowcodeBitahwa Bindu
Watch the Video Tutorial
DC motors are used in many industrial, commercial, and domestic applications. We have DC motors in toys, irrigation pumps, robotics, Drills and in many applications. In this article, we are going to learn how to interface a DC motor with a PIC Microcontroller, rotating it in either the clockwise or anticlockwise direction.
A DC Motor cannot be driven directly from a Microcontroller’s pin. Normally DC Motors require high current and high voltage than a Microcontroller can handle as Microcontrollers usually operates at +5 or +3.3V supply and it I/O pin can provide only up to 25mA current which on most cases is not enough for a motor. Typical small DC Motors require 12V supply and about 300mA current which way beyond what a Microcontroller can handle, however there are a couple of interfacing techniques that can be used.
The solution is to use an H-bridge circuit constructed from four MOSFET transistors, as shown on figure 1 below.
Figure 1: H-Bridge DC Motor Driving circuit
To Switch OFF/STOP the motor, a logic ‘0’ should be applied to RB0, RB1, RB2 and RB3. To Switch ON the Motor Clockwise, a logic ‘1’ should be applied to RB0 and RB2 while leaving RB1 and RB3 on logic ‘0’. To Reverse (Anticlockwise) the Motor, RB1 and RB3 should be set high (1) while RB0 and RB2 set low (0). Because a motor is an inductive load, a back emf could destroy the transistors when the motor switches OFF, the four Diodes are used to suppress the back emf.
Instead of using four transistors, we could have used a motor controller chip, for example the L293D.
L293D Quadruple Half-H Drivers
The L293 and L293D are quadruple high-current half-H drivers. The L293 is designed to provide bidirectional drive currents of up to 1 A at voltages from 4.5 V to 36 V. The L293D is designed to provide bidirectional drive currents of up to 600-mA at voltages from 4.5 V to 36 V. Both devices are designed to drive inductive loads such as relays, solenoids, dc and bipolar stepping motors, as well as other high-current/high-voltage loads in positive-supply applications.
L293 and L293D Features
- Wide Supply-Voltage Range: 4.5 V to 36 V
- Separate Input-Logic Supply
- Thermal Shutdown
- High-Noise-Immunity Inputs
- Output Current 1 A Per Channel (600 mA for L293D)
- Peak Output Current 2 A Per Channel (1.2 A for L293D)
- Output Clamp Diodes for Inductive Transient Suppression (L293D)
L293 and L293D Diagram
- Vcc1 : Motor Supply
- Vcc2: Input Logic
- A : Input
- Y : Output
- 1, 2 EN : Enable Of First Pair 1, 2
- 3, 4 EN : Enable of Second Pair 3, 4
The motor is connected directly to this chip. Figure 3 shows the internal block diagram of the L293/L293D Motor Controler. Drivers are enabled in pairs. Logic ‘1’ at EN1,2 pin enables the drivers 1 and 2. Logic ‘1’ at EN 3,4 enables the drivers 3 and 4. Logic Voltage should be provided to the Vcc1 pin and motor supply is given to Vcc2 pin. For example, if your motor works at 12v and you are going to control it with control signals from a PIC Microcontroller. Then you have to connect 12V to Vcc2 (pin 8) and 5V to Vcc1 (pin 16) and the ground of both supplies should be common. For more information, you can read the L293 and L293D Datasheet
Figure 3: L293 and L293D block diagram
In this example on figure 4 below, we are using only the first pair of drivers to drive one DC Motor. First pair of drivers are enabled by connecting EN1 to Logic HIGH. IN1 and IN2 are connected to RB0 and RB1 of PIC Microcontroller respectively which are used to provide control signal to the DC Motor. DC Motor is connected to OUT1 and OUT2 of the L293D. By connecting the EN pin to a PWM pin of a PIC Microcontroller, the speed of the motor can be controlled.
Here how the Control Signals and Motor Status:
- RB0: logic 0 and RB1: logic 0: The motor stops.
- RB0: logic 1 and RB1: logic 0: The motor rotates in Clockwise direction.
- RB0: logic 0 and RB1: logic 1: The motor rotates in Anti-Clockwise direction.
- RB0: logic 1 and RB1: logic 1: The motor stops.
Figure 4: L293D Motor Driving Chip Circuit
Flowcode has a motor template component under the Mechatronics components group. We’re gonna use the full bridge motor template for our simulation.
The motor can be added to the 3D system panel or the 2D dashboard panel. In this example we will add it to the 3D system panel as shown on figure 6 below.
We have some few properties for this component as shown on figure 7 below:
Figure 7: Motor Properties
Pin A and Pin B: Here you can specify which pins of the microcontroller will control this motor, in this example we specified Pin A to PORTB.0 and Pin B to PORTB.1
Rotation pattern: This is the state of the pins to drive the motor in a particular direction. Like the L293D pattern, we’re gonna use the same pattern in this example, for forward rotation: Pin A = logic 1 and Pin B = logic 0. For reverse: Pin A = logic 0 and Pin B = logic 1 and for stop: Pin A = logic 0 and Pin B = logic 0.
The motor settings are just for simulation and won’t affect the output code.
Speed: Is the simulation speed of rotation of the motor in either direction. We used 250 in this example.
Acceleration and Deceleration: Is the speed change per second when the motor is powered ON or Costing when there is no power. A value of 0 will indicate instantaneous speed change.
In this example (figure 4), the PIC18F26K20 will send control signal to rotate the motor in clockwise position for 5 seconds, then stops for 2 seconds, then rotates in anti-clockwise position for 5 seconds, then the process starts again. We are using Flowcode component Macro: StartForwards(), Stop() and StartReverse(). We could also use the Output icons as well in this example with similar results.
You can download the full project files (Flowcode source code and Proteus Schematic design) below here. All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).
Download Flowcode Project: DC_Motor_Flowcode
Download Proteus Schematic: DC_Motor_Proteus_Project