Categories: Firmware Guide-PIC18

DC Motor Interfacing using L293D with PIC18F4550

DC Motor

A DC motor is a rotary electrical machine used to convert electrical energy into mechanical energy. It works on the principle of electromagnetism. It contains a stator and rotor with field windings. DC current carrying windings when passed through a magnetic field experiences a perpendicular mechanical force according to Flemings Left-Hand rule, producing a rotatory motion of the rotor. The direction of rotation can be reversed by reversing the direction of current passing through the DC motor. An H Bridge could be used to this end. A Simple DC motor is cheaper and faster than a stepper motor but cannot be used for precise movements.

L293D Motor Driver IC

A microcontroller alone cannot provide adequate current for operating a DC Motor. To interface the DC motor with a microcontroller we need to use a driver circuit or driver IC.

L293D is a dual H-bridge, high current motor driver Integrated Circuit. It acts as a current amplifier as it takes a low current input signal from the microcontroller and provides high current output to the motor. It supports a peak voltage of 36 V and a peak current of 600mA per channel. 

The motor supply voltage is provided at Vcc2 (Pin 8) within a wide range of 4.5 V to 36 V. The L293D also requires a 5 V supply at Vcc1 (Pin 16) for internal logic translation. It has four independent driver channels which can be used to drive four different DC motors in one direction. The first two channels are enabled by 1,2EN (Pin1) and the last two channels with 3,4EN (Pin 16). If enabled, each channel output pins provides the voltage provided at Vcc2 when the channel is turned ON by providing a logical HIGH (5 V) signal at its corresponding input pin. The motor driver could also be used as a dual H Bridge to drive two separate DC motors in both directions.

Circuit Diagram

Here we are providing a motor power supply of 9 V by default. An additional external power supply could also be provided and the greater voltage will be used to drive the motor. This output voltage could be used to drive up to four independent motors with a negative end connected to ground. It can also be used to implement a bi-directional motor driver with an H Bridge.

H Bridge Implementation

An H Bridge is used to obtain the bidirectional rotation of a DC motor. Its structure resembles that of the English alphabet ‘H’ and has four switches S1, S2, S3, and S4.

Closing S1 and S4, with S2 and S3, kept open, will give us a rotation in one direction and toggling all the switches will give us a rotation in the opposite direction.

The same concept could be implemented with the help of two channels of an L293D.

Here the motor is connected between the output pins of Channel 1 and Channel 2. The motor could be rotated by enabling both channels with the 1,2EN pin and turning ON one channel while keeping the other channel OFF. The following table will give us the motor output obtained in each case.

The same method can be used to operate another DC motor with Channel 3 and Channel 4. Thus the L293D can be used to operate to DC motors bidirectionally, making it a dual H Bridge motor driver.

DC Motor Interfacing using L293D with PIC18F4550

To operate a DC motor connected to the L293D, we need to provide appropriate enable and input signals. Let us consider the case where we need to operate a DC motor connected between OUT1 and OUT2. To operate the motor, we need to enable the channel by giving a HIGH signal to the 12EN pin of the L293D. This will enable both Channel 1 and Channel 2. To turn ON Channel 1 output we need to provide a HIGH signal at IN1. This will provide a high current voltage through the OUT1 pin of the L293D. The Channel 2 could be turned OFF by providing a LOW signal at IN2 to make the OUT2 voltage equal to the GND voltage.

Firmware Example:

#define EN12 PORTCbits.RC0      //Defining 12EN pin
#define EN34 PORTCbits.RC1      //Defining 34EN pin
#define IN1 PORTCbits.RC4       //Defining IN1 pin
#define IN2 PORTCbits.RC5       //Defining IN2 pin

TRISC = 0x00; //Defining Port C as Output port

EN12=1; //Enabling Channel 1 and Channel 2
EN34=0; //Disabling Channel 3 and Channel 4
IN1=1; //Turning ON Channel 1
IN2=0; //Turning OFF Channel 2

Speed control of DC motor

The normal functioning of an L293D will provide a constant High voltage at the output pin giving us the full speed rotation of the motor at the provided supply voltage. This speed can also be controlled by using a Pulse Width Modulated (PWM) signal to drive the motor. A PWM signal is defined by its duty cycle and time period. The duty cycle describes the amount of time the signal remains in the HIGH state in a given cycle and the time period describes the duration of a single cycle.

A PWM signal could be applied at the enable pin or the input pin of the L293D which will provide motor rotation only at the HIGH state of the signal. Hence this method could be used to control the speed of the DC motor by varying the duty cycle of the applied signal. A more descriptive chapter of using PWM signal to drive the motor can be found in our guide Speed Control of DC Motor Using PWM.

 

Share