DC motor control using L293D Motor Shield and Arduino

DC motors are an essential component of many robotic and automation systems. They are simple to control and can be powered by a variety of voltage sources. The L293D motor shield can be used for all kinds of robotics projects. For example, see bluetooth car with Arduino tutorial where L293D shield is used and the car can be controlled using mobile app. In this arduino motor shield tutorial you will learn how to use the DK electronics L293D Motor Shield for Arduino to control the speed and direction of a DC motor. You will learn how to install the AFmotor library and how to use it for driving the DC motor. Program code and video demonstration are also provided.

The L293D motor shield is shown below.

L293D Motor Shield
 
The L293D motor shield is a popular add-on shield that can be used to operate motors, DC motors, stepper motors(see Stepper Motor Control using L293D IC and Arduino) and servo motors(see controlling servo motor with L293D Arduino motor shield). 
 
It is a compact and easy-to-use shield that can control up to two DC motors. The shield is equipped with an L293D integrated circuit, which is capable of driving motors with a voltage ranging from 4.5V to 36V and with a current of up to 600mA. The L293D motor shield provides a simple and convenient way to control DC motors, as it does not require any additional components or wiring.

Motor Driver Integrated Circuits

The motor driver module consist mainly of two L293D motor driver IC and a 74HC595 shift register IC along with supporting resistors, capacitors and connection ports. 

DK electronics L293D motor shield

Motor Connection Port

The L293D motor driver can drive:

- Four DC motors which can be controlled in bi-bidirectionally and varying speed using PWM

- Two Stepper Motors 

- Two Servo Motors

Motor connection of L293D motor driver shield

Power

The motor driver shield can be used to drive small motors which can operate with current requirement in the range of 600mA per motor and peak current of 1.2A and voltage requirement in the range from 4.5V to 25V. 

On the shield there is a power supply selection jumper. If you want to power both the Arduino and the motor(s) with external power supply then place the jumper. If you want separate power for Arduino and Motor(s), either because want to power Arduino using USB or external DC jack, remove the power selection jumper. It is better if we use different power supply for powering Arduino and motors and in that case we will remove the power selection jumper.

DK electronics L293D motor shield

Interfacing DC motor with Motor Shield

To control the DC motor using an L 293D motor shield and an Arduino, we need to connect the shield to the Arduino board. The shield has headers that are compatible with the headers on an Arduino board, so connecting the two is straightforward. After connecting the shield to the Arduino, we can connect the DC motor to the shield. The shield has two motor connections, each with its own set of power and control lines.

Once the shield is connected to the Arduino and the DC motor, we can write a sketch (program) to control the motor. The sketch will use the digital I/O pins on the Arduino to control the motor. To control the speed of the motor, we can use pulse-width modulation (PWM) on one of the digital I/O pins. PWM allows us to control the average voltage applied to the motor, which in turn controls its speed.

 In this tutorial we are going to attach a DC motor to the Motor connection port M1. We will use different power supply to drive the Arduino UNO and the motor so we will remove the power selection jumper. A 9V power supply is connected to the external power supply terminal. 

Interfacing DC motor with Motor Shield

Programming Arduino with Motor Shield

To program Arduino to run a DC motor using Motor Shield we will use the Adafruit Motor Shield Library. To install the library, from the Arduino IDE, go to Tools > Manager Libraries... 




Then from the Libary Manager window, search for AF motor, find Adafruit Motor Shield Library and install it. As shown below because the library in our case was already installed, the install button is disabled. But for you if you have not yet installed the library the install button will be available. Click on it to install the library and restart the IDE.


To use the library is fairly simple. Open a sketch and type in the following at the top in order to use the library functions. 

 #include <AFMotor.h>

After including the header file we have to create an object with some alias name. For DC motors we have to use the class AF_DCMotor to create a dc motor object. For example the following line create a DC motor object called motor. 

AF_DCMotor motor(1);

The motor object accepts two parameters which are the number(1, 2, 3 or 4) or the channel of the motor where the motor will be connected to. And the 2nd parameter is the frequency. The frequency setting for channel 1 and 2 are:

MOTOR12_64KHZ

or, MOTOR12_8KHZ

or, MOTOR12_2KHZ

or, MOTOR12_1KHZ

And the frequencies for channel 3 & 4 are: 

MOTOR34_64KHZ

or, MOTOR34_8KHZ

or, MOTOR34_1KHZ

 It is optional to specify the frequency parameter while creating the DC motor object. If we do not specify the frequency then the default frequency is 1KHz.

Once the DC object is created, we can use it's various methods. These methods are explained briefly below.

1. motor.setSpeed(parameter);

This setSpeed() method will set the speed of the DC motor which takes one parameter that ranges from 0(lowest speed) to maximum of 255(highest speed).

eg. motor.setSpeed(127);

2. motor.run(parameter)

This run() method will set the direction of rotation of the motor. It can take the parameters- FORWARD, BACKWARD, RELEASE

eg. motor.run(FORWARD)

Program code

The following program will move the DC motor attached to motor channel 1 of the motor shield either in forward, backward, accelerate, deaccelerate or stop according to command we send to the Arduino via serial port. If we send character 'f' then it will run the motor in forward direction, if we send character 'b' then the motor will run in backward direction, if we send 'a' then it will accelerate, if we send 'd' then the motor will deaccelerate and if we send 's' then the motor will stop spinning.

#include <AFMotor.h>

AF_DCMotor motor(1);

void setup(){
  mstop();
  Serial.begin(9600);
}

void loop(){
  if(Serial.available()){
      char cmd = Serial.read();
      switch(cmd){
        case 'f':
          forward();
          break;

        case 'b':
          backward();
          break;

        case 'a':
          accelerate();
          break;

        case 'd':
          deaccelerate();
          break;

        case 's':
          mstop();
          break;

        default:
          break;
      }
    }
}

void forward(){
  motor.setSpeed(200);
  motor.run(FORWARD);
  }

void backward(){
  motor.setSpeed(200);
  motor.run(BACKWARD);
  }

void accelerate(){
  motor.run(FORWARD); 

    uint8_t i;

    for (i=0; i<255; i++){
    motor.setSpeed(i);  
    delay(10);
    }
}

void deaccelerate(){
  motor.run(FORWARD); 

  uint8_t i;
  for(i=255; i!= 0; i--){
    motor.setSpeed(i);  
    delay(10);
    }
}

void mstop(){
   motor.run(RELEASE);
  } 

In conclusion, controlling a DC motor using an L 293D motor shield and an Arduino is a straightforward and convenient way to control motors in a variety of applications. With just a few simple components and a little programming, we can create sophisticated control systems that use DC motors. Whether you are building a robotic system or automating a process, the L293D motor shield and the Arduino are powerful tools that can help you achieve your goals.

In the above code we have used motor shield library, for writing program for motor shield without library see the tutorial DC motor Control using Arduino USART serial communication.

References:

2 Comments

Previous Post Next Post