motorShield_v3

Sign in or create your account | Project List | Help

Example-DC-motor

controlling speed and direction of a small DC motor

Introduction

This example shows how to control a small DC motor using the motor shield v3 for Arduino compatible boards.

Scenario

The scenario of use is as shown on this picture:

motor shield with DC motor

(c) 2009 Picture courtesy of Rui Costa

Code

The following sketch for Arduino shows how to control the speed and direction of the motor connected as shown in the previous picture.

int dirA = 12;
int dirB = 13;  // not used in this example
int speedA = 10;
int speedB = 11; // not used in this example

void setup() { pinMode (dirA, OUTPUT); pinMode (dirB, OUTPUT); pinMode (speedA, OUTPUT); pinMode (speedB, OUTPUT); }

void loop() { // move the motor A to one direction increasing speed digitalWrite (dirA, HIGH); for (int j = 0; j < 255; j += 10) { analogWrite (speedA, j); delay (100); }

delay(1000); // keep the motor rolling for one second

// move the motor A to one direction decreasing speed digitalWrite (dirA, HIGH); for (int j = 255; j >= 0; j -= 10) { analogWrite (speedA, j); delay (100); }

// stop the motor digitalWrite(speedA, LOW);

delay(1000); // keep the motor stopped for one second

}

Created: 8 years 6 months ago
by David Cuartielles

Labels:
Other:Featured

Commissioned by BLUSHINGBOY