Testing Arduino motor shield with servo motor

Recently I’ve got Arduino motor shield. It is based on ladyada original mshield circuit. It uses two popular L293D quadruple half-H divers. It is cheap and reliable shield to drive various motors. These can be 2 hobby servo motors, 4 bidirectional DC motors or 2 (unipolar or bipolar) stepper motors. Load current is limited to L293D chips. Specification says that each channel can provide constant 0.6A and peak 1.2A. There is also a thermal shutdown to prevent circuit from damaging. Motors can be externally powered using voltage range from 4.5V to 36V. Each motor control channel is pulled down with resistor to disable any motor at power up.

Arduino motor shield

In this post we are going to try servo motor control There are couple connectors on motor shield where you can connect two servo motors using standard 3 wire connector (GND, VCC and PWM).

arduino motor shield with servo connected

There are two options in powering motor. You can power it from Arduino supply. This convenient when motor is low power and Arduino supplied current is enough to drive the load. If you use higher power motor, then you should attach external power to terminal and remove jumper that connects motor power to Arduino:

arduino motor shield internal power jumper   arduino motor shield external power jumper

Servo motor is controlled using PWM signal. Rotation angle can be changed by changing PWM duty cycle. Usually PWM frequency is 50Hz (period 20ms). Now what about rotation angle and pulse width.? Normally there are following scenarios:

  • 0º angle – 1ms pulse width;

  • 90º is neutral – 1.5ms pulse width;

  • 180º maximum angle – 2ms.

As you can see pulse width vary in small range comparing to PWM period. Lets try to run simple Arduino program to get motor shaft turning arduino motor shield Servo1 channel is attached to Arduino D10 pin. Controlling servo motors with Arduino is really easy, and you even don’t need a motor shield for this. Anyway motor shield is convenient because there are already pins available where standard servo connector fits. And so to drive servo you only need to include servo.h library in to Arduino code:

#include <Servo.h> 
//create servo object
Servo servo1;
void setup() {
  //attach servo1 to pin 10
  servo1.attach(10); 
}
void loop() {
while(1){
  //0 position
  servo1.write(0);
  delay(1000);  
  //90 degrees
  servo1.write(90);
    delay(1000);
 //180 degrees
  servo1.write(180);
  delay(1000);    
 //180 degrees
  servo1.write(90);
  delay(1000);        
  }
}

As you can see first we create a servo1 object from class Servo.

Servo servo1;

Then we assign pin 10 to it where servo motor is connected.

Servo1.attach(10);

And that’s it – we can start using servo motor right away by using command:

servo1.write(angle);

Servo library allows to set servo motor rotation angle in degrees from 0 to 180. In this example program we simply rotate servo motor 90º backwards and forward every second.

There are other functions available in servo library that can be used. We used couple of those – attach() and write(). There is also detach() command that allows to free pin from servo control and use it as normal pin like for using analogWrite(). If you don’t like to control servo motor by writing angular values you can use writeMicroseconds() function instead of write(). It is easy to convert angle to microseconds. If we know that 0º angle is 1ms or 1000us. Then instead of writing servo1.write(0) we can write servo1.writeMicroseconds(1000) and this will be same. Also this command is useful when you get a bit different servo motor, where signal timing won’t match common practice. Then you can set custom pulse length to match the angle. The last command in servo library is read(). It simply returns last angle sent with write() command. As we can see controlling servo motors with Arduino motor shield is easy. And you don’t need any external libraries for this. Next time we will try to run DC motor and stepper motor.

4 Comments:

  1. Pingback: Drive DC motor using Arduino motor shield - Do It Easy With ScienceProg

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

  3. Pingback: Demo tests with Arduino motor shield | Embedded projects from around the web

  4. Hi there ,
    Its great article that you have written. Its really helpful.
    I am doing a final year project in which I m using this motor shield a motor with higher power and I am not able to understand which jumper are you talking about in these lines.

    “If you use higher power motor, then you should attach external power to terminal and remove jumper that connects motor power to Arduino”

    Please help me with this. I shall be grateful to you.
    Thankyou

Leave a Reply

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