การควบค ม 2 servo motor ก บ l293d board code

If you intend to build your own robot, you’ll need to control various motors like DC motors, stepper motors, and servos, and there’s no better option than the L293D Motor Driver Shield. It is capable of controlling all of these motors; there is no need for additional modules.

The L293D motor driver shield has all the bells and whistles required for a wide variety of low- to moderate-complexity projects. It can control:

การควบค ม 2 servo motor ก บ l293d board code

Hardware Overview

Driver Chipset

The brains of the shield are two L293D motor drivers and a 74HC595 shift register.

การควบค ม 2 servo motor ก บ l293d board code

The L293D is a dual-channel H-Bridge motor driver that can control two DC motors or a single stepper motor. Because the shield includes two such motor drivers, it can control up to four DC motors or two stepper motors.

The 74HC595 shift register, on the other hand, extends the Arduino’s four digital pins to the eight direction control pins of two L293D chips.

Motor Power Connections

The shield supports a motor voltage range of 4.5 to 25 volts. This power can be shared with the Arduino or used separately. In order to choose between the two, a special jumper labeled PWR is provided near the two-terminal power connector.

การควบค ม 2 servo motor ก บ l293d board code

When the jumper is in place, power is supplied to the motors via the Arduino DC power jack. In this case, the motors and Arduino are not physically isolated from each other. This method makes the shield easier to use because it requires only one power supply; however, you can use this method only when the motor supply voltage is less than 12V.

When the jumper is removed, the motor power is disconnected from the Arduino, allowing the motors to be physically isolated from the Arduino. In this case, however, you must provide a separate motor power supply to the two-terminal power connector labeled EXT_PWR.

Warning:

Applying power to the EXT_PWR two-terminal power connector while the jumper is in place will short the two power supplies together, potentially damaging the motor shield as well as the Arduino!

DC Motor Connections

The output channels of both L293D ICs are broken out to the edge of the shield with two 5-pin screw terminals labeled M1, M2, M3, and M4. A total of four DC motors operating at 4.5 – 25V can be connected to these terminals.

การควบค ม 2 servo motor ก บ l293d board code

Each channel on the module can supply up to 600 mA (1.2A peak) current to the DC motor. The amount of current supplied to the motor, however, depends on the capacity of the motor power supply.

Stepper Motor Connections

You can also connect two stepper motors to the output terminals. One stepper motor is connected to M1-M2 and the other to M3-M4.

การควบค ม 2 servo motor ก บ l293d board code

If you have a 5-wire unipolar stepper motor, connect the center tap wire to the center ground terminal.

Servo Motor Connections

The shield brings out the 16-bit PWM output lines to two 3-pin headers, which you can use to connect two servo motors.

การควบค ม 2 servo motor ก บ l293d board code

Unfortunately, the servo motors are powered directly from the Arduino’s 5V supply, which is generally a bad idea. Doing so can cause the Arduino’s on-board 5V regulator to overheat and can also introduce electrical noise on the 5V supply. On the plus side, it has a 100uF capacitor on these power pins, which helps a little.

So, if you want to use this feature, only use small servos like the SG90.

Bonus Features

Among the shield’s perks are the following:

การควบค ม 2 servo motor ก บ l293d board code

The shield includes a pulldown resistor array to keep motors off during power-up.

The on-board LED indicates that the motor power supply is working properly. The motors will not run if it is not lit.

The RESET button is nothing but Arduino’s reset button. It has been brought to the top for easy access.

Six analog pins (A0 to A5), as well as 5V and ground connections, are provided in the bottom right corner. You can populate these with headers, making them useful for connecting various sensors.

Arduino to Shield Pin Connections

For DC and stepper motor control, the shield makes use of pins D3, D4, D5, D6, D7, D8, D11, and D12.

D9 and D10 are used to control the servo motors. D10 is connected to Servo 1, while D9 is connected to Servo 2.

Please note that the shield does not use the D2 or D13 pins.

Installing AFMotor Library

To communicate with the shield, we must first install the AFMotor.h library. This will let us control DC, stepper, and servo motors with simple commands.

To install the library, navigate to Sketch > Include Library > Manage Libraries… Wait for the Library Manager to download the libraries index and update the list of installed libraries.

การควบค ม 2 servo motor ก บ l293d board code

Filter your search by entering ‘motor shield’. Look for Adafruit Motor Shield library(V1 Firmware) by Adafruit. Click on that entry and then choose Install.

การควบค ม 2 servo motor ก บ l293d board code

Experiment 1 – Driving DC Motors with L293D Shield

Now that we’ve learned everything there is to know about the shield, we can start connecting it to the Arduino!

Wiring

Begin by mounting the motor shield on top of an Arduino.

Next, connect the motor power supply. In our experiment, we are using DC gearbox motors, also called “TT” motors, which are often found in two-wheel-drive robots. They are rated for 3 to 12V. So, we will connect the external 9V power supply to the EXT_PWR terminal.

Finally, connect the motor to one of the M1, M2, M3, or M4 terminals. We are connecting it to M4.

การควบค ม 2 servo motor ก บ l293d board code

Arduino Code

The sketch below will show you how to control the speed and spinning direction of a DC motor using the L293D motor driver shield and can serve as the basis for more practical experiments and projects.

The sketch accelerates the DC motor in one direction before slowing down to stop. After one revolution, the motor reverses its spinning direction and repeats the process.


# include <AFMotor.h>
AF_DCMotor motor(4);
void setup() 
{
  //Set initial speed of the motor & stop
  motor.setSpeed(200);
  motor.run(RELEASE);
}
void loop() 
{
  uint8_t i;
  // Turn on motor
  motor.run(FORWARD);
  // Accelerate from zero to maximum speed
  for (i=0; i<255; i++) 
  {
    motor.setSpeed(i);  
    delay(10);
  }
  // Decelerate from maximum speed to zero
  for (i=255; i!=0; i--) 
  {
    motor.setSpeed(i);  
    delay(10);
  }
  // Now change motor direction
  motor.run(BACKWARD);
  // Accelerate from zero to maximum speed
  for (i=0; i<255; i++) 
  {
    motor.setSpeed(i);  
    delay(10);
  }
  // Decelerate from maximum speed to zero
  for (i=255; i!=0; i--) 
  {
    motor.setSpeed(i);  
    delay(10);
  }
  // Now turn off motor
  motor.run(RELEASE);
  delay(1000);
}

Code Explanation:

The sketch begins by including the AFMotor.h library.

The second line AF_DCMotor motor(motorPort#) creates a library object. Here, you must specify the motor port number to which the motor is connected. Write 1 for port M1, 2 for port M2, and so on.

If you want to connect multiple motors to the shield, make a separate object for each motor. The following code snippet, for example, creates two AFmotor objects.

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

In the setup and loop sections of the code, we simply call the two functions listed below to control the speed and direction of a motor.

  • setSpeed(speed) function controls the motor’s speed. The speed ranges from 0 to 255, with 0 being off and 255 being full throttle. In the program, you can change the speed whenever you want.
  • run(cmd) function controls the motor’s spinning direction. The following are valid cmd values:
    • FORWARD – spins the motor forward.
    • BACKWARD – spins the motor backwards.
    • RELEASE – This stops the motor and is equivalent to setSpeed(0). Because the motor shield lacks dynamic braking, the motor may take some time to stop spinning.

Experiment 2 – Driving Stepper Motors with L293D Shield

Our next experiment will involve connecting a stepper motor to the L293D shield. Begin by mounting the motor shield on top of an Arduino.

Wiring for the 28BYJ-48 unipolar stepper

28BYJ-48 unipolar stepper motors are rated at 5V and provide 48 steps per revolution. So, connect an external 5V power supply to the EXT_PWR terminal.

Don’t forget to remove the PWR jumper.

Finally, connect the motor to the stepper motor terminals M1-M2 (port

1) or M3-M4 (port

2). We are connecting it to M3-M4.

การควบค ม 2 servo motor ก บ l293d board code
Wiring Unipolar Stepper Motor to L293D Motor Shield & Arduino

Wiring for the NEMA 17 bipolar stepper

NEMA 17 bipolar stepper motors are rated at 12V and provide 200 steps per revolution. So, connect an external 12V power supply to the EXT_PWR terminal.

Don’t forget to remove the PWR jumper.

Finally, connect the motor to the stepper motor terminals M1-M2 (port

1) or M3-M4 (port

2). We are connecting it to M3-M4.

การควบค ม 2 servo motor ก บ l293d board code
Wiring Bipolar Stepper Motor to L293D Motor Shield & Arduino

Arduino Code

The following sketch will show you how to control a unipolar or bipolar stepper motor with the L293D shield, and it is the same for both motors except for the

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

0 parameter.

Before running the sketch, modify this parameter to match the specifications of your motor. For example, set it to 200 for NEMA 17 and 48 for 28BYJ-48.


# include <AFMotor.h>
// Number of steps per output rotation
// Change this as per your motor's specification
const int stepsPerRevolution = 48;
// connect motor to port 
# 2 (M3 and M4)
AF_Stepper motor(stepsPerRevolution, 2);
void setup() {
  Serial.begin(9600);
  Serial.println("Stepper test!");
  motor.setSpeed(10);  // 10 rpm   
}
void loop() {
  Serial.println("Single coil steps");
  motor.step(100, FORWARD, SINGLE); 
  motor.step(100, BACKWARD, SINGLE); 
  Serial.println("Double coil steps");
  motor.step(100, FORWARD, DOUBLE); 
  motor.step(100, BACKWARD, DOUBLE);
  Serial.println("Interleave coil steps");
  motor.step(100, FORWARD, INTERLEAVE); 
  motor.step(100, BACKWARD, INTERLEAVE); 
  Serial.println("Micrsostep steps");
  motor.step(100, FORWARD, MICROSTEP); 
  motor.step(100, BACKWARD, MICROSTEP); 
}

Code Explanation:

The sketch begins by including the AFMotor.h library.

The second line

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

1 creates a library object. Here, you must specify the motor’s steps-per-revolution and the port number to which the motor is connected.

In the setup and loop sections of the code, we simply call the two functions listed below to control the speed and direction of a motor.

  • AF_DCMotor motor1(1); AF_DCMotor motor2(2);

    2 sets the motor’s speed, where

    AF_DCMotor motor1(1); AF_DCMotor motor2(2);

    3 is the number of revolutions per minute you want the stepper to turn.
  • AF_DCMotor motor1(1); AF_DCMotor motor2(2);

    4 function is called every time you want the motor to move.

    AF_DCMotor motor1(1); AF_DCMotor motor2(2);

    5 is the number of steps you want it to take. The

    AF_DCMotor motor1(1); AF_DCMotor motor2(2);

    6 can be FORWARD or BACKWARD, and the

    AF_DCMotor motor1(1); AF_DCMotor motor2(2);

    7 can be any of the following:

    • SINGLE – One coil is energized at a time.
    • DOUBLE – Two coils are energized at a time for more torque.
    • INTERLEAVE – Alternate between single and double to create a half-step in between. This can result in smoother operation, but the speed is reduced by half due to the extra half-step.
    • MICROSTEP – Between each full step, adjacent coils are ramped up and down to create a number of micro-steps. This produces finer resolution and smoother rotation, but at the expense of torque.

Experiment 3 – Driving Servo Motors with L293D Shield

Our final experiment will involve driving a servo motor with the L293D shield.

Wiring

The shield brings out the 16-bit PWM output pins D9 and D10 to two 3-pin headers, which you can use to connect two servo motors. Keep in mind that D10 is connected to Servo 1, whereas D9 is connected to Servo 2.

The servo motors are powered directly from the Arduino’s 5V supply, so there is no need to connect anything to the EXT_PWR terminal.

การควบค ม 2 servo motor ก บ l293d board code
Wiring Servo Motor to L293D Motor Shield & Arduino

Arduino Code

This sketch is based on the standard sweep sketch found in the Arduino Examples under servo. It sweeps the shaft of a servo motor back and forth across 180 degrees.