Objectives
- We keep on playing with direct current motors.
- Introduce the Adafruit Motor Shield V1 .
- We will see how to use it with the appropriate library.
- Assemble a little rover with 4×4 traction .
Bill of materials
Controlling several DC motors
In previous chapters we have seen how to handle typical DC motors. We have seen how to rotate them and how to change its speed of rotation, as well as how to reverse the direction of rotation using an integrated H-bridge like the L293D.
And what does a normal man do when he manages to control a motor (especially one who is reading this)? Of course, he tries to drive several motors at once, let’s say 4 of them, and starts to think about making a small rover with 4-wheel drive and wireless remote control and …. Calm down!
Let’s go by parts. At the moment we have only controlled one engine, although we know that the L293D has the ability to handle a second motor. We could design a circuit with two L293D to move 4 motors and maybe make a shield to assemble everything in the hump of our Arduino.
But as I always tell you, if there is a need in this electronic world, there is always someone ready to sell us the solution conveniently. And as you can imagine, we are not the first to have this idea (nor will we be the last) nor is it an orginal solution. The market offers a variety of shields to control very interesting motors, depending on the size of the engines, their consumption, voltage and anything else you can imagine.
As in this humble house we think that in order to learn we have to put our hands at work and preferably spending little money, we will present a simple motor shield that was designed by our friends of Adafruit and later abandoned in favor of a more advanced (and expensive) motor controller, whose relay has been taken over by the cloud of chinese manufacturers, providing us with an Arduino motor shield v1.0, for very little money, which allows us to start us spending the minimum.
Let’s see its features:
Adafruit Motor Shield V1.0
Here is a picture of the Motor Shield V1.0, which you can easily find at any supplier:
Its main features are:
- 4 H-Bridges built by using two L293D chips.
- Up to 4 DC motors with bidirectional control and 8 bit speed selection.
- 0,6 Amper maximum current intensity (although it accepts up to 1,2 Amper peaks) with over-heating protection.
- Supports motors with an operation voltage between 4,5 and 25V.
- We can attach another two servo or stepper motors.
- It provides a motor power connection separated from the one of the shield to avoid noise and interferences.
- Low price (Seriouslly).
- Compatible with Arduino UNO and Megas at least.
- There is an easy to use library to handle the motors.
In short, it is a cheap and practical shield to handle small 5V DC motors , but it will fall short if you need to drive powerful motors, since the demanded current intensity will easily surpass the maximum 0.6 ampers that this Shield can provide.
It is ideal for building autonomous robots with small motors and little weight and, above all, as a learning tool before investing a a lot of money in other more sophisticated options.
Connecting the Motor Shield
The shield connects directly the L293D H-Bridges to the PWM pins of Arduino and also incorporates an old acquaintance of ours, the 74HC595, a Shift Register, to save pins in the connection.
The Shield reserves the pins 3, 4, 5, 6, 7, 8, 9, 10, 11, 12. Pins 9 and 10 are used for the servos, in case we include them. Pins 2 and 13 are left free, as well as pins 0 and 1, that are used to communicate the Arduino with our PC via USB.
Pins A0 to A5 are available for our use and remember that, if needed, you can use them as digital pins as well.
There are connections for the terminals of the 4 motors, marked as M1, M2, M3 and M4 and it is advisable to follow the same criterion when connecting the terminals and the shield because otherwise you will get some of the motors turning the other way round (which is not very serious because it is easily solved).
An interesting thing about this shield is that it allows us to separate the power supply of the motors from the Arduino power supply, to which we should be very grateful because motors generate a lot of electromagnetic interference that can make our Arduino behave erratically.
That is why, whenever possible, it is convenient to separate both power supplies (although in an autonomous robot it will be difficult). To do this, we simply remove the power jumper and the power will be separated. This will be essential if you use 9 0 12V DC motors because your Arduino only operates at 5V (otherwise you will make an Arduino barbecue).
We will connect a first motor to the shield in order to test it. First assemble the shield to your Arduino. I recommend using an Arduino MEGA so that we can keep on doing things in the future, but it also works any other.
Connect the motor terminals to the screws A and B of the motor shield, leaving the center pin, marked as GND, free.
- It is not important which terminal is connected to which pin, because the only thing that will happen if you reverse it is that the motor will turn the other way round. It is recommended that you keep the same criteria for all motors, because it will save you headaches.
- As in this first example we will power the motor from Arduino and USB, do not forget to connect the power jumper, so that we power the motors straight from our Arduino.
It would be easy to drive the motor via the shield directly, using the control pins, and we propose it as an exercise if you like it , but here we will now download an Adafruit library to handle the shield directly, which will allow us to abstract the detail of pins.
The library we need is adafruit-Adafruit-Motor-Shield-library-8119eec, and to install it we follow the standard procedure.
To use it, the first thing we have to do is to write these two statements:
#include <AFMotor.h> AF_DCMotor Motor1(1);
The first line includes the Adafruit library in our sketch. The second creates a motor object instance, connected in this case to Motor1 gate, and then we set it with a parameter that can take values from 1(motor M1) to 4 (motor M4).
To set the speed of the motor we use:
Motor1.setSpeed(200); // We set the speed of Motor1 Motor1.run(RELEASE);
The first line set the motor speed at 200/255 of the speed limit, and the second line indicates that we want to leave the motor in neutral.
If we want the motor to move forward we use the following statement:
Motor1.run(FORDWARD);
And to move it backward we use the following statement:
Motor1.run(BACKWARD);
And that is all we need to control a motor. If we want to make a simple sketch to move the motor forward a few seconds and then backward, we can write a sketch similar to this:
#include <AFMotor1.h> AF_DCMotor1 Motor1(1); void setup() { Motor1.run(RELEASE); } void loop() { Motor1.run(FORWARD); delay (2000); Motor1.setSpeed(180); Motor1.run(BACKWARD); delay (2000); }
Moving several motors simultaneously
Let’s now connect 4 motors to our Motor Shield. We have around here a chassis with 4 wheels and motors that we will use as a base to build a four-wheel drive robot.
But it doesn’t matter wichever other robot you can have, or even a frame with wheels, provided that we can attach to it our Arduino plus the motor shield. A hard cardboard box to which you can attach the motors and wheels works great.
In our case we have put it on a pedestal, so we can test the different motors preventing it from running away. Let’s go with the test sketch. The first thing to do is to include the library and define the 4 instances of the motor objects:
#include <AFMotor.h> AF_DCMotor Motor1(1); AF_DCMotor Motor2(2); AF_DCMotor Motor3(3); AF_DCMotor Motor4(4);
We set the speed of the 4 motors inside the setup() fucntion:
void setup() { Serial.begin(9600); // Set up Serial library at 9600 bps Motor1.setSpeed(255); Motor2.setSpeed(255); Motor3.setSpeed(255); Motor4.setSpeed(255); }
And finally we will command the 4 motors to move back and forth simultaneously:
Motor1.run(RELEASE); Motor2.run(RELEASE); Motor3.run(RELEASE); Motor4.run(RELEASE); delay (1000) ; Motor1.run(FORWARD) ; Motor2.run(FORWARD); Motor3.run(FORWARD); Motor4.run(FORWARD); delay (2000); Motor1.run(BACKWARD); Motor2.run(BACKWARD); Motor3.run(BACKWARD); Motor4.run(BACKWARD); delay (2000);
As you can see, there is no difference from moving a single motor, it is only more tedious. Here you have the sketch:
It is convenient that you test your prototype so that all the wheels turn in the same direction and no one turns in reverse, because it would be a problem when moving fordward.
So we trust we have left the hardware of our robot ready to start programming it seriously. But it seems more convenient to end this chapter here and leave something for the next chapter, in which we will see how to move the robot and make it spin, apart from varying the cruising speed.
Pinout description
A question from a reader raised the issue of which pins were used and what for in this Motor Shield V1 and as it was a very reasonable question that was not previously covered , we have chosen to add this small annex specifying the pins that uses each motor and which are available otherwise .
Bear in mind that there are no Arduino pins attached to the motors directly. The management of the motors is done via the shift register to save pins, therefore you must necessarily use the library to handle them.
Summary
- We have introduced the Adafruit Motor Shield V1 , which is very cheap and useful to move small motors.
- We have seen its features and limitations, but it is always more useful to use a shield than building one ourselves.
- We have installed the motor control library and have started to learn the foundations of programming DC motors.
- We attached the motor shield to a small 4-wheel robot and tested that all worked as expected, leaving the hardware ready to start programming the movement of the robot.
Give a Reply