Simple Motor Control

I’ve finally gotten around to outlining the basics of DC motor control with the Arduino. We won’t be completely starting from scratch. I will assume you have some familiarity with the Arduino and its programming environment. If you’ve never done anything with the Arduino before I’d suggest doing some simple tutorials, like how to blink an LED, first. If you can make the blink tutorial work and wire everything up properly, you should be able to get through the examples here.

I’ll start out with a parts list, then I’ll outline how to control a single motor, and then I’ll move on to dual motor control.


Parts List

  • 1 Arduino; I’ll be using the Duemilanove but the examples should work with the newer Uno and most of the other Arduino variants as well.
  • 1 dual motor driver; I’ll be using the TB6612FNG from Pololu
  • 2 DC motors; I’m using micro metal gearmotors from Pololu
  • Batteries; I’m using a 4 AA pack that gives around 6V but I’ll discuss other options as well
  • 1 voltage regulator; this can be really handy but it’s not totally necessary
  • Hookup wire; I suggest you use ribbon cable but, at a minimum you should use several different colors of wire. It’s hard to appreciate how hard it is to wire up electronics using only one color of wire unless you’ve actually tried it.
  • Finally, some soldering skill will be necessary. There’s a good tutorial here if you’re not familiar. Do a lot of practicing before you try to solder anything you care about.

The Motor Driver

The Arduino’s digital pins cannot provide enough current to run even a small motor. So we need a power source for the motor that can supply a lot of current, but we also need a way to control that power source using the Arduino. This can be done using a simple switching transistor as in this great Adafruit tutorial but using a motor driver circuit like the TB6612FNG has some advantages:

  • We don’t have to build our own motor control circuit. As long as we wire it up correctly the TB6612FNG works right out of the box.
  • It allows us to control two motors with the same circuit, so it saves us a bit of complexity for dual motor control.
  • Most importantly, it gives us software control of the motor’s direction using the AIN1 and AIN2 pins for one motor, and the BIN1, and BIN2 pins for the other:
    • If IN1 is driven HIGH and IN2 is LOW the motor will turn forward
    • If IN1 is driven LOW and IN2 is HIGH the motor will turn backward
    • If IN1 and IN2 are LOW the motor will freewheel
    • If IN1 and IN2 are driven HIGH the motor will lock up or be in a “braking” state

The Battery / Voltage Regulator

All of the components in this setup have different power requirements. The motors require around 6V and they draw a lot of current. The Arduino needs 7-12V. It is possible to power the Arduino by connecting a 5V source to the 5V pin, but this bypasses the Arduino’s onboard voltage regulator so you need to make sure you have a regulated 5V source if you want to go that route. Lastly, the motor driver needs 5V for its logic. The motor driver can get its 5V from the 5V pin on the Arduino, but how do we satisfy the different requirements of the motors and the Arduino?

Option 1: Different power sources for the motors and Arduino

A 4 AA battery pack provides between 5.5 and 6.5V and can give a lot of current, which makes it good for the motors. But the Arduino needs 7-12V. Some people solve this by using a separate power source for the Arduino, usually a 9V battery. This is problematic for a couple of reasons. First, the Arduino’s onboard regulator isn’t very efficient when stepping 9V down to 5V and a lot of energy from the 9V battery is lost as waste heat. Second, having 2 separate battery packs adds weight and complexity. Wouldn’t it be much easier if we could power everything using just one battery pack?

Option 2: Voltage regulator

The addition of a voltage regulator like this one can make things much simpler. As in the wiring diagram above, you can power the motors directly from the battery pack and the Arduino can get power from the voltage regulator, which is tuned to output 5V. This approach is simpler, lighter weight, and more efficient than option 1.

A word of warning: do not try to power the motor with the output of the voltage regulator. They will draw more current than the regulator can supply and cause the voltage from the regulator to drop. When this happens your Arduino will reset, which can cause some pretty baffling problems if you don’t know what’s happening. Just power your motor directly from the battery as in the diagram.


Single Motor Control

The most important part of this project is wiring the physical components correctly. As I said in a previous post, I highly recommend wiring everything up on a breadboard. Do not make any soldered connections if you don’t absolutely have to. Rewiring is far easier when you’re using a breadboard. Below is a diagram of a wiring configuration that will work with my example sketch.

single motor control

breadboard layout for single motor control

Single Motor Example Sketch

It’s finally time for the example code! You should be able to copy and paste the code below into your Arduino IDE if you’ve wired your motor up exactly as I have in the diagram above.

// Single Motor Control example sketch
// by Andrew Kramer
// 5/28/2015

// makes the motor run at 1/2 power

#define PWMA 3 // PWM pin for right hand motor
#define AIN1 4 // direction control pin 1
               // AIN1 on the motor controller
#define AIN2 5 // direction control pin 2 
               // AIN2 pin on motor controller

void setup() {
  // set all pins to output
  for (int pin = 3; pin <= 5; pin++) {
    pinMode(pin, OUTPUT); // set pins 3 through 5 to OUTPUT
  }
  // set motor direction to forward 
  digitalWrite(AIN1, HIGH);
  digitalWrite(AIN2, LOW);
  // set the motor to run at 1/2 power
  analogWrite(PWMA, 128);
}

void loop() {
  // we're just making the motor run at a constant speed,
  // so there's nothing to do here
}

So now we can wire up a motor and feed it a constant amount of voltage. That’s useful, but not very interesting by itself. To make a differential drive robot like Colin, we need to be able to control two motors simultaneously.


Dual Motor Control

The basics of dual motor control are the same as for single motor control, we’re just adding an additional motor. Wire up your second motor as shown in the diagram below.

dual motor control

wiring diagram for dual motor control

Dual Motor Example Sketch

The sketch below, when used in a differential-drive robot will make the robot drive in a small circle. It’s a common problem to load up this example and find one or both of your motors is running backward. If this happens, you can either switch the states of the IN1 and IN2 pins for the backward motor in void setup() . Or, if you want a hardware solution, you can switch the positions of the wires connecting to IN1 and IN2 or OUT1 and OUT2.

// Dual Motor Control example sketch
// by Andrew Kramer
// 5/28/2015

// makes motor A run at 1/4 power and
// motor B run at 1/8 power
// in a differential drive robot this would cause the robot to
// drive in a small circle

#define PWMA 3 // PWM pin for right hand motor
#define AIN1 4 // direction control pin 1 for right motor
#define AIN2 5 // direction control pin 2 for right motor
#define PWMB 9 // PWM pin for left hand motor
#define BIN1 7 // direction control pin 1 for left motor
#define BIN2 8 // direction control pin 2 for left motor

void setup() {
  // set all pins to output
  for (int pin = 3; pin <= 5; pin++) {
    pinMode(pin, OUTPUT); // set pins 3 through 5 to OUTPUT
  }
  for (int pin = 7; pin <= 9; pin++) {
    pinMode(pin, OUTPUT); // set pins 7 through 9 to OUTPUT
  }
  // set both motors to forward 
  digitalWrite(AIN1, HIGH);
  digitalWrite(AIN2, LOW);
  digitalWrite(BIN1, HIGH);
  digitalWrite(BIN2, LOW);
  // set right motor to run at 1/4 power
  analogWrite(PWMA, 64);
  // set left motor to run at 1/8 power 
  analogWrite(PWMB, 32);
}

void loop() {
  // we're just making the motors run at constant speed,
  // so there's nothing to do here
}

Further Work

So now we can give two motors a constant amount of input power. That’s a bit boring, isn’t it? You can take things a bit further by making the motor speed respond to sensor inputs. For example, you could control the power to the motors using a potentiometer. You could make your differential drive robot follow more complicated paths or avoid obstacles using ultrasonic sensors, which I’ll discuss in a later post.

There’s a big problem here though. Just using analogWrite()  doesn’t control the motor’s speed, only the voltage to the motor. Even if you give both motors the same input voltage they won’t run at exactly the same speed. So you won’t be able to get a differential drive robot to drive in a straight line. For that we need actual speed control, and for that the motor needs to be able to tell the Arduino how fast it’s turning, and for that we need encoders. I’ll write a post about speed control using encoders like these soon.