Library Reference | Adafruit Motor Shield V2

Skip to main content Adafruit Motor Shield V2 Library Reference
  • Overview
  • FAQ
  • Install Headers & Terminals
  • Install Software
    • Library Reference
    • Arduino Library Docs
  • Powering Motors
  • Using RC Servos
  • Using DC Motors
  • Using Stepper Motors
  • Python & CircuitPython
    • Python Docs
  • Stacking Shields
  • Resources
  • Single page
  • Feedback? Corrections?
  • Text View
Featured Products view all
  • Angled shot of a Adafruit Motor/Stepper/Servo Shield for Arduino v2 Kit connected to several different steppers and servos. Adafruit Motor/Stepper/Servo Shield for Arduino v2 Kit $19.95 Add to Cart
  • Lit Shot of the Motor party add-on pack for Arduino. Motor party add-on pack for Arduino $39.95 Add to Cart
  • Angled shot of a Stepper motor - NEMA-17. Stepper motor - NEMA-17 size - 200 steps/rev, 12V 350mA $14.00 Add to Cart
  • Angled shot of small Reduction Stepper Motor Small Reduction Stepper Motor - 5VDC 32-Step 1/16 Gearing $4.95 Add to Cart
  • Angled shot of a Small Reduction Stepper Motor. Small Reduction Stepper Motor - 12VDC 32-Step 1/16 Gearing $4.95 Add to Cart
  • DC Toy Hobby Motor DC Toy / Hobby Motor - 130 Size $1.95 Add to Cart
  • Continuous Rotation Servo with three pin cable Continuous Rotation Servo $11.95 Add to Cart
  • Standard hobby servo with three pin cable Standard servo - TowerPro SG-5010 $12.00 Add to Cart
207 Intermediate Project guide 🤖 5

Library Reference

adafruit_products_1438top_LRG.jpg adafruit_products_OVERHEAD_ALL.jpg class Adafruit_MotorShield;The Adafruit_MotorShield class represents a motor shield and must be instantiated before any DCMotors or StepperMotors can be used. You will need to declare one Adafruit_MotorShield for each shield in your system.

Adafruit_MotorShield(uint8_t addr = 0x60);

The constructor takes one optional parameter to specify the i2c address of the shield. The default address of the constructor (0x60) matches the default address of the boards as shipped. If you have more than one shield in your system, each shield must have a unique address.

void begin(uint16_t freq = 1600);

begin() must be called in setup() to initialize the shield. An optional frequency parameter can be used to specify something other than the default maximum: 1.6KHz PWM frequency.

Adafruit_DCMotor *getMotor(uint8_t n);

This function returns one of 4 pre-defined DC motor objects controlled by the shield. The parameter specifies the associated motor channel: 1-4.

Adafruit_StepperMotor *getStepper(uint16_t steps, uint8_t n);

This function returns one of 2 pre-defined stepper motor objects controlled by the shield. The first parameter specifies the number of steps per revolution.The second parameter specifies the associated stepper channel: 1-2.

void setPWM(uint8_t pin, uint16_t val);void setPin(uint8_t pin, boolean val);

These are low-level functions to control pins on the on-board PWM driver chip. These functions are intended for internal use only. adafruit_products_ID711_LRG.jpg class Adafruit_DCMotorThe Adafruit_DCMotor class represents a DC motor attached to the shield. You must declare an Adafruit_DCMotor for each motor in your system.

Adafruit_DCMotor(void);

The constructor takes no arguments. The motor object is typically initialized by assigning a motor object retrieved from the shield class as below: Download File Copy Code // Create the motor shield object with the default I2C address Adafruit_MotorShield AFMS = Adafruit_MotorShield(); // Select which 'port' M1, M2, M3 or M4. In this case, M1 Adafruit_DCMotor *myMotor = AFMS.getMotor(1); // You can also make another motor on port M2 Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2); // Create the motor shield object with the default I2C address Adafruit_MotorShield AFMS = Adafruit_MotorShield(); // Select which 'port' M1, M2, M3 or M4. In this case, M1 Adafruit_DCMotor *myMotor = AFMS.getMotor(1); // You can also make another motor on port M2 Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

void run(uint8_t);

The run() function controls the motor state. The parameter can have one of 3 values:

  • FORWARD - Rotate in a forward direction
  • BACKWARD - Rotate in the reverse direction
  • RELEASE - Stop rotation

Note that the "FORWARD" and "BACKWARD" directions are arbitrary. If they do not match the actual direction of your vehicle or robot, simple swap the motor leads.Also note that "RELEASE" simply cuts power to the motor. It does not apply any braking.

void setSpeed(uint8_t);

The setSpeed() function controls the power level delivered to the motor. The speed parameter is a value between 0 and 255.Note that setSpeed just controls the power delivered to the motor. The actual speed of the motor will depend on several factors, including: The motor, the power supply and the load.

adafruit_products_12vstepper_LRG.jpg class Adafruit_StepperMotorThe Adafruit_StepperMotor class represents a stepper motor attached to the shield. You must declare an Adafruit_StepperMotor for each stepper motor in your system.

Adafruit_StepperMotor(void);

The constructor takes no arguments. The stepper motor is typically initialized by assigning a stepper object retrieved from the shield as below: Download File Copy Code // Create the motor shield object with the default I2C address Adafruit_MotorShield AFMS = Adafruit_MotorShield(); // Connect a stepper motor with 200 steps per revolution (1.8 degree) // to motor port #2 (M3 and M4) Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2); // Create the motor shield object with the default I2C address Adafruit_MotorShield AFMS = Adafruit_MotorShield(); // Connect a stepper motor with 200 steps per revolution (1.8 degree) // to motor port #2 (M3 and M4) Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

void step(uint16_t steps, uint8_t dir, uint8_t style = SINGLE);

The step() function controls stepper motion.
  • The first parameter specifies how many steps to move.
  • The second parameter specifies the direction: FORWARD or BACKWARD
  • The last parameter specifies the stepping style: SINGLE, DOUBLE, INTERLEAVED or MICROSTEP
The ste() function is synchronous and does not return until all steps are complete. When complete the motor remains powered to apply "holding torque" to maintain position.

void setSpeed(uint16_t);

The setSpeed() function controls the speed of the stepper motor rotation. Speed is specified in RPM.

uint8_t onestep(uint8_t dir, uint8_t style);

The oneStep() function is a low-level internal function called by step(). But it can be useful to call on its own to implement more advanced functions such as acceleration or coordinating simultaneous movement of multiple stepper motors. The direction and style parameters are the same as for step(), but onestep() steps exactly once.Note: Calling step() with a step count of 1 is not the same as calling onestep(). The step function has a delay based on the speed set in setSpeed(). onestep() has no delay.

void release(void);

The release() function removes all power from the motor. Call this function to reduce power requirements if holding torque is not required to maintain position.

Page last edited August 16, 2013

Text editor powered by tinymce.

Install Software Powering Motors Related Guides Your browser does not support the video tag. This links to the guide CurieBot: Arduino 101 Mini Robot Rover. CurieBot: Arduino 101 Mini Robot Rover By John Park beginner Improve the Low Speed of Brushed DC Motors By Jan Goolsbey intermediate Improve Brushed DC Motor Performance By Jan Goolsbey intermediate All About Stepper Motors By Bill Earl beginner Adafruit Motor Selection Guide By Bill Earl beginner CircuitPython Hardware: PCA9685 DC Motor & Stepper... By Tony DiCola intermediate Motorized Camera Slider MK3 By Ruiz Brothers intermediate 3D Printed Animatronic Robot Head By Rick Winscot beginner Bluetooth Controlled Motorized Camera Slider By Ruiz Brothers beginner WiFi Controlled Mobile Robot By M. Schwartz beginner Adafruit SPI FRAM Breakouts By lady ada beginner Adafruit TRRS Jack Breakout Board By Kattni Rembor beginner Adafruit MAX31865 RTD PT100 or PT1000 Amplifier By lady ada intermediate Adafruit 1.14" 240x135 Color Newxie TFT Display By Liz Clark beginner Adafruit QT Py RP2040 By Kattni Rembor beginner
Create Wishlist
× Title Description Close Search Search
Categories

Tag » Arduino Adafruit Motor Shield V2 Library