User Tools

Site Tools


arduino:adafruit_motor_shield

Adafruit Motor Shield for Arduino

What is it?

An Arduino Shield for easily controlling motors (DC motors, steppers, servo and more). Check the FAQ for all the pins allocation, micro-stepping, servo support and more.

Documentation: http://www.ladyada.net/make/mshield/ and http://www.ladyada.net/make/mshield/faq.html

Download library: https://github.com/adafruit/Adafruit-Motor-Shield-library

If you need non-blocking multi-steppers, use AccelStepper https://github.com/adafruit/AccelStepper

Simple example

// Adafruit Motor shield library
// http://www.ladyada.net/make/mshield/faq.html
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!
 
#include <AFMotor.h>
#include <Servo.h> 
 
AF_DCMotor motor(4);
Servo myservo; 
 
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Adafruit Motor Shield");
  motor.run(RELEASE);
 
  myservo.attach(9);  // pins 9-10 on the Arduino to servo headers on the shield
  myservo.write(90); // middle position
}
 
void loop() {
 
  Serial.println("DC Motor Moving");
 
  // reduced speed
  Serial.println("Forward 127");
  motor.setSpeed(127);
  motor.run(FORWARD);
  delay(2000);
 
  // full speed
  Serial.println("Backward 255");
  motor.setSpeed(255);
  motor.run(BACKWARD);
  delay(1000);
 
 
  // ramp down
  Serial.println("Ramp down");
  motor.run(FORWARD);
  for (int i=255; i>0; i--) {
    motor.setSpeed(i);  
    delay(20);
 }
  Serial.println("DC Motor Released");
  motor.run(RELEASE);
 
  Serial.println("Servo Moving");
  for(int pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5);                       // waits 5ms for the servo to reach the position 
  } 
  for(int pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5);                       // waits 5ms for the servo to reach the position 
  } 
 
  delay(3000);
}

Controlling motors via serial communication

#include <Messenger.h> // ATTENTION: Messenger library version 1.5 only
#include <AFMotor.h>
#include <Servo.h>
 
// Instantiate Messenger object with the message function and the default separator 
// (the space character)
Messenger message = Messenger(); 
AF_DCMotor motor(1);  // dc motor attached to M1
Servo servo1;  // regular servo connected to bottom servo connector on AF Motor shield
 
// Define messenger function
void messageCompleted() {
  // This loop will echo each element of the message separately
  while ( message.available() ) {
 
    // checking what is the message
    // expects "on" or "off"
    /*
    if ( message.checkString("on") ) {
      motor.run(FORWARD);
    } else if ( message.checkString("off") ) {
      motor.run(RELEASE);
    }*/
 
    // expects a numeric value between 0 and 255
    int incomingValue = message.readInt();
    Serial.println(incomingValue);
    motor.setSpeed(incomingValue);
    motor.run(FORWARD);
    servo1.write(incomingValue); // 0 -255
 
  }
}
 
void setup() {
  // Initiate Serial Communication
  Serial.begin(115200); 
  message.attach(messageCompleted);
  motor.setSpeed(255); // 0 - 255
  //setup servo
  servo1.attach(9);  // pin 9 labelled servo_2
}
 
void loop() {
  // The following line is the most effective way of 
  // feeding the serial data to Messenger
  while ( Serial.available() ) message.process( Serial.read() );
 
}
arduino/adafruit_motor_shield.txt · Last modified: 2011/11/27 14:48 by camille