Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Controlling DC motors using a shield

Save for later
  • 4 min read
  • 27 Feb 2015

article-image

 In this article by Richard Grimmett, author of the book Intel Galileo Essentials,let's graduate from a simple DC motor to a wheeled platform. There are several simple, two-wheeled robotics platforms. In this example, you'll use one that is available on several online electronics stores. It is called the Magician Chassis, sourced by SparkFun. The following image shows this:

controlling-dc-motors-using-shield-img-0

(For more resources related to this topic, see here.)

To make this wheeled robotic platform work, you're going to control the two DC motors connected directly to the two wheels. You'll want to control both the direction and the speed of the two wheels to control the direction of the robot.

You'll do this with an Arduino shield designed for this purpose. The Galileo is designed to accommodate many of these shields. The following image shows the shield:

controlling-dc-motors-using-shield-img-1

Specifically, you'll be interested in the connections on the front corner of the shield, which is where you will connect the two DC motors. Here is a close-up of that part of the board:
controlling-dc-motors-using-shield-img-2

It is these three connections that you will use in this example. First, however, place the board on top of the Galileo. Then mount the two boards to the top of your two-wheeled robotic platform, like this:
controlling-dc-motors-using-shield-img-3

In this case, I used a large cable tie to mount the boards to the platform, using the foam that came with the motor shield between the Galileo and plastic platform. This particular platform comes with a 4 AA battery holder, so you'll need to connect this power source, or whatever power source you are going to use, to the motor shield. The positive and negative terminals are inserted into the motor shield by loosening the screws, inserting the wires, and then tightening the screws, like this:
controlling-dc-motors-using-shield-img-4

The final step is to connect the motor wires to the motor controller shield. There are two sets of connections, one for each motor like this:
controlling-dc-motors-using-shield-img-5

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at £16.99/month. Cancel anytime

Insert some batteries, and then connect the Galileo to the computer via the USB cable, and you are now ready to start programming in order to control the motors.

Galileo code for the DC motor shield

Now that the Hardware is in place, bring up the IDE, make sure that the proper port and device are selected, and enter the following code:
controlling-dc-motors-using-shield-img-6

The code is straightforward. It consists of the following three blocks:

  1. The declaration of the six variables that connect to the proper Galileo pins:
    int pwmA = 3;
    int pwmB = 11;
    int brakeA = 9;
    int brakeB = 8;
    int directionA = 12;
    int directionB = 13;
  2. The setup() function, which sets the directionA, directionB, brakeA, and brakeB digital output pins:
    pinMode(directionA, OUTPUT);
    pinMode(brakeA, OUTPUT);
    pinMode(directionB, OUTPUT);
    pinMode(brakeB, OUTPUT);
  3. The loop() function. This is an example of how to make the wheeled robot go forward, then turn to the right. At each of these steps, you use the brake to stop the robot:
    // Move Forward
    digitalWrite(directionA, HIGH);
    digitalWrite(brakeA, LOW);
    analogWrite(pwmA, 255);
    digitalWrite(directionB, HIGH);
    digitalWrite(brakeB, LOW);
    analogWrite(pwmB, 255);
    delay(2000);
    digitalWrite(brakeA, HIGH);
    digitalWrite(brakeB, HIGH);
    delay(1000);
    //Turn Right
    digitalWrite(directionA, LOW); //Establishes backward direction of Channel A
    digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
    analogWrite(pwmA, 128); //Spins the motor on Channel A at half speed
    digitalWrite(directionB, HIGH); //Establishes forward direction of Channel B
    digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
    analogWrite(pwmB, 128); //Spins the motor on Channel B at full speed
    delay(2000);
    digitalWrite(brakeA, HIGH);
    digitalWrite(brakeB, HIGH);
    delay(1000);

    Once you have uploaded the code, the program should run in a loop. If you want to run your robot without connecting to the computer, you'll need to add a battery to power the Galileo. The Galileo will need at least 2 Amps, but you might want to consider providing 3 Amps or more based on your project. To supply this from a battery, you can use one of several different choices. My personal favorite is to use an emergency cell phone charging battery, like this:

    controlling-dc-motors-using-shield-img-7

    If you are going to use this, you'll need a USB-to-2.1 mm DC plug cable, available at most online stores. Once you have uploaded the code, you can disconnect the computer, then press the reset button. Your robot can move all by itself!

Summary

By now, you should be feeling a bit more comfortable with configuring Hardware and writing code for the Galileo. This example is fun, and provides you with a moving platform.

Resources for Article:


Further resources on this subject: