Arduino Basics: hooking up DC motors


Chances are the first time you hear about Arduino is when it’s built into something that moves – anything from a robot to an automated lawnmover.

It’s also very likely the tech doing the ‘moving’ is one or more DC motors. However, the power demands of most DC motors not only require strong, high-capacity batteries, but often become too much for an Arduino board to handle.

Arduino needs help

Think about moving several tonnes of rock – you’re not going to do that yourself; you’ll rope in an excavator
or something. The same thing goes for Arduino and controlling DC motors – unless the motor is tiny, the Arduino Uno board can’t power it on its own.

Arduino has the brain, but not the muscle; however, the most common muscle for powering DC motors is what’s known as a full-bridge driver or H-bridge for short.

What’s an H-bridge?

hAn H-bridge is an electrical circuit technique that uses four electronic switches or transistors to control
a DC motor, where the motor is the horizontal bridge between two pairs of transistors.

You can see from the diagram that by carefully selecting which diagonally opposite pair of transistor switches is switched on, you can change the motor’s rotation direction.

Switch those transistors on and off at different rates and you can control the motor’s speed in that direction – a technique called pulse width modulation (PWM).

Of course, there’s more to it than that, but for our purposes, that’s enough of the theory.

Motor Drive Shield

shieldIn a nutshell, that’s also what happens inside the very popular Motor Drive Shield.

This special Arduino add-on has two L293D dual H-bridge chips. Each chip can handle up to two DC motors and the two chips together can handle four.

Add in support for two servo motors plus an ultrasonic sensor (both of which we’ve looked at previously), and it’s easy to see why this shield is a popular choice for small robotics projects.

gm2However, that’s only half the story. DC motors come in all shapes and sizes and they have two main parameters: their working voltage and their current consumption.

The L293D bridge can only handle small motors with a current draw of up to 600 milliamps (600mA) – anything larger than that and you need a more powerful solution (the L298D module
is a start).

So really, we’re talking small hobby motors only, typically those rated around 6VDC at no more than 600mA, like the yellow GM2 motors used on robots.

Connecting power

metalThe Motor Drive Shield has three terminal blocks: one small two-screw block and two five-terminal blocks.

The smaller two-pin block provides the main power for the motors, shield and the Arduino Uno board itself if you choose.

Next to that two-pin block, you’ll find a small two-pin header bridge – keep this in place to have this main power feed also drive the Arduino.

The two big terminal blocks provide connections for up to four motors (you typically ignore the middle GND pin in each block). Each outer pair of pins will be labelled M1 through M4.

Writing the code

To code the shield, the first thing is to load the AFMotor library into the libraries subfolder of your Arduino IDE installation, then reboot the IDE. Grab the latest version of the IDE here and you’ll find the library and demo source code on our website.

When writing your code, you must use the ‘#include’ statement to add in the AFMotor library – that goes right at the top of your app.

Next, you have to create an AF_DCMotor object, give it a name, define the H-bridge it uses (1 to 4) and set the clock rate of the digital signal sent to the motors.

Remember the PWM we mentioned before? The code we apply here sets the clock rate. By using the ‘MOTOR12_1KHZ’ code, we’re setting a 1kHz base clock rate.

Moving the motor

Once you’ve done that, there are only two commands you have to worry about from then on: ‘’ and ‘motor.setSpeed’. sets the direction and you provide it with a simple text command in brackets – FORWARD to go forward and BACKWARD to go in reverse.

The ‘Motor.setSpeed’ command is a little more complex; here, you provide an 8-bit decimal number from 0 to 255, with 0 being stopped and 255 flat-stick.

Motor torque

What that 8-bit number is doing is setting the proportion of time that power is applied to the motor in any one-millisecond interval (one cycle of a 1kHz waveform).

The higher the number, the longer the power is applied, so setting it to 128 means the power is on for half of each cycle and off for half of each cycle. It’s a relatively easy way to set the speed, but it’s only one aspect of it.

Many hobby motor units, like the GM2, feature a built-in gearbox and a tiny high-speed motor. These motors can spin at incredible revs (10,000rpm or more), but in terms of the power they produce, they can barely blow out a candle.

The gearbox reduces the revs, but gears up the torque, or the motor’s pulling ability. You’ll find 1:48 is a common ratio for these motors.

However, you only get the benefit of that torque if the motor is running flat-chat. Reduce the speed and you also reduce the torque, so a robot that will punch through walls at full speed will progressively struggle to find its way out of a wet paper bag as the speed is reduced.

Ideally, if you need a lower rotational speed, you should look for a geared motor with a higher gear ratio (we’ve seen up to 1:224 with these smaller motors).

If you’re planning to use multiple motors, remember the power consumed adds up, so you’ll need a decent set of AA batteries to power them.

It’s usually recommended you remove that little header bridge on the Motor Drive Shield and power the Arduino board separately using a 9V battery, as voltage fluctuations from a single supply powering the motors and the Arduino can lead to instability. That’s the simple option, but it relies on two power sources.

You’ll also need some basic soldering skills to attach wires to the motors that you can screw into the shield terminal blocks – if you don’t know how, rope in a mate who does.

LEGO Mindstorms kits are popular, but building your own Arduino robots is way more fun.

  • the longer the power is applied, so setting it to 128 means the power is on for half of each cycle and off for half of each cycle. It’s a relatively easy way to set the speed, but it’s only one aspect of it.