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

  1. Raspberry Pi2
  2. Plastic Gear Motor
  3. L293D ic
  4. Power supply or Battery (5v)
  5. Bread Board
  6. 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:

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:

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

Shop With Us

Resources

L293D Datsheet


Leave a Reply