Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

DRIVING DC MOTORS WITH L293D SHIELD

Now that we know everything about the shield, we can begin hooking it up
to our Arduino!

Start by plugging the shield on the top of the Arduino.

Next, connect power supply to the motors. Although you can connect DC
motors having voltages between 4.5 to 25V to the shield, in our experiment
we are using DC Motors that are rated for 9V. So, we will connect external
9V power supply to the EXT_PWR terminal.

Now, connect the motor to either M1, M2, M3 or M4 motor terminals. In our
experiment we are connecting it to M4.

CODE
#include <AFMotor.h>

AF_DCMotor motor(1);

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);
}

You might also like