DC Motor Control Using L293D in Raspberry Pi2

As we know that a DC Motor is the equipment which converts electrical power to mechanical power. So it is widely used in many areas especially in the field of Robotics where its applications varies according to the type of DC motors, ranges from a small plastic gear motor to a large metal gear motor with gear box itself. In this tutorial we make use of a plastic gear motor in which its rotation is controlled with a L293D motor driver ic and that allows the motor to rotate in both the direction. The L293D is designed to provide bidirectional drive currents of up to 600-mA at voltages from 4.5 V to 36 V.

In the hardware part Raspberry Pi GPIO pins is used and we make use of a keyboard to enter the commands for varying the actions. The main thing to be noted is that the DC motor should be powered only through a power supply or a suitable battery bank and don’t connect the module directly to the raspberry Pi. As the motor needs about 400 mA current to start, It could not produced by the Pi, and the Pi will be damaged. Also while doing the connections the Pi2 module should be off to avoid damages due to shorting by mistakes.

Modules Needed

Raspberry Pi2

Plastic Gear Motor

L293D ic

Power supply or Battery (5v)

Bread Board

Connecting Wires

Connection Diagram

L293D Interfacing Diagram

Data wire Connection:

L293D–Pin 1 connected to GPIO 25–Pin 22

L293D–Pin 2 connected to GPIO 24–Pin 18

L293D–Pin 7 connected to GPIO 23–Pin 16

Now we check these connections and working by a python script. Open the python file and write a sample program as shown below:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

import RPi.GPIO asGPIO

from time import sleep

GPIO.setmode(GPIO.BOARD)

Motor1=16# Input Pin

Motor2=18# Input Pin

Motor3=22# Enable Pin

GPIO.setup(Motor1,GPIO.OUT)

GPIO.setup(Motor2,GPIO.OUT)

GPIO.setup(Motor3,GPIO.OUT)

print"FORWARD MOTION"

GPIO.output(Motor1,GPIO.HIGH)

GPIO.output(Motor2,GPIO.LOW)

GPIO.output(Motor3,GPIO.HIGH)

sleep(3)

print"BACKWARD MOTION"

GPIO.output(Motor1,GPIO.LOW)

GPIO.output(Motor2,GPIO.HIGH)

GPIO.output(Motor3,GPIO.HIGH)

sleep(3)

print"STOP"

GPIO.output(Motor3,GPIO.LOW)

GPIO.cleanup()

Import the Rpi GPIO pins and the time module. Pins 22,18, & 16 should be set as output pins. Pin 22 of the RPi2 is connected to the enable pin of the L293D, so it should be made to high at the running time of the motor. When it is made to low the motor will be off. Motor1 and Motor2 are the input pins. When both the pins are made to high or low simultaneously the motor will not rotate and when one is high and other is low the motor will rotate in a direction. To rotate in the reverse direction the input condition should also be reversed.

The next process is to change the motor direction through keyboard. To get an easy access for the keypress function we make use of the opencv library. When a particular key is pressed the direction of the motor should be reversed. Copy an image and paste it in the working folder where this program lies. Now open the Python script and edit the program shown below:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

import cv2

import RPi.GPIO asGPIO

from time import sleep

GPIO.setmode(GPIO.BOARD)

Motor1=16

Motor2=18

Motor3=22

GPIO.setup(Motor1,GPIO.OUT)

GPIO.setup(Motor2,GPIO.OUT)

GPIO.setup(Motor3,GPIO.OUT)

ck=1

whileck==1:

vis=cv2.imread("pic5.png")

cv2.imshow('image',vis)

ch=0xFF&amp;cv2.waitKey(1)

ifch==ord('1'):# Press the key '1'

print"FORWARD MOTION"

GPIO.output(Motor1,GPIO.HIGH)

GPIO.output(Motor2,GPIO.LOW)

GPIO.output(Motor3,GPIO.HIGH)

ifch==ord('2'):# Press the key '2'

print"BACKWARD MOTION"

GPIO.output(Motor1,GPIO.LOW)

GPIO.output(Motor2,GPIO.HIGH)

GPIO.output(Motor3,GPIO.HIGH)

ifch==ord('3'):# Press the key '3'

print"STOP"

GPIO.output(Motor3,GPIO.LOW)

ifch==ord('4'):# Press the key '4'

print"FINISHED"

ck=4

GPIO.cleanup()

By pressing the suitable keys the corresponding actions will be performed.