Using DC Motors | Adafruit Motor Shield V2

Skip to main content Adafruit Motor Shield V2 Using DC Motors
  • 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

Using DC Motors

adafruit_products_dcmotortest.jpg

DC motors are used for all sort of robotic projects.The motor shield can drive up to 4 DC motors bi-directionally. That means they can be driven forwards and backwards. The speed can also be varied at 0.5% increments using the high-quality built in PWM. This means the speed is very smooth and won't vary!Note that the H-bridge chip is not meant for driving continuous loads of 1.2A, so this is for small motors. Check the datasheet for information about the motor to verify its OK!

Connecting DC Motors

To connect a motor, simply solder two wires to the terminals and then connect them to either the M1, M2, M3, or M4. Then follow these steps in your sketch

Include the required libraries

Make sure you #include the required libraries

Download File Copy Code #include <Wire.h> #include <Adafruit_MotorShield.h> #include "utility/Adafruit_MS_PWMServoDriver.h" #include <Wire.h> #include <Adafruit_MotorShield.h> #include "utility/Adafruit_MS_PWMServoDriver.h"

Create the Adafruit_MotorShield object

Download File Copy Code Adafruit_MotorShield AFMS = Adafruit_MotorShield(); Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Create the DC motor object

Request the DC motor from the Adafruit_MotorShield:

Download File Copy Code Adafruit_DCMotor *myMotor = AFMS.getMotor(1); Adafruit_DCMotor *myMotor = AFMS.getMotor(1);

with getMotor(port#). Port# is which port it is connected to. If you're using M1 its 1, M2 use 2, M3 use 3 and M4 use 4

Connect to the Controller

In your setup() function, call begin() on the Adafruit_MotorShield object:

Download File Copy Code AFMS.begin(); AFMS.begin();

Set default speed

Set the speed of the motor using setSpeed(speed) where the speed ranges from 0 (stopped) to 255 (full speed). You can set the speed whenever you want.

Download File Copy Code myMotor->setSpeed(150); myMotor->setSpeed(150);

Run the motor

To run the motor, call run(direction) where direction is FORWARD, BACKWARD or RELEASE. Of course, the Arduino doesn't actually know if the motor is 'forward' or 'backward', so if you want to change which way it thinks is forward, simply swap the two wires from the motor to the shield.

Download File Copy Code myMotor->run(FORWARD); myMotor->run(FORWARD);

Page last edited July 09, 2013

Text editor powered by tinymce.

Using RC Servos Using Stepper Motors Related Guides WiFi Controlled Mobile Robot By M. Schwartz beginner All About Stepper Motors By Bill Earl beginner Improve Brushed DC Motor Performance By Jan Goolsbey intermediate Bluetooth Controlled Motorized Camera Slider By Ruiz Brothers beginner CircuitPython Hardware: PCA9685 DC Motor & Stepper... By Tony DiCola intermediate Adafruit Motor Selection Guide By Bill Earl beginner Improve the Low Speed of Brushed DC Motors By Jan Goolsbey intermediate 3D Printed Animatronic Robot Head By Rick Winscot beginner 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 Motorized Camera Slider MK3 By Ruiz Brothers intermediate Adafruit FunHouse By M. LeBlanc-Williams beginner Adafruit H3LIS331 and LIS331 High-g 3-Axis... By Bryan Siepert beginner Your browser does not support the video tag. This links to the guide Adafruit 1.3" Color TFT Bonnet for Raspberry Pi. Adafruit 1.3" Color TFT Bonnet for Raspberry Pi By Kattni Rembor beginner Adafruit PiUART - USB Console and Power Add-on for... By lady ada beginner Adafruit PiCowbell Adalogger for Pico By Liz Clark beginner
Create Wishlist
× Title Description Close Search Search
Categories

Tag » Arduino Motor Shield 4 Dc Motors Code