You will need:
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.
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.)
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.
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:
Color | Pin | Function |
---|---|---|
Black | 5 | Ground |
Red | 3 | TD (Transmit Data, aka Tx) |
White | 6 | DSR (Data Set Ready) |
Purple | 7 | RTS (Request To Send) |
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.
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.
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.
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.
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.
That's it! The circuit is complete. Reconnect the power, and your Pololu board should be alive and ready for commands.
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 SubThis 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 SubThis 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 ifThat'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.
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.
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!