Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

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