Sample Arduino Code for TReX Dual Controller

I recently purchased a TReX Dual controller and I spent this evening writing some code to figure out how to interact with it via serial from my Arduino Uno. I figured that the example might be useful to others. It just sends commands to the TReX to check signature and modes. It can be used as a springboard to other commands.

enjoy,
-Mark

/*
  This is code the demonstrates interaction with the
  TReX Dual Motor Controller from Pololu:
  
  https://www.pololu.com/product/777
  
  Command Reference:
  https://www.pololu.com/file/0J1/TReX_Commands_v1.2.pdf
  
  The circuit setup that this code has been successfully tested
  with is:
  
  Pololu G  -> Arduino GND
  Pololu S0 -> Arduino Pin 5 (configured as Serial RX)
  Pololu S1 -> Arduino Pin 4 (configured as Serial TX)
  
  Separate power is given to the Arduino (via USB cable) and
  the TReX controller (via the motor VIN and GND connectors).
  All jumpers were removed.
  
  This code is free, use it however you want, but no guarantees
  or warranties are given or implied.  Uti periculo tuo.
*/

#include <Stream.h>
// Included for serial communication
#include <SoftwareSerial.h>
 
// Define pins you're using for serial communication
// Do not use pins 0 or 1 as they are reserved for
// standard I/O and programming
#define TREX_TX_PIN 4
#define TREX_RX_PIN 5
 
// Create an instance of the software serial
// communication object. This represents the
// interface with the TReX device
SoftwareSerial trex(TREX_RX_PIN, TREX_TX_PIN);
 
// Main application entry point 
void setup() {
	// Define the appropriate input/output pins
	pinMode(TREX_RX_PIN, INPUT);
	pinMode(TREX_TX_PIN, OUTPUT);
 
 	// Setup serial to use for debug messages
	Serial.begin(9600);
	
	// Setup serial for communicating with the trex interface
	trex.begin(19200);
	
	Serial.println("Setup complete.");
}

void loop() {
	getSignature();
	getMode();
	getSerialIsMaster();
	delay(3000);
}

void getSignature() {
	Serial.println("Getting signature...");
	
	// We expect 7 characters back and we add a null to terminate the string
	char signature[8] = "xxxxxxx";
	uint8_t index = 0;
	
	trex.write(0x81);
	delay(10);
	
	while (trex.available() && index < 8) {
		signature[index++] = trex.read();
	}
	
	Serial.println(signature);
}

void getMode() {
	Serial.println("Getting mode...");

	char mode;

	trex.write(0x82);
	delay(10);
	
	while (trex.available()) {
		mode = trex.read();
	}
	
	Serial.println(mode);
}

void getSerialIsMaster() {
	Serial.println("Getting serial is master...");

	boolean isMaster;

	trex.write(0x83);
	delay(10);
	
	while (trex.available()) {
		isMaster = trex.read();
	}
	
	Serial.println(isMaster);
}

Hello, Mark.

Thank you for sharing your Arduino code! I am sure many of our forum members will appreciate having this example for the TReX.

-Derrill

Thank you for your code, I am trying to figure out why I’m not getting responses from TRex. I get 0’s for
"getting serial master", and xxxxxxxx for for “signature”. Nothing in response to “mode”. The TRex has both blue and red led’s on (very pretty). I removed all the jumpers and attached a ground between the Arduino and the TRex.

I have the TX of the Arduino connected to the RX of the TRex, and the RX of the Arduino connected to the TX of the TRex.

The code compiled fine. I am seeing the text for the Queries on the serial monitor. I’ve rebooted the TRex and reset the Arduino multiple times but no change. What have I forgotten?

I think I should be on Logic Level in and out rather than RX, TX but even after switching those connections I am still not getting any responses from the TRex.

Hello.

Your serial connections should be made to the SO and SI pins as show in the “Signal Connections” section of the TReX user’s guide. Could you describe in more detail what the LEDs are doing? Are you confident the TReX is still set to its default baud rate of 19.2 kbps? If you post pictures of your setup that show your connections, I can take a look and see if there is anything that might cause any issues.

Grant

I apologize for the zombie thread, but I thought I would contribute a little something I figured out:

Be careful about the pins you use. The example setup used pins 4 and 5, however on my Mega/Mega2560, pins 4 and 5 won’t work. As stated in the SoftwareSerial documentation:

  • Not all pins on the Mega and Mega 2560 boards support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69). Not all pins on the Leonardo and Micro boards support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
  • On Arduino or Genuino 101 boards the current maximum RX speed is 57600bps.
  • On Arduino or Genuino 101 boards RX doesn’t work on digital pin 13.

Once I switched to pins 11/12, this example worked flawlessly.

Hope this helps…

P

1 Like