The most recent version of this article can be found at this link.
Introduction
The RoboClaw is very easy to control from an Arduino. We’ll be using a serial connection to allow the Arduino and RoboClaw to communicate. This type of setup requires minimal wiring, configuration and code to work well.
Materials
- (1) RoboClaw motor controller
- (2) motors
- (1) battery
- (1) Arduino board
- (2) male to female jumpers
- (1) small screwdriver
- (1) computer
Steps
1. Install BasicMicro Motion Studio on your computer. Please see the tutorial here. Note that Basic Micro Motion Studio requires Windows 7 or later.
2. Download the RoboClaw Arduino library and examples from here.
3. Identify the positive and negative leads of your battery. There are two screw terminals on the RoboClaw labeled + and -. Find these terminals and loosen the screws on them. Attached the negative lead first by placing the lead in the loosened terminal and tightening the screw down until the connection is snug. Repeat this step with the positive lead.
Figure 1
Figure 2
4. Connect the USB cable between the RoboClaw and your computer.
5. Open BasicMicro Motion Studio. In the upper left hand corner of the application you should see a box labeled “Attached Devices”. Your RoboClaw should appear here, if not check your USB connection and check that power has been properly applied to the RoboClaw. If your device is listed, select it and click “Connect Selected Unit”. The Stat1 led on the RoboClaw will begin flashing. Once you’ve verified that you can connect the RoboClaw to Motion Studio click “Disconnect Selected Unit” to stop communication with the RoboClaw.
Figure 3
6. Disconnect power from the RoboClaw by removing the positive lead and then the negative lead. You must do it in this order or you will damage the RoboClaw. If your battery has a switch you can simply open the switch to disconnect power.
7. Now we will begin wiring the motors to the RoboClaw. Locate the terminals labeled M1A and M1B. Loosen the screws on these terminals before you begin wiring. Wire the first motor to the terminals labeled M1A and M1B by placing one end of the bare wire in each terminal and tightening the screws with your screwdriver until the connection is snug. Note that the order of wiring does not matter for the first motor.
8. The second motor is wired to the terminals labeled M2A and M2B. The order of wiring does matter for this motor. If the motor is wired improperly the direction of rotation might be incorrect. Look at the first motor you wired and note what color wire you wired to the M1A terminal. Connect the same colored wire of the second motor to the M2A terminal. Lastly, connect the final wire of the second motor to the M2B terminal. Make sure that your motor wire connections are secure and do not come loose from the terminals.
Figure 4
9. Reconnect power to the RoboClaw as shown earlier.
10. Open Basicmicro Motion Studio again and reconnect the RoboClaw again by clicking “Connect Selected Unit”. Click on “PWM Settings” in the left-hand pane. Here we will test the motors to make sure they’re working. Find the box labeled “Control”. Slide the sliders for Motor 1 and Motor 2. The motors should turn when operating the sliders. Also check to ensure that the motors turn forwards when sliding the sliders up and backwards when sliding the sliders down. If the motors do not turn the proper direction you need to reverse the wiring of one or both motors.
Figure 5
11. Disconnect the RoboClaw in Motion Studio and remove power from the RoboClaw as shown earlier.
12. Now we will make the connections necessary for serial communication between the RoboClaw and the Arduino. On the Arduino we will be using pin 10 as the RX pin and pin 11 as the TX pin. Locate the pin headers S1 and S2 on the RoboClaw. Note that these are three pin headers. The pin on the outside edge of the board is the signal line for each header. Using a jumper connect pin 11 on the Arduino to the signal pin of S1 on the RoboClaw. Next connecting pin 10 on the Arduino to the signal pin of S2.
RoboClaw | Arduino |
S1 signal pin (pin closest to edge of board) | Pin 11 |
S2 signal pin | Pin 10 |
S1 ground pin (pin closest to inside of board) | Any ground pin |
13. If you are powering the Arduino via USB or external power there is no need to make a power connection between the RoboClaw and the Arduino. However, you do need to ensure there is a common ground between the RoboClaw and the Arduino. Using a jumper connect any ground connection on the Arduino to the ground pin of either the S1 or S2 headers on the RoboClaw. This is the pin that is on the innermost side of the pin header and is labeled as such on the RoboClaw.
Figure 6
14. Reconnect power to the RoboClaw as done previously.
15. Open Basicmicro Motion Studio and connect the RoboClaw in use. Click on the left-hand pane titled “General Settings”. Find the area labeled “Setup” and set the Control Mode to Packet Serial [Figure 7]. Now find the area labeled “Serial”. Set the field labeled Packet Serial Address to 128. Finally, in the same area, set the field titled Baudrate to 38400.
Figure 7
16.) Save the settings to the RoboClaw by going to the menu and selecting “Device” and then “Save Settings”.
17. Connect the Arduino to your computer with a USB cable.
18. Open the Arduino IDE. In the menu click on “Sketch” -> Include Library -> Add .ZIP Library. When prompted located the arduino.zip file you downloaded earlier and click “Open”. This will add the Basicmicro Arduino library to your system.
19. Via your file explorer navigate to where your arduino.zip file is. Right click the file and select “Extract All”, then click “Extract” to unzip the file. In the unzipped file are examples of Arduino code ready to use with the RoboClaw.
20. In the Arduino IDE menu click “File” -> “Open...”. Navigate to the directory we created when we unzipped the file in the last step. Open the file “PacketSerialSimplePWM” which is located by going to “arduino/RoboClaw/examples/PacketSerialSimplePWM”.
21. The file should now be visible in the Arduino IDE. Click the Upload icon to send the program to your Arduino.
Figure 8
22. There are several commands used in the file you loaded. Let’s take a look at them briefly:
ForwardM1(address, 64)
ForwardM2(address, 64)
This instructs the RoboClaw to turn the selected motor, based upon using either “M1” or “M2” in the command, in the forwards direction. The address value is needed in the packet serial mode we’re using to indicate which RoboClaw we’d like to send commands to. The numerical argument tells the RoboClaw how fast to turn the motor. The range of values is 0-127, with 0 being stopped and 127 full speed.
BackwardM1(address, 64)
BackwardM2(address, 64)
This works like excactly like the previous command except it commands the RoboClaw to turn the indicated motor backwards. The range of speed values is still the same here.
ForwardBackwardM1(address, 92)
ForwardBackwardM2(address, 32)
This command is slightly different that the previous ones. It has has ability to turn the selected motor forwards or backwards. The speed value is what determines the direction of rotation. The range of values of still 0-127, however now 0 indicates full backwards speed, 64 in the middle indicates stop, and 127 indicates full forwards speed.
23. Your motors should now be running in the order the program runs them in. If the system is not running properly please check that all of your connections are correct and secure. Common problems to look out for are reversed serial lines, RX and TX, as well as reversed motor wiring. If you do need to reverse or otherwise change the wiring be sure to remove power from the RoboClaw before doing so.
Figure 9
Conclusion
You’ve now seen how to connect an Arduino to the RoboClaw and operate it via packet serial commands. To further explore this concept edit the code in the Arduino IDE and try other commands. A full listing of these commands can be found in the source code provided with the library you downloaded earlier.