Source: https://mokxi.com/libraries/adafruit-motorshield
Updated: 2026-11-02

Libraries / Motion
Arduino library · Motion

# Adafruit Motor Shield V2: four DC motors or two steppers over I2C

The Adafruit Motor Shield V2 drives up to four DC motors or two steppers, and talks to the Arduino over I2C rather than with a pin per motor input. On the board a PCA9685 PWM chip at address 0x60 makes every speed and direction signal, and two TB6612 dual H bridges turn them into motor current. That leaves almost all of the Arduino’s pins free.

Open the example in the editor See the calls

Built in on Mokxi: #include <Adafruit_MotorShield.h> and it builds, with nothing to install. Free, and no account needed.

## What Adafruit Motor Shield V2 Library does

The library hands you motor objects. getMotor(1) to getMotor(4) return a DC motor on ports M1 to M4; getStepper(steps, 1) or getStepper(steps, 2) return a stepper on M1 and M2 or M3 and M4. You set speed from 0 to 255 and call run() with FORWARD, BACKWARD or RELEASE.

This is the library for the V2 shield only. The older V1 shield and its L293D clones are driven by the AFMotor library, with a different set of calls, and a sketch for one will not work on the other.

Library Manager name | Adafruit Motor Shield V2 Library

Author | Adafruit

Include | #include <Adafruit_MotorShield.h>

Boards | every board Mokxi compiles for but ATtiny85

In the simulator | Simulated: it runs against the part the way it does on the bench.

Parts it drives | Servo driver, PCA9685 16-channel, Motor driver, TB6612FNG, DC motor

## Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <Adafruit_MotorShield.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Adafruit Motor Shield V2 Library" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit Motor Shield V2 Library to lib_deps in PlatformIO. Mokxi follows the calls of release 1.1.3, so a sketch written here builds there too.

What to know in the simulator: Build the shield from its parts: a PCA9685 at address 96 (0x60) whose channels drive TB6612 inputs, as the header lists.

ESP32-C3 · Arduino Uno · Arduino Nano · Arduino Mega 2560 · Arduino Leonardo · SparkFun Pro Micro · Arduino Micro · ESP32-C6 · Raspberry Pi Pico · Raspberry Pi Pico W · Seeed XIAO SAMD21 · STM32 · STM32 Nucleo-F411RE · STM32 Blue Pill · micro:bit V2 · Arduino Uno R4 · ESP32 DevKit · ESP32-S3-DevKitC-1 · ESP8266 NodeMCU · Arduino Pro Mini 3.3 V · Seeed XIAO ESP32-C3 · ESP32-C3 SuperMini · Seeed XIAO ESP32-C6 · ESP32-DevKitC V4 · LOLIN32 Lite · Wemos D1 R32 · Cheap Yellow Display · TTGO T-Display · Seeed XIAO ESP32-S3 · Arduino Nano ESP32 · Arduino Nano RP2040 Connect · Seeed XIAO RP2040 · Waveshare RP2040-Zero · Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### Adafruit_MotorShield AFMS = Adafruit_MotorShield();

The shield at the default address 0x60. Stacked shields with their address jumpers set take Adafruit_MotorShield(0x61) and so on.

### AFMS.begin()

Starts I2C and the shield’s PWM chip at 1.6 kHz. Returns false when nothing answers at the address.

### Adafruit_DCMotor *myMotor = AFMS.getMotor(1);

A pointer to the DC motor on port M1. Every call on it uses ->, not a dot.

### myMotor->setSpeed(150)

Speed from 0 to 255. It sets the PWM duty and does not start the motor by itself.

### myMotor->run(FORWARD)

Starts the motor in one direction; BACKWARD reverses it and RELEASE lets it coast to a stop.

### Adafruit_StepperMotor *stepper = AFMS.getStepper(200, 2);

A stepper of 200 steps a turn on port 2 (M3 and M4).

### stepper->setSpeed(10)

The stepper speed in rpm, used by step().

### stepper->step(100, FORWARD, DOUBLE)

Moves 100 steps and blocks until done. SINGLE, DOUBLE, INTERLEAVE and MICROSTEP pick the coil pattern.

### stepper->release()

Turns both coils off so the stepper stops drawing current and can be turned by hand.

## Example: A DC motor on the V2 shield

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3

### Wiring

- Arduino Uno R3 A4 to Servo driver, PCA9685 16-channel SDA
- Arduino Uno R3 A5 to Servo driver, PCA9685 16-channel SCL
- Arduino Uno R3 5V to Servo driver, PCA9685 16-channel VCC
- Arduino Uno R3 GND to Servo driver, PCA9685 16-channel GND
- Arduino Uno R3 GND to Servo driver, PCA9685 16-channel OE
- Servo driver, PCA9685 16-channel PWM8 to Motor driver, TB6612FNG PWMA
- Servo driver, PCA9685 16-channel PWM10 to Motor driver, TB6612FNG AIN1
- Servo driver, PCA9685 16-channel PWM9 to Motor driver, TB6612FNG AIN2
- Arduino Uno R3 5V to Motor driver, TB6612FNG STBY
- Arduino Uno R3 5V to Motor driver, TB6612FNG VCC
- Arduino Uno R3 GND to Motor driver, TB6612FNG GND1
- Battery: AA, coin cell, LiPo V+ to Motor driver, TB6612FNG VM
- Battery: AA, coin cell, LiPo V- to Motor driver, TB6612FNG GND2
- Motor driver, TB6612FNG AO1 to DC motor 1
- Motor driver, TB6612FNG AO2 to DC motor 2

Open it in the editor, wired and ready to run

## Common errors and how to fix them

### Could not find Motor Shield. Check wiring.

begin() got no answer at 0x60. Make sure the shield is seated on the headers (SDA and SCL included) and that no address jumper is soldered; with one set, pass that address to the constructor.

### 'class Adafruit_DCMotor' has no member named or 'AF_DCMotor' was not declared

The sketch mixes the V1 library (AFMotor.h, AF_DCMotor) with the V2 one (Adafruit_MotorShield.h, Adafruit_DCMotor). Use the library that matches the shield in front of you and the calls from its own examples.

### The motor does not turn after setSpeed()

setSpeed() only sets the duty; the motor starts when run(FORWARD) or run(BACKWARD) is called. Also check that the motor supply is connected to the shield’s power terminal with the power jumper as the guide says.

## Keep going

### Learn

- Arduino DC Motor: Transistor Wiring and PWM Speed
- Arduino PCA9685 Servo Driver: Wiring and Code

### Parts

- Servo driver, PCA9685 16-channel
- Motor driver, TB6612FNG
- DC motor

### More motion libraries

- Servo
- Stepper
- AccelStepper
- L298N
- Adafruit PWM Servo Driver Library

Every built-in library · Open the editor

## Try Adafruit Motor Shield V2 Library without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
