Bring old toys back to life with Arduino

Motor Skills

To understand how to move a DC motor, I'll first try with an external motor like that shown in Figure 8. This type of motor is common in Arduino starter kits and is not powerful enough to damage your electronics when it is switched off (See the Motor Danger box).

Figure 8: A typical DC motor used in home robotics and good for Arduino experiments.

Motor Danger

Be careful what you plug in to your Arduino in the way of motors and servos. One false step, and you can irreparably kill your electronics. To rotate a spool of copper wire, you need to run an electric current through an electromagnet (which is in essence how an electric motor works). When you cut off the electric current, the spool does not stop rotating immediately, so it creates a current between the two poles of an electromagnet, that is, an electric feedback from the motor that rushes back to whatever was powering it in the first place.

So, if you have a motor connected directly to your Arduino and that motor is powerful enough, the moment you shut it down, an electric current will surge from the motor toward your Arduino. If it's strong enough, it can fry your board.

The simplest way to run the motor is by plugging one of the wires into the 5V pin on your Arduino and the other into GND. But this is boring: The Arduino just acts like a power source.

Much better is to start and stop the motor from a sketch. First, set up a circuit as shown in Figure 9. Before going into how it works, load Listing 2 into your Arduino and see how your motor starts rotating for a second, then stops, then starts again, and so on.

Figure 9: Wiring the motor to the Arduino.

Listing 2

Starting and Stopping a Motor

01 int motorPin = 3;
02 void setup()
03 {
04   pinMode(motorPin, OUTPUT);
05 }
07 void loop()
08 {
09   digitalWrite(motorPin, HIGH);
10   delay(1000);
11   digitalWrite(motorPin, LOW);
12   delay(1000);
13 }

Apart from the Arduino and the motor, you have a resistor, a transistor, and a diode. The most interesting component is the transistor. The transistor has three legs and acts something like a switch: When in the "on" state, an electric current flows into one leg (the collector) and out of the other (the emitter). When in the "off" state, no current passes through.

The third leg, called the base, is what decides whether the transistor is on or off. If you send a small electric current (e.g., sending a HIGH signal through one of Arduino's pins) to the base, the larger current will flow from the collector to the emitter.

If you cut off the electric current to the base (sending a LOW signal through the Arduino's pin) the flow through the circuit will stop.

The transistor in the picture is a PN2222 [3]. Holding it with the flat face toward you, the emitter is on the left, the base is in the center, and the collector is on right, so the current flows right to left, toward ground in this layout.

A current to the base of the transistor that is too powerful can destroy it, so I inserted a resistor between Arduino pin 3 and the base. In this case, I used a 27ohm resistor (red, purple, and black bands).

The diode is also there for protection. It ensures current runs in only one direction, and the "feedback" I mentioned in the Motor Danger box never happens. The stripe on the diode should be nearer the input of the current. As placed, the diode ensures the current only flows from right to left.

An interesting twist on the above explanation is that on pin 3, you can regulate the speed of the motor by using analogWrite() instead of digitalWrite(). Be warned, though, that under a certain number, the motor won't spin. It's up to you to find out what its lowest threshold is.

To try this out, you can change out the loop section of your sketch with Listing 3. When run, you will notice how your motor spins at full speed for a second, slows down for another second, and then speeds up again, and so on.

Listing 3

Speed Sketch

01 void loop()
02 {
03   analogWrite(motorPin, 255);
04   delay(1000);
06   analogWrite(motorPin, 50);
07   delay(1000);
08 }

Bridging the H

When the time comes to connect your Arduino up to the Robosapien, you're going to want to control not only when the motor runs and at what speed, but also its direction. If you want the motor to lift Robo's arm, at some point you're also going to want to lower it back down again, and this is where things start getting tricky.

Starting with the current circuit in Figure 9, you can change the direction the motor spins by swapping the polarity of the circuit (e.g., by physically unplugging the GND and 5V jumpers and switching them around). So, if the current flows from left to right within the circuit, the motor will rotate, say, clockwise, and if the current flows from right to left, the motor will rotate counter clockwise. This is crude, but it's actually a good start.

In the circuit in Figure 10, the power source sits at the top with a motor bang in the middle and the ground at the bottom. It also has two switches on each side: two above the motor and two below.

Figure 10: An H bridge allows you to change the direction of the current as it crosses the motor.

If you open the switches in the upper right and the lower left, the current won't have any choice but to run down the left side of the circuit first, cross the motor from left to right, and then continue down to ground on the bottom right side, as shown in Figure 10A.

If you open the upper left and lower right switches, the opposite happens, and the current crosses the motor from right to left as shown in Figure 10B. As you already saw, changing the direction of the current crossing the motor also changes the direction in which it spins. The circuit in Figure 10 is called an H bridge, and if you tilt your head to one side you'll see why.

Now, wouldn't it be cool if you had some kind of electronic component that acted like a switch, but you could open and close it by sending it a signal from an Arduino pin? That is exactly what I've done in Figure 11 and Figure 12 by setting up an H bridge using transistors.

Figure 11: An H bridge implemented on a breadboard with an Arduino controlling the transistors.
Figure 12: It looks a bit messy, but this H bridge drives a motor in both directions.

By studying Figure 11, you will notice two things: (1) Only two pins (9 and 10) are used to send pulses to the transistors, despite having four transistors, and (2) the transistors at the bottom of the circuit have a P printed on them instead of an N. In this example, you have two kinds of transistor: NPN and PNP. NPN transistors are the kind you have already seen (i.e., those that open a flow from the collector to the emitter when triggering the Base and shut it off when cutting off the current to the Base).

PNP transistors [4], on the other hand, work the other way around and shut off the current when spiking the Base and let a current pass from collector to emitter when cutting off the current to the Base.

From Figure 10, you can see that by combining both types of transistor, you can connect S1 and S3 to just one pin, and have them take on opposite states, no matter what is sent down the cable. Likewise with S2 and S4. To check out how the H bridge works, copy and upload the code in Listing 4 to your Arduino.

Listing 4

H Bridge Example

01 void setup() {
02   pinMode(9, OUTPUT);
03   pinMode(10, OUTPUT);
04 }
06 void loop() {
07   analogWrite(9, 225);     // Spin one way
08   analogWrite(10, 0);
09   delay(1000);
11   analogWrite(9, 0);      // Spin the other way
12   analogWrite(10, 225);
13   delay(1000);
14 }

You can also try this out on the Robosapien. Locate the left elbow and hand plug (Figure 4) and connect the lowermost pins to the H bridge where you connected the motor (see Figure 11). The left elbow will now rotate, opening and closing the claw.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content