[Car sporting super cheap Arduino motor shield]

Robotic Cars

Most of the cars here are powered by 2 motors: one for the right wheels, one for the left. To go straight, you need to drive both motors at the same speed. To turn right, drive the left wheels faster than the right ones, and the opposite to turn left.

Motor shields

Wiring for motors is complicated, so we've simplified it by making something called a "motor shield" that plugs in to the top of the Arduino.

You can still plug other things in to the digital and analog pins on top of the shield, but there are some pins you shouldn't use because they're already being used for the motors. Those are digital pins 2, 3, 4, 5, 9, and 10:
Motor # Control Direction Direction
0 9 2 3
1 10 4 5

You should also avoid using pins 0 and 1, since they're used for serial printing (in case you need that for debugging).

Pins 6, 7, 8, and 11 through 13, are safe to use, as are all of the Arduino's analog pins.

Plugging in the motor

[motor wires plugged into the motor shield] [motor wires going into 5-pin header]

A motor has 2 wires coming from it. The two wires plug in to the header in the corner of the motor shield.

If the header has 5 pins instead of 4, use the outer 2 on each end and leave the center hole empty.

You can plug in a motor either way -- it doesn't matter which wire goes where, except that one way the motor will run backward. The only way to be sure which direction the motor will run is to try it. So don't be surprised if your car's wheels go backward the first time you try it!

Programming it

There's a motor library called HalfBridge you can use to make the programming a lot simpler. It works like this:

Note: this doesn't work as well as I'd like, and I'm currently reworking the software. HalfBridge still works but I should have a better library out soon.


// On the motor shields,
// Motor 0 has a speed control on pin 9, direction controls on pins 2 and 3.
// Motor 1 uses 10 for speed control, 4 and 5 for direction.
Motor motors[2] = { Motor(9, 2, 3), Motor(10, 4, 5) };

void setup()

Now inside loop(), to change the speed of one of your motors, you can do something like this:


That makes both of the motors go at the same speed. To stop a motor, set its speed to zero:


To change direction, you can just give it a negative speed.


Speeds can be between 0 and 255. But don't set the speed at 255 to begin with -- test at a much lower speed, like 100-159.

Testing -- important!

Remember, if you get your code right your wheels are going to start spinning, and you may not know which direction. So when you're first testing, put something underneath your car's body -- not touching the wheels -- so the wheels can spin without the car running off the desk.

If you have a motor spinning in the wrong direction, the easiest way to fix that is to unplug it and plug it in the other way.

Once you get your program working, you can hook up batteries, unplug the USB cable and put the car on the floor to see where it goes.

What next?

Once you figure out how to get the wheels spinning in the right direction, the rest is up to you! Maybe hook up a rangefinder and have the car stop before it hits something. Or hook up a light sensor and have the car spin around when you shine a flashlight on it.