Tutorial: Pololu Micro Dual Serial Motor Controller

This page describes how to get started with one of Pololu's excellent motor controllers, the Micro Dual Serial Motor Controller. Though their manual is excellent, and I will assume you have it on hand, this document is aimed more at electronics newbies (like myself). My goal is that pretty much anyone with some patience and the items below should be able to follow along, and soon have a motor turning under computer control.

You will need:

Actually, you can skip the sacrificial cable if you have a DB9/breadboard adapter; and if that adapter also converts RS232 to TTL signals, then you can skip the transistor and two of the resistors as well. But the instructions below will assume you haven't any of these, and describe how to hack your own serial interface.

Preparing the Serial Cable

Keyspan USA-19HS The first thing you'll need is some way to get serial transmissions out of your computer and into your breadboard. If your computer is like mine, it doesn't even have a serial port, so you'll need a USB-to-RS232 adapter. I use the Keyspan USA-19HS, shown at right. Other brands and models don't work well at all on modern Macs, but this one should work fine on Windows and Linux PCs too. Make sure you've installed the latest drivers from Keyspan.

Now that you have a working RS-232 port, with a DB9 connector on the end, you need some way to get signals into your breadboard. You could spring for a DB9 Breadboard Adapter. Since we need TTL-level serial signals for use with the motor controller, you might even use a breadboard-compatible RS-232-to-TTL converter, like the Pololu Deluxe Serial Adapter or the HVW RS-232 Driver Module. serial cable

Alternatively, you can make one yourself, as shown here. One way to do that is to hack a standard DB9 serial cable, like the one shown at right. (Another way is to start with a bare DB9 connector, and solder wires to it. But being a poor solderer, I prefer the method shown here.)

cable guts The first step is to cut the cable with wire cutters. You need the female end to plug into your serial port (or adapter), so if you want to maximize the cable length, cut it near the male end. Then carefully cut the cable sheath away from the wires, a few centimeters from the end. The result should look like the image at right; there is a colored wire for each of the nine pins of the connector, plus a naked (uninsulated) ground wire which connects to the metal plates surrounding the connectors.

line hunting Next, you have to figure out which wires correspond to the pins of interest. I couldn't find a list of standard colors for serial cable lines; I suspect there is no standard. So it was a matter of trial and error. Here's my method: Slightly bend the end of a short piece of breadboard wire, then insert it into one of the holes on the DB9 connector. (The bend is to make sure it has a good connection, and make it stick in the hole a little better.) Then hold the other end of the wire against one probe of your multimeter, as shown here.

Then, set your multimeter to reading ohms (I set mine to 20 kOhm), and with the other probe, gently brush the ends of each of the colored wires sticking out of the cable. This is harder than it sounds, because each of these wires has more insulator than conductor. But with a bit of patience, you should find that the ohmmeter reacts to one of the wires. That's the wire connected to the hole you're testing; write its color down.

Note that if you find it hard to detect the connected wire this way, you could just strip all the wires, and then identify them. But for this application, we need only four of the nine wires, and I am lazy, so I preferred to identify the ones needed and strip only those. You need to find the wires connected to pins 3, 5, 6, and 7 (as shown in the schematic in the Pololu 0410 manual). In case your cable is like mine, here are the colors I found:

Red3TD (Transmit Data, aka Tx)
White6DSR (Data Set Ready)
Purple7RTS (Request To Send)

stripped cable When you've found the relevant wires, then you need to strip the insulation from them. I used wire cutters for this; gently close the cutters on the insulation, without biting too deep, about 5 mm from the end. Then rotate them, back and forth, so that they cut through (or at least score) the insulation all the way around. Finally, pull the end piece of insulation off, either with the wire cutters themselves or with needlenose pliers. As you can see at right, the first wire I did (the black one) suffered a bit, but the others came out pretty well. If you botch one too badly, you can always cut back the cable sheath a bit further and try again. Note that I also chose to bind up the unused wires with a twist tie to keep things neat. You could also just clip them, but I kept them around in case I need them for some other project one day.

cable in breadboard The last step is to push those exposed wires into your breadboard, as shown here. Note that I pushed the white (DRS) and purple (RTS) lines into the same row to connect them together, as shown in Pololu's schematic. The others I just spread out a bit so I'd have room to work. If your cable is like mine, this step is harder than it sounds, because the exposed wires are stranded rather than solid -- that is, they are composed of many tiny fibers which would rather bunch up than go into the breadboard. Twist the fibers up into a tight bundle, and they should go in after a few attempts. Test each one by inserting a breadboard wire into the same row, and measuring the resistance between that and the corresponding hole on the DB9 connector. If all read low resistance, then congratulations! Your computer is successfully connected to the breadboard, and the hardest part is over.

Power Supply & Motor

Once you have a serial connection to your breadboard, you're almost ready to make something move. Start by ensuring you can drive the motor directly from its power supply.

If your motors run at 2.5 to 5.5 volts, then you can run the motor and the logic board off the same power supply. That's the simplest configuration, so let's do that first. For testing purposes, I bought a small toy motor (Radio Shack item 273-258), rated for 1.5-3 VDC, and drawing up to 0.25 A, well within the 1A limit of the controller.

ready board batteries Two fresh AA batteries puts me at about 3V, which is within the range of both the motor and the controller board. You can probably find a two-battery clip with wire leads you can just plug into the breadboard. I had only a four-battery clip, so left two of the battery bays empty, and used a small piece of wire to make a connection between the other two, as shown in the first image at right. The other image shows the whole breadboard, with power provided to three of the vertical +/- lines.

Then, attach wires to your motor and plug them into the power lines on your breadboard. It should spin vigorously. Pull the motor wires back out, and you're ready to make the controller work.

A Word of Caution about Static Electricity
If you live in a dry environment, like I do, you have to be especially careful not to zap your controller board with a spark of static electricity. Consider getting a static dissipative mat (I got mine from Jameco), and ground it to a metal pipe or to the third (D-shaped) hole of a wall socket. Touch this each time before touching your Pololu controller, or anything connected to it.

Motor Controller Circuit

All that's left to do at this point is to add the handful of components and connections that accomplish two things: (1) convert the RS-232 serial signals to logic-level serial signals, and (2) connect the Pololu board to its inputs and and outputs. (Note that if you're using an RS232-to-TTL converter, then the first task is already done; you can connect that directly to your controller board.)

schematic The excellent product manual from Pololu included the schematic at right (but see update below), which shows us how to convert the RS-232 serial signals to logic-level signal. It both reduces the voltage, and inverts the signals (so that a "1" bit is a high voltage rather than low). This is the bulk of what we need to do. Note that since we are using the same power supply for both the logic and the motor, we don't have to worry about which is which. If you're using separate power supplies, then be careful to use the correct one for each step.

(UPDATE: Since doing this project, Pololu has updated the schematic in their manual to reflect a more typical connection for the handshaking lines. Please consult the manual for the latest version, which connects pin 6 to pin 4, and pin 7 to pin 8. All my photos below reflect the older arrangement (shown in the schematic here), but you can easily adjust for the new one. For the project described here, these handshaking lines aren't really needed anyway.)

Here are the steps for assembling the circuit, which you can see illustrated in the image below.

  1. Disconnect one of your battery wires, so the board is not powered while you're working on the circuit.
  2. Connect the Ground wire from the serial cable to the the logic ground (yellow wire in row 29 in the picture). transistor
  3. Connect the Tx line from the serial cable through a 10k resistor to some unused row of the board (around row 25).
  4. Connect the middle pin (pin 2, the Base) of an NPN transistor to the other end of that 10k resistor. Orient the transistor with the flat side to the left, and you should have the Collector (pin 3) towards the top of the board, and the Emitter (pin 1) towards the bottom. For your reference, a diagram relating transistor pin numbers to their function is shown at right.
  5. Connect the Emitter (pin 1) of the transistor to the logic ground (row 25 in the picture), and the Collector (pin 3) to an unused breadboard row (row 22).
  6. Add a 4.7k resistor between the Collector pin of the transistor, and another unused row (around row 20).
  7. Connect the other end of the resistor to the logic supply voltage (row 18).
  8. Connect that same Collector pin to the serial control input (pin 4) of the Pololu controller. I chose to put the controller in the next section of the breadboard, so I would have plenty of room. If you're eager to conserve space, you could probably cram it right in next to the transistor. (The controller is the thin board with two chips from rows 19 to 27 in the picture, with its serial input connected via the orange wire in row 22.)
  9. Refer now to page 3 of the Pololu manual, "Connecting the Motor Controller." As described there, we want to connect controller pin 2 (ground) to both the logic ground and the negative terminal of the motor power source (green wire in row 20).
  10. Connect controller pin 1 (motor supply) to the positive terminal of the motor power source -- in our case, that's the same as the logic supply (orange wire in row 19).
  11. Connect controller pin 3 (logic supply) to the logic supply voltage (orange wire in row 21).
  12. Connect controller pin 5 (reset) through a 4.7k resistor to the logic supply. Note that in a real application, you might want to connect this through some sort of switch, as suggested in the manual. But for prototyping, I was quite content to reset the board by unplugging the power as needed.
  13. Connect pins 8 and 9 to the motor. If you have two motors, connect pins 6 and 7 to the second one.


That's it! The circuit is complete. Reconnect the power, and your Pololu board should be alive and ready for commands.

Speaking to the Controller via REALbasic

All Pololu serial devices use a straightforward yet scalable serial protocol, and the Micro Dual Serial Motor Controller is no exception. All commands to the controller take the form of a simple four-byte message, as documented in the manual. With REALbasic's cross-platform serial control support, controlling these devices is quite easy.

serial control To get started, make a new REALbasic project, and drag a serial control onto the main window editor. Change its name to "PMC" (for Pololu Motor Controller). The motor controller never sends any data back -- and indeed, it couldn't do so, since we didn't connect that line of the serial cable to anything. So the only event you need to implement is the Error event. Double-click the serial control, switch to the Error event, and enter:

  MsgBox "Serial port error: " + str(me.LastErrorCode)
This will make sure you know about any errors that may occur. Now, to send commands to the controller, it's helpful to have some utility functions that pack up the command messages for you. The way I see it, there are two basic operations: setting the speed and direction, and applying the brake. Applying the brake is easier, so let's do that first. Add the following function to your window (or to some module):
Sub BrakeMotor(motorNumber As Integer)
  // Actively stop the given motor from spinning.
  // (To free it, call SetMotor with a speed of 0.)
  Dim msg As New MemoryBlock(4)
  msg.Byte(0) = &h80  // constant start byte
  msg.Byte(2) = motorNumber * 2 // low bit 0 --> reverse
  msg.Byte(3) = 0  // speed 0 in reverse --> brake
  PMC.Write msg
End Sub
This tells the Pololu controller that you want a speed of 0 in reverse, which it interprets as a command to actively brake the motor. Note that we made "motorNumber" a parameter, so you can stop any motor on your serial chain, or half the motors at once (see the Pololu manual for details about motor addressing).

Next, you'll want a way to set the motor spinning, or at least to allow it to spin freely. Add the following method for that:

Sub SetMotor(motorNumber As Integer, speed As Integer)
  // Utility function to set a motor to the given forward speed
  // (or if speed < 0, then that speed in reverse).
  // Valid range for speed is -127 to 127.
  // Note that a speed of 0 does not brake the motor with this
  // function; use BrakeMotor for that.
  Dim msg As New MemoryBlock(4)
  msg.Byte(0) = &h80  // constant start byte
  Dim dirBit As Integer
  if speed >= 0 then dirBit = 1  // motor direction
  msg.Byte(2) = motorNumber * 2 + dirBit  
  msg.Byte(3) = Abs(speed)
  PMC.Write msg
End Sub
This lets you specify a speed as a signed number, from -127 to 127, where negative numbers make it spin in reverse. A speed of 0 stops applying power to the motor, but does not actively brake it. All that's left is to establish a serial connection with the device. In the Open event of the window, add this code:
  PMC.SerialPort = System.SerialPort(0)
  if not PMC.Open then
    MsgBox "Unable to open serial port 0."
  end if
screen shot That's it! You now have a connection to the unit, and methods for telling it what to do. Add a bit of UI (e.g., PushButtons) to call SetMotor and BrakeMotor, and you can test your connection interactively.

I've made a simple demo project (made in REALbasic 2005R4) available which includes the above functions, plus some controls for letting you start, stop, break, and change the speed of any motor. A screen shot is shown at right. This code is public domain; share and enjoy.

That's it!

These simple steps should suffice to control one or two motors from your computer. Additional motors can be controlled by simply configuring more controller boards for different motor numbers (as described in the manual), and then attaching them all to the same serial signals.

If it doesn't work right away, and you hacked a serial cable like I did, then the most likely culprit is the connection between the cable and the breadboard. This worked well for me for a while, but has proved rather unreliable. It's probably worth spending a few bucks for one of those serial/breadboard adapters. with DB9 adapter

After finishing the first draft of this tutorial, I took my own advice, and got the Winford DB9 Breadboard Adapter. As you can see in the image at right, this neatened the board a bit, and formed a much more reliable connection. I haven't had any difficulties with the circuit since.

If you have any questions or feedback about this tutorial, feel free to write to me about it. Happy hacking!

Last Updated: 12/20/05 . . . . . . webmaster@strout.net