Controlling a DC Motor with Motor Shield Rev3

Learn how to connect a DC motor to the shield, and how to control the speed and direction of the motor.


In this tutorial, we will learn how to control a DC motor, using the Motor Shield Rev3, a shield compatible with the Arduino UNO. We will take a look at three different pins: brake, pwm & direction, where we will create a simple sketch that uses all three of them. In addition we will also take a look at how we can power our project, using an external power source.


The goals of this project are:

  • Set up your Motor Shield Rev3 to control a DC motor.
  • Control the brake, pwm and direction of the motor.
  • How to connect an external power source to the shield.

Hardware & Software Needed

Controlling a DC Motor

There are several ways we can control a DC motor, perhaps the easiest one is just by applying power to it. Very early inventions using the DC motor simply worked like that: add a power source and the motor will start rotating, switch the polarity and you switch the direction.

But if we want to do a bit more than just making a motor spin full speed in two directions, we need a motor control circuit. More specifically, the dual full-bridge driver L298P, which we can find on the Motor Shield Rev3.

With this IC, we can set the work duty (0-100), enable brakes (HIGH, or LOW), and set the direction (HIGH or LOW). Each of these features can be controlled using a different set of pins. As we are going to control a DC motor in this tutorial, let's take a look at the pins that are used:

Channel A:

  • D12 - Direction
  • D3 - PWM (work duty)
  • D9 - Brake
  • A0 - current sensing.

Channel B:

  • D13 - Direction
  • D11 - PWM (work duty)
  • D8 - Brake
  • A1 - current sensing.

Now if we were to use just the bare chip, it would involve a bit more complex circuitry. Fortunately, all we have to worry about is connecting the DC motor to one of the channels, connect an external power source, and we are good to go!


Let's begin by mounting our Arduino Motor Shield Rev3 on top of an Arduino UNO.

Mounting the shield.
Mounting the shield.

Now, let's connect the motor to the A channel, following the image below. The channels are marked next to the screw terminals on the shield.

Connecting a power source and DC motor.
Connecting a power source and DC motor.

Finally, we can connect the USB cable to the computer.

Programming the Board

We will now get to the programming part of this tutorial.

First, let's take a look at some key commands in the code. We are actually not using a library, as the operation is very basic.

  • int directionPin = 12;
    - assign direction pin.
  • int pwmPin = 3;
    - assign PWM (work duty) pin.
  • int brakePin = 9;
    - assign brake pin.
  • digitalWrite(directionPin, state)
    - sets the direction of the pin by using HIGH or LOW states.
  • digitalWrite(brakePin, state)
    - release or activate brakes, using HIGH or LOW states.
  • analogWrite(pwmPin, 30)
    - write a value between 0-100 to set the work duty.
  • directionState = !directionState
    - a boolean that switches every time the loop is run.

The sketch can be found in the snippet below. Upload the sketch to the board.

1int directionPin = 12;
2int pwmPin = 3;
3int brakePin = 9;
5//uncomment if using channel B, and remove above definitions
6//int directionPin = 13;
7//int pwmPin = 11;
8//int brakePin = 8;
10//boolean to switch direction
11bool directionState;
13void setup() {
15//define pins
16pinMode(directionPin, OUTPUT);
17pinMode(pwmPin, OUTPUT);
18pinMode(brakePin, OUTPUT);
22void loop() {
24//change direction every loop()
25directionState = !directionState;
27//write a low state to the direction pin (13)
28if(directionState == false){
29 digitalWrite(directionPin, LOW);
32//write a high state to the direction pin (13)
34 digitalWrite(directionPin, HIGH);
37//release breaks
38digitalWrite(brakePin, LOW);
40//set work duty for the motor
41analogWrite(pwmPin, 30);
45//activate breaks
46digitalWrite(brakePin, HIGH);
48//set work duty for the motor to 0 (off)
49analogWrite(pwmPin, 0);

Testing It Out

After we have uploaded the code, the program will start running immediately. If everything is working correctly, the motor should start spinning as soon as the program finishes uploading.

The expected outcome is that the motor spins in one direction for 2 seconds, with the work duty set to

(quite low), and brakes disengaged.

Switching the direction.
Switching the direction.

When 2 seconds have passed, the brakes are activated and work duty is set to 0. This means the motor comes to a full stop.

Motor at full stop.
Motor at full stop.


If the code is not working, there are some common issues we can troubleshoot:

  • We have not connected the circuit properly. We can double check this by going back to the circuit at the top of this page.
  • We have connected the motor to the wrong channel. Remember that the pin definitions in the code need to match the channel you connect it to.
  • The external power source is not working: double check that your batteries are working, and re-charge them if needed.


In this tutorial, we have learned how to control a DC motor using the Motor Shield Rev3. With the brake, pwm and direction, you now have a lot more options when it comes to motor control. Having this type of control allows you to create many different cool projects, without having to create complicated programs or advanced circuits!

Suggested changes

The content on is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.


The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.