Support » Jrk G2 Motor Controller User’s Guide » 6. Setting up the control method »
6.2. Setting up serial control
This section explains what kind of serial interface the Jrk G2 has and how to connect a microcontroller or other TTL serial device to it so that you can send commands to control the Jrk G2. The Jrk G2 library for Arduino makes it particularly easy to control the Jrk G2 from an Arduino or Arduino-compatible board such as an A-Star 32U4. For example code that shows how to use the Jrk’s serial interface from a computer, see Section 15.
About the serial interface
The RX and TX pins of the Jrk provide its serial interface. The Jrk’s RX pin is an input, and its TX pin is an output. Each pin has an integrated 100 kΩ resistor pulling it up to 5 V and a 220 Ω series resistor protecting it from short circuits.
The serial interface uses non-inverted TTL logic levels: a level of 0 V corresponds to a value of 0, and a level of 5 V corresponds to a value of 1. The input signal on the RX pin must reach at least 4 V to be guaranteed to be read as high, but 3.3 V signals on RX typically work anyway.
The serial interface is asynchronous, meaning that the sender and receiver each independently time the serial bits. The sender and receiver must be configured to use the same baud rate, which is typically expressed in units of bits per second. The data format is 8 data bits, one stop bit, with no parity, which is often expressed as 8-N-1. The diagram below depicts a typical serial byte:
Diagram of a non-inverted TTL serial byte. |
---|
The serial lines are high by default. The beginning of a transmitted byte is signaled with a single low start bit, followed by the bits of byte, least-significant bit (LSB) first. The byte is terminated by a stop bit, which is the line going high for at least one bit time.
Connecting a serial device to one Jrk
If you have not done so already, you should follow the instructions in Section 4.3 to configure and test your motor, and follow the instructions in the appropriate part of Section 5 to set up your desired feedback method. You should leave your Jrk’s input mode set to “Serial / I²C / USB” (the default). In the “Serial interface” box, you will need to change the serial mode to “UART, fixed baud rate” and specify your desired baud rate. Be sure to click “Apply settings”.
Next, connect your serial device’s GND (ground) pin to one of the Jrk’s GND pins.
If your serial device operates at 5 V, you can directly connect the device’s TX line to the Jrk’s RX line and connect the Jrk’s TX line to the device’s RX line. The connection to the Jrk’s TX line is only needed if you want to read data from the Jrk. These connections, and some other optional connections, are shown in the diagram below:
Connecting a 5V microcontroller or other TTL serial device to the TTL serial interface of the Jrk G2. Dashed connections are optional. |
---|
If your serial device operates at 3.3 V, then you might need additional circuitry to shift the voltage levels. You can try connecting the device’s TX line directly to the Jrk’s RX line; this will usually work, but the input signal on the RX pin must reach at least 4 V to be guaranteed to be read as high. If you want to read data from the Jrk, you will need to consider how to connect the Jrk’s TX line to your device’s RX line. If your device’s RX line is 5V tolerant, meaning that it can accept a 5 V output being applied directly to it, then you should be able to connect the Jrk’s TX line directly to your device’s RX line. If your device’s RX line is not 5V tolerant, you will need to a use a level shifter—a separate board or chip that can convert 5 V signals down to 3.3 V. A voltage divider made with two resistors would work too.
Whenever connecting devices, remember to wire the grounds together, and ensure that each device is properly powered. Unpowered devices with a TTL serial port can turn on or partially on, drawing power from the serial line, which means that extra care must be taken when turning power off and on to reset the devices.
Note: You must use an inverter and level shifter such as a MAX232 or a Pololu 23201a Serial Adapter if you want to interface an RS-232 device with the Jrk. Connecting an RS-232 device directly to the Jrk can permanently damage it.
If you are using an Arduino or Arduino-compatible board, you should now try running the SerialSetTarget example that comes with the Jrk G2 Arduino library. The library’s README has information about how to get started and which pins of the Arduino to use. If you are using a different kind of microcontroller board, you will need to find or write code to control the Jrk on your platform. If you are writing your own code, we recommend that you first learn how to send and receive serial bytes on your platform, and then use the SerialSetTarget example and the source code of the Jrk G2 library as a reference. You should also refer to the sections in this guide about the Jrk’s commands (Section 11) and serial protocol (Section 12).
If your connections and code are OK, you should now see your motor moving back and forth. If the motor is not moving, you should check all of your connections and solder joints. You should make sure that the Jrk and your device are configured to use the same baud rate. The Jrk uses 9600 bits per second by default. You should also check the “Status” tab of the Jrk G2 Configuration Utility to see if any errors are being reported.
The SerialSetTarget example only writes data to the Jrk, so it does not test your connection to the Jrk’s TX line. If you want to read data from the Jrk, you should now try the SerialGetFeedback example, which reads the feedback value from the Jrk. If you are using open-loop speed control (“Feedback mode” is “None”), you should change the getScaledFeedback
command in that example to getPIDPeriodCount
so you can see a number that is actually meaningful.
Optional connections
The Jrk’s 5V (out) pin provides access to the output of the Jrk’s 5V regulator, which also powers the Jrk’s microcontroller and the red and yellow LEDs. You can use the Jrk’s regulator to power your microcontroller or other serial device if the device does not draw too much current (see Section 7.8).
The VM pin provides access to the Jrk’s power supply after the reverse-voltage protection circuit, and this pin can be used to provide reverse-voltage-protected power to other components in the system if the Jrk supply voltage is within the operating range of those components. Note: this pin should not be used to supply more than 500 mA; higher-current connections should be made directly to the power supply. Unlike the 5V (out) pin described above, this is not a regulated, logic-level output.
The ERR pin of the Jrk is normally pulled low, but drives high to indicate when an error (other than the “Awaiting command” error bit) is stopping the motor. You can connect this line to an input on your microcontroller (assuming it is 5V tolerant) to quickly tell whether the Jrk is experiencing an error or not. Alternatively, you can query the Jrk’s serial interface to see if an error is happening, and which specific errors are happening. For more information about the ERR pin, see Section 7.7.
The RST pin of the Jrk is connected directly to the reset pin of the Jrk’s microcontroller and also has a 10 kΩ resistor pulling it up to 5 V. You can drive this pin low to perform a hard reset of the Jrk’s microcontroller and immediately turn off the motor, but this should generally not be necessary for typical applications. You should wait at least 10 ms after a reset before sending commands to the Jrk.
Connecting a serial device to multiple Jrks
The Jrk’s serial protocol is designed so that you can control multiple Jrks using a single TTL serial port. Before attempting to do this, however, we recommend that you first get your system working with just one Jrk as described above.
Next, make sure that the serial device and the Jrks all share a common ground, for example by connecting a GND pin from the device to a GND pin on each of the Jrks. Make sure that the TX pin on the serial device is connected to the RX pin of each Jrk (via a level shifter if needed).
If you attempt to run the SerialSetTarget example in this configuration, you should see each of your Jrk controllers moving. That example uses the Jrk’s Compact Protocol, which is only suitable for controlling one device. The Compact Protocol commands do not contain a device number, so every Jrk device that sees a Compact Protocol command will obey it. This is probably not what you want.
To allow independent control of multiple Jrks, you should use the Jrk G2 Configuration Utility to configure each Jrk to have a different device number. Then you should change your code to use the Pololu Protocol as described in Section 12. If you are using our Jrk G2 Arduino library, you can declare one object for each Jrk, and specify the device number of each Jrk, by writing code like this at the top of your sketch, which uses device numbers 11 and 12:
JrkG2Serial jrk1(jrkSerial, 11); JrkG2Serial jrk2(JrkSerial, 12);
If you want to read data from multiple Jrks, you cannot simply connect all of the Jrk TX lines together, because when one of Jrks tries to drive its TX line low to send data, the TX lines from the other Jrks will still be driving the line high and will prevent the signal from going all the way to 0 V. Instead, you will need to connect an external AND gate. The TX line of each Jrk should be connected to an input line of the AND gate. The output of the AND gate should be connected to the RX line of your serial device (through a voltage divider or level shifter if necessary). The following diagram shows these connections along with optional connections of the ERR and RST pins:
Wiring diagram for controlling multiple Jrk G2 modules from a single TTL serial source, such as a microcontroller. |
---|
The ERR pins can all be safely connected together. In such a configuration, the line will be high if one or more Jrks has an error; otherwise, it will be low.
Using I²C instead of serial to read data from multiple Jrks does not require an AND gate (see Section 6.3).
The microcontroller shown in the diagram above can be a Jrk that is connected to a computer via USB. You would set the serial mode of that Jrk to “USB Chained” while leaving the serial modes of the other Jrks set to “UART”. The USB-connected Jrk would act as a USB-to-serial adapter while also listening for serial commands from the computer. You would be able to send serial commands to its USB Command Port in order to control all of the Jrks.
More information about the serial interface
This user’s guide has more information about the Jrk’s commands (Section 11) and serial protocol (Section 12).