Drive DC motor using Arduino motor shield

This is a continue of previous post where we have tried to run a servo using arduino motor shield. This was simple task to do with Arduino Servo library. Today we are going to push things a bit forward and drive DC motor using same motor shield. This motor shield is capable of running small DC motors that require less than 0.6A of current and operating voltage us less than 25V. In my drawer I found small 12V motor which will fit for this demo. Before we begin programming, we need to connect motor to the Board. We are going to use M1 connector.:

arduino motor shield with dc motor

Since motor requires 12V power supply we are going to use external power supply. It can be connected to External power screw terminal. Be sure to remove jumper as well.

arduino motor shield external power supply

There is NO reverse polarity protection, so be sure you connect it right otherwise things can get hot or damaged. If you get LED shining then you are done and it is safe to connect USB cable to Arduino.

Driving DC and stepper motor becomes more complicated, because there is a serial in parallel out shift register 74hct595n used for setting enable signals on L293D:

L293

74hct595n shift register in arduino motor shield

We can try running using standard Arduino commands. Bu why bother if there is already a great library to drive motors using few commands. First of all download AFMotor library from github. Unpack it and copy AFmotor folder to Arduino->libraries folder so that AFmotor.cpp and AFmotor.h files were in top level of folder just like this:

arduino library folder structure

After your library is in place just restart Arduino IDE and then go to File->Examples->AFmotor->MotorTest. And you should be loaded with a simple example program which can be used to test DC motor.

loading arduino motor example

 Here is test program. I just removed serial messages for more compact view:

#include <AFMotor.h>
AF_DCMotor motor(1);
void setup() {
  // turn on motor
  motor.setSpeed(200);
  motor.run(RELEASE);
}
void loop() {
  uint8_t i;
  motor.run(FORWARD);
  for (i=0; i<255; i++) {
    motor.setSpeed(i);  
    delay(50);
 }
  for (i=255; i!=0; i--) {
    motor.setSpeed(i);  
    delay(10);
 }
  motor.run(BACKWARD);
  for (i=0; i<255; i++) {
    motor.setSpeed(i);  
    delay(10);
 }
  for (i=255; i!=0; i--) {
    motor.setSpeed(i);  
    delay(50);
 }
  motor.run(RELEASE);
  delay(1000);
}

As you can see Arduino program includes our new library with command

#include <AFMotor.h>

Then we make sure to indicate where motor is connected. Since we connected motor to M1 screw terminal we create motor object as follows:

AF_DCMotor motor(1);

Then in setup we just show initial motor speed in range 0 – 255, where 0 means motor is stopped and 255 is max speed. As motor speed is controlled with PWM then speed determines PWM duty cycle. Then follows run() command with one if three parameters:

  • RELEASE – motor is stopped;
  • FORWARD – motor is driven in one direction;
  • BACKWARD – motor spins in other direction.

Other part of program is self explanatory. It simply drives backwards and forward gradually speeding and slowing. As you can see driving DC motors is also easy task. Arduino motor shield allows driving up to four DC motors simultaneously in both directions. By tweaking AFmotor library a bit you can practically drive 8 DC motors single direction where one motor pin is connected to control signal and another to ground.

There is one thing left to try – stepper motor. These motors are more complex but once you get them up and running you get precise rotation control. Next time we will give it a try.

2 Comments:

  1. Pingback: How to drive stepper motor with Arduino motor shield - Do It Easy With ScienceProg

  2. Thanks for this great tutorial. I got my light following robot working now.

Leave a Reply

Your email address will not be published. Required fields are marked *