Interfacing DC Motor with PIC Microcontroller – MikroC

Interfacing L293D with PIC

Interfacing DC Motor with PIC Microcontroller – MikroC

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.

H-Bridge DC Motor Driving circuit

                     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

Figure 2: L293 and L293D pin 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.

H-Bridge DC Motor Driving circuit

                             Figure 4: L293D Motor Driving Chip Circuit

Buy a Motor Driving Chip from our Store

MikroC Source Code

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.

You can download the full project files (mikroC 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 mikroC Project: DC_Motor_mikroC_Project

Download Proteus Schematic: DC_Motor_Proteus_Project

Share this post

has been added to your cart:
Select your currency